FirebaseRecyclerAdapter doesn't recognize first layout as a position

后端 未结 2 2062
余生分开走
余生分开走 2020-12-06 20:52

My Title is kind of hard to understand but basically when I add items into my database is should display it in a RecyclerView. Now in my RecyclerView I have two

2条回答
  •  臣服心动
    2020-12-06 21:13

    The ideal solution would have been to set two adapters on a single RecyclerView but unfortunatelly this is not possible. However, you can make a single custom Adapter that handles two types of items. I will explain this by getting an example.

    Let's assume you need to display objects of two types, humans and aliens. Your objects require completely different layouts and completely different ViewHolders. Please see the below code for the ViewHolders:

    public class MyAdapter extends RecyclerView.Adapter {
        private static class HumanViewHolder extends RecyclerView.ViewHolder {
            public HumanViewHolder(View itemView) {
                super(itemView);
    
                //Prepare your ViewHolder
            }
    
            public void bind(Human human) {
    
                //Display your human object
            }
        }
    
        private static class AlienViewHolder extends RecyclerView.ViewHolder {
            public AlienViewHolder(View itemView) {
                super(itemView);
    
                //Prepare your ViewHolder
            }
    
            public void bind(Alien alien) {
    
                //Display your alien object
            }
        }
    }
    

    First you need to add two different constants to your adapter representing both type of views:

    private static final int ITEM_TYPE_HUMAN;
    private static final int ITEM_TYPE_ALIEN;
    

    To keep things simple, let's also assume you store your objects in a list:

    private List items = new ArrayList<>();
    
    public MyAdapter(List items) {
        this.items.addAll(items);
    
        //Other stuff if needed
    }
    
    
    

    Now, the first you need to do, is to implement getItemViewType() method:

    @Override
    public int getItemViewType(int position) {
        if (items.get(position) instanceof Human) {
            return ITEM_TYPE_HUMAN;
        } else {
            return ITEM_TYPE_ALIEN;
        }
    } 
    

    Second, you need to use the item type inside the onCreateViewHolder() method like this:

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)  {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    
        if (viewType == ITEM_TYPE_HUMAN) {
            View view = layoutInflater.inflate(R.layout.item_human, parent, false);
    
            return new HumanViewHolder(view);
        } else {      
            View view = layoutInflater.inflate(R.layout.item_alien, parent, false);
    
            return new AlienViewHolder(view);
        } 
    } 
    

    In the end, you just need to bind the proper view holder like this:

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        Object item = items.get(position);
    
        if (viewHolder instanceof HumanViewHolder) {
            ((HumanViewHolder) viewHolder).bind((Human) item);
        } else {
            ((AlienViewHolder) viewHolder).bind((Alien) item);
        } 
    } 
    

    提交回复
    热议问题