简体   繁体   English

RecyclerView不会在视图之间切换

[英]RecyclerView doesn't switch between views

I am trying to build a navigation drawer with headers. 我正在尝试构建带有标题的导航抽屉。 I've got all working with one exception. 我都工作有一个例外。 First I'll give a brief info about the structure then lay down the exact issue. 首先,我将提供有关该结构的简要信息,然后列出确切的问题。 The structure is as following: 结构如下:

  1. I have 2 layout files, one for the drawer headers view called header_row.xml . 我有2个布局文件,其中一个用于标题为header_row.xml的抽屉标题视图。 It contains two widgets, a textview and an imageview. 它包含两个小部件,一个textview和一个imageview。 The orientation is vertical, so the viewitem will have the imageview setting under the textview. 方向是垂直的,因此viewitem将在textview下具有imageview设置。 The the textview in this layout is the header text, and the imageview is a horizontal line that sits under that header text as a simple divider. 此布局中的textview是标题文本,而imageview是一条水平线,位于该标题文本下方,作为简单的分隔线。

  2. The second layout is the drawer clickable items. 第二种布局是抽屉可单击的项目。 It also contain two widgets, one textview and one imageview. 它还包含两个小部件,一个文本视图和一个图像视图。 the textview is the clickable item. textview是可单击的项目。 And the imageview is that item's icon. imageview是该项目的图标。 This layout is horizontal, so the icon sits to the left of the item. 此布局是水平的,因此图标位于项目的左侧。 This layout is called data_row.xml . 此布局称为data_row.xml

The goal behind these 2 layout files is (try to imagine this) is to have a recycler-view with headers. 这两个布局文件的目的是(尝试想象一下)具有带标题的回收站视图。

My Recycler Adapter code is as follow: 我的回收站适配器代码如下:

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);
    }
}

} }

The problem is that recycler view always takes the first row layout (in my case header_row.xml) and uses it for all the rows regarding their position and thus types as headers (header_row.xml users) or items (data_row.xml users). 问题在于,回收站视图始终采用第一行布局(在我的情况下为header_row.xml),并将其用于所有与行位置相关的行,因此将其用作标题(header_row.xml用户)或项(data_row.xml用户)。

Any help please? 有什么帮助吗? Thank you. 谢谢。

I found the solution to this. 我找到了解决方案。 I had to override another method: getViewItemType(); 我不得不重写另一个方法:getViewItemType(); in the adapter in order to check for headers position at getView(); 在适配器中以便检查标题在getView()的位置; level. 水平。 So just add the following to the code in the original question: 因此,只需将以下内容添加到原始问题的代码中:

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

Just make sure you declare HEADER AND ITEM. 只要确保您声明HEADER和ITEM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM