RecyclerView 不会在视图之间切换

RecyclerView doesn't switch between views

我正在尝试使用 header 构建导航抽屉。除了一个例外,我已经完成了所有工作。首先,我将提供有关结构的简要信息,然后确定确切的问题。 结构如下:

  1. 我有 2 个布局文件,一个用于名为 header_row.xml 的抽屉 header 视图。它包含两个小部件,一个文本视图和一个图像视图。方向是垂直的,所以 viewitem 将在 textview 下设置 imageview。此布局中的文本视图是 header 文本,图像视图是位于 header 文本下方的水平线,作为简单的分隔线。

  2. 第二种布局是抽屉式可点击项目。它还包含两个小部件,一个文本视图和一个图像视图。 textview 是可点击的项目。图像视图是该项目的图标。此布局是水平布局,因此图标位于项目的左侧。此布局称为 data_row.xml .

这 2 个布局文件背后的目标是(试着想象一下)是 recycler-view 和 headers。

我的 Recycler Adapter 代码如下:

package com.android.yousef.tm;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
* Created by Yousef on 3/3/2015.
*/
public class MyRecyclerAdapter extends  
RecyclerView.Adapter<RecyclerView.ViewHolder> {

String[] texts; //An array of the text elements that contains both header 
//text and items text

int[] imgs; // An array of the image elements that contains both headers 
//dividers and items icons.

// NOTE THAT, THE DATA IN THE ABOVE ARRAYS ARE ARRANGE IN ORDER SO THAT 
//POSITIONS 0, 3, AND 5 WILL
// CONTAIN HEADER'S TEXT AND DIVIDER'S IMAGE RESOURCES


private ClickListner clickListner; // Ignore this

//My constructor as bellow getting data from the NavigationDrawer.java class

public MyRecyclerAdapter(String[] texts, int[] imgs) {
    this.texts = texts;
    this.imgs = imgs;
}


// Ignore the listner
public void setClickListner(ClickListner clickListner) {
    this.clickListner = clickListner;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int  i) {

    // positions 0,3,and 5 are my headers positions
    if(i == 0 || i == 3 || i ==5)
    {
        // if the current position is a header's position then return a  
        //header_row item

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.header_row, viewGroup, false);
        return new VHHeader(view);
    }
    else {
        // if the current position is not a header's position then return an  
        //item_row item
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.data_row, viewGroup, false);
        return new VHItem(view);
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

    // if the current position is a header's position then set header's 
    //value: text and divider

    if(viewHolder instanceof VHHeader)
    {
        ((VHHeader) viewHolder).header.setText(texts[i]);
        ((VHHeader) viewHolder).divider.setImageResource(imgs[i]);
    }

    // if the current position is not a header's position then set item's 
    //value: text and icon

    else if(viewHolder instanceof VHItem)
    {
        ((VHItem) viewHolder).title.setText(texts[i]);
        ((VHItem) viewHolder).icon.setImageResource(imgs[i]);
    }
}

@Override
public int getItemCount() {
    //total rows in my recycler view including header and clickable items.
    return 8;
}

//Ignore the interface
public interface ClickListner {

    public void itemClicked(View v, int position);
}

// My Item's ViewHolder class.
class VHItem extends RecyclerView.ViewHolder {
    TextView title;
    ImageView icon;

    public VHItem(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.list_txt_data);
        icon = (ImageView) itemView.findViewById(R.id.list_img_data);
    }
}

// My Header's ViewHolder class
class VHHeader extends RecyclerView.ViewHolder {
    TextView header;
    ImageView divider;

    public VHHeader(View itemView) {
        super(itemView);
        header = (TextView) itemView.findViewById(R.id.list_txt_header);
        divider = (ImageView) itemView.findViewById(R.id.list_img_header);
    }
}

}

问题是回收站视图总是采用第一行布局(在我的例子中是 header_row.xml)并将它用于所有行的位置,因此类型为 headers(header_row.xml 个用户)或项目(data_row.xml 个用户)。

有什么帮助吗?谢谢。

我找到了解决方法。我不得不重写另一个方法:getViewItemType();在适配器中检查 getView() 中的 headers 位置;等级。所以只需在原问题的代码中添加以下内容:

@Override
public int getItemViewType(int position) {
    if(position == 0 || position == 3 || position ==5)
    {
     HEADER = position;
        return HEADER;
    }
    else {
        ITEM = position;
        return ITEM;
    }
}

只需确保声明 HEADER AND ITEM。