简体   繁体   English

回收站视图getItemViewType位置问题

[英]Recycler View getItemViewType position issue

Inside my adapter I use getItemViewType to decide based on the value wether to add a header and search indicator to the recyclerview. 在我的适配器内部,我使用getItemViewType根据值决定是否向recyclerview添加标题和搜索指示器。

getItemViewType getItemViewType

private static final int TYPE_SEARCH = 0;
private static final int TYPE_HEADER = 1;
private static final int TYPE_CELL = 2;

@Override
public int getItemViewType(int position) {

    if (position == 0) {
        return TYPE_HEADER;
    }
    else if(position  == 1) {
        return TYPE_SEARCH;
    }
    else
        return TYPE_CELL;
}

 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        final View convertView;
        switch (viewType) {
            case 0:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_search_ind, parent, false);
                return new Adapter.ViewHolderSearch(convertView);
            case 1:
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_header, parent, false);
                return new Adapter.ViewHolderHeader(convertView);
            case 2:
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_normal_layout, parent, false);
               return new Adapter.ViewHolderFeed(convertView);
        }
        return null;
    }

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
   ....
}

So if the position is 0 it will inflate and return the header layout card, if position is 1 it will inflate and return the search layout card, else just return the "normal layout". 因此,如果位置为0,则将膨胀并返回标题布局卡;如果位置为1,则将膨胀并返回搜索布局卡,否则仅返回“常规布局”。 What's happening now is, is seems like the header and search "headers" are using the two spaces where Dog10 and Dog9 should go. 现在正在发生的事情似乎是标题和搜索“标题”正在使用Dog10和Dog9应该放在的两个空格。 So now my recyclerview feed starts at Dog8. 所以现在我的recyclerview提要从Dog8开始。 If I remove the two "headers", my recyclerview feed starts at Dog10. 如果删除两个“标题”,则我的recyclerview feed从Dog10开始。 See image below to make sense of what I'm saying. 请看下面的图片以理解我的意思。

Image of my app RecyclerView feed 我的应用程序RecyclerView提要的图像

How do I get around this? 我该如何解决? Should I just add two dummy entries to the data that gets returned from mysql. 我应该只将两个虚拟条目添加到从mysql返回的数据中。

You can add dummy entries for the list. 您可以为列表添加虚拟条目。 But make sure the onBindViewHolder() does not bind for position 0 and 1. 但是请确保onBindViewHolder()不绑定位置0和1。

Yes, you can add two objects at index 0 and 1. and also better if you take property (instance variable) as a viewType so your adapter doesn't care about where you want to show header and search. 是的,您可以在索引0和1处添加两个对象。如果将属性(实例变量)作为viewType,也可以更好,这样适配器就不必在意标题和搜索的位置了。 and you add your dummy data with type Search and Header and give the default type as Item cell. 然后使用“搜索”和“标题”添加您的虚拟数据,并将默认类型指定为“项目”单元格。 hope It will clear all your doubts. 希望它将清除您的所有疑问。

@Override
public int getItemViewType(int position) {
    return data.get(position).getViewType();
}

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

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