Android RecycleView添加header后自动下滑,跳过header

如题,我在adapter里通过viewType设置了header,但是一进入界面就自动划过header。
但是header是存在的,下滑也会有,就是一刷新就自动跳过header,有大神知道原因吗

这是adapter中onCreateViewHolder()方法代码

 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View headerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_online, parent, false);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            headerView.setLayoutParams(layoutParams);
            return new OnlineHeaderHolder(headerView, context);
        } else if (viewType == TYPE_STAR) {
            View starView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_online_star, parent, false);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            starView.setLayoutParams(layoutParams);
            return new OnlineStarViewHolder(starView, context);
        } else {
            View normalView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_online, parent, false);
            return new OnlineListHolder(normalView, context);
        }
    }

这是我header的布局文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/xx"
    android:background="@color/colorPrimary">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/dimens_xh_15dp"
        android:layout_marginStart="@dimen/dimens_xh_15dp"
        android:text="@string/latest_active"
        android:textColor="@color/xx"
        android:textSize="17sp" />

    <ImageView
        android:id="@+id/xx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginEnd="@dimen/xx"
        android:layout_marginRight="@dimen/xx"
        android:layout_toLeftOf="@+id/xx"
        android:layout_toStartOf="@+id/xx"
        android:src="@mipmap/xx" />

    <ImageView
        android:id="@+id/xx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="xx"
        android:layout_marginRight="xx"
        android:src="@mipmap/xx" />

</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/xx" />

</LinearLayout>

下面是我adapter的onbindviewholder的代码

 @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    OnlineNewAdaper.OnlineModel onlineModel = modelList.get(position);
    switch (onlineModel.getType()) {
        case TYPE_HEADER:
            ((OnlineHeaderHolder) holder).setData(onlineModel);
            ((OnlineHeaderHolder) holder).setOnlineSelectListener(listener);
            break;
        case TYPE_STAR:
            ((OnlineStarViewHolder) holder).setData(onlineModel);
            break;
        case TYPE_NORMAL:
            ((OnlineListHolder) holder).setData(onlineModel, mServerTime);
            break;
        default:
            break;
    }
}

这里OnlineModel 有一个type的属性,用type区分类型

你应该是在哪里调用了scollToPosition之类的定位函数,检查一下代码

这段代码是没啥问题,但是不觉得过度冗余了吗?例如:获取布局加载器,没必要每次都实例化,放在构造方法里即可。适当的简化下吧,这代码看的有点......

建议把布局也贴出来,定位下问题原因。

我给你补齐了一些代码,亲测没问题。你看看:
public class OnlineNewAdaper extends RecyclerView.Adapter {
private final int TYPE_HEADER = 1;
private final int TYPE_STAR = 2;
private final int TYPE_NORMAL = 3;
private Context context;
private int mServerTime;
private List modelList = new ArrayList<>();
private OnlineSelectListener listener;
int type;
private String TAG =getClass().getSimpleName();

public OnlineNewAdaper(Context context) {
    this.context = context;

    for (int i = 0; i < 10; i++) {
        if (i==0){
            type=TYPE_HEADER;
        }else if (i==1){
            type = TYPE_STAR;
        }else {
            type = TYPE_NORMAL;
        }
        modelList.add(new OnlineModel(i,type));
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_HEADER) {
        View headerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_online, parent, false);
        return new OnlineHeaderHolder(headerView, context);
    } else if (viewType == TYPE_STAR) {
        View starView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_online_star, parent, false);
        return new OnlineStarViewHolder(starView, context);
    } else {
        View normalView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_online, parent, false);
        return new OnlineListHolder(normalView, context);
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    OnlineNewAdaper.OnlineModel onlineModel = modelList.get(position);
    int type = onlineModel.getType();
    Log.e(TAG, "onBindViewHolder: "+type );
    switch (type) {
        case TYPE_HEADER:
            ((OnlineHeaderHolder) holder).setData(onlineModel);
            ((OnlineHeaderHolder) holder).setOnlineSelectListener(listener);
            break;
        case TYPE_STAR:
            ((OnlineStarViewHolder) holder).setData(onlineModel);
            break;
        case TYPE_NORMAL:
            ((OnlineListHolder) holder).setData(onlineModel, mServerTime);
            break;
        default:
            break;
    }
}

@Override
public int getItemViewType(int position) {
    if (position==0){
        return TYPE_HEADER;
    }else if (position==1){
        return TYPE_STAR;
    }else {
        return TYPE_NORMAL;
    }
}

@Override
public int getItemCount() {
    return 10;
}

public static class OnlineStarViewHolder extends RecyclerView.ViewHolder{

    private final TextView test;
    private OnlineModel data;

    public OnlineStarViewHolder(View starView, Context context) {
        super(starView);
        test = ((TextView) starView.findViewById(R.id.start_tv));
    }

    public void setData(OnlineModel data) {
        this.data = data;
        test.setText("position=="+String.valueOf(data.getI())+"--type=="+String.valueOf(data.getType()));
    }
}

private class OnlineHeaderHolder extends RecyclerView.ViewHolder {
    private final ImageView iv1;
    private final ImageView iv2;
    private final TextView tv;
    private OnlineModel data;
    private OnlineSelectListener onlineSelectListener;

    public OnlineHeaderHolder(View headerView, Context context) {
        super(headerView);
        tv = ((TextView) headerView.findViewById(R.id.header_tv));
        iv1 = ((ImageView) headerView.findViewById(R.id.iv1));
        iv2 = ((ImageView) headerView.findViewById(R.id.iv2));
    }

    public void setData(OnlineModel data) {
        this.data = data;
        tv.setText("头");
    }


    public void setOnlineSelectListener(OnlineSelectListener onlineSelectListener) {
        this.onlineSelectListener = onlineSelectListener;
    }
}

private class OnlineListHolder extends RecyclerView.ViewHolder {
    private OnlineModel onlineModel;
    private int serverTime;

    public OnlineListHolder(View normalView, Context context) {
        super(normalView);
    }

    public void setData(OnlineModel onlineModel, int mServerTime) {
        this.onlineModel = onlineModel;
        this.serverTime = mServerTime;
    }
}

private class OnlineModel {
    private int i;
    private int type;

    public OnlineModel(int i, int type) {
        this.i = i;
        this.type= type;
    }

    public int getI() {
        return i;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setI(int i) {
        this.i = i;
    }
}

private interface OnlineSelectListener {
    void seleted(int postion);
}

}

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/header_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_marginStart="15dp"
            android:text="最近活动"
            android:textColor="@color/colorAccent"
            android:textSize="17sp" />

        <ImageView
            android:id="@+id/iv1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="26dp"
            android:layout_marginRight="26dp"
            android:src="@mipmap/beauty" />

        <ImageView
            android:id="@+id/iv2"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="28dp"
            android:layout_marginRight="28dp"
            android:layout_toLeftOf="@+id/iv1"
            android:layout_toStartOf="@+id/iv1"
            android:src="@drawable/control" />

    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f00" />

</LinearLayout>

xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="250dp">

<ImageView
    android:id="@+id/list_iv"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:src="@mipmap/beauty"
    android:scaleType="centerInside"
    />

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/start_tv"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="测试"
        />
</LinearLayout>

public class TestActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.test_rv);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(new OnlineNewAdaper(this));
}

}

##  activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/test_rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

你的代码只贴出了一部分,我不好测试,只能自己加了一些垃圾代码。头一次回答别人问题,贴出来的代码果然辣眼睛。。。。。

问题解决了,如果有需要的可以看看
问题是因为我在竖向recycleview中添加了横向recycleview,这个横向recycleview会获取焦点,从而影响竖向的,在竖向Recycleview中添加
android:descendantFocusability="blocksDescendants"属性
详细参考
http://cashow.github.io/Android-Debug-Log-For-Recyclerview-Auto-Scroll-Down.html