How to set multiple layout in FirebaseRecyclerAdapter?

后端 未结 2 1548
半阙折子戏
半阙折子戏 2021-01-07 16:02

I just make a chat application with Firebase and using RecyclerView in XML and using FirebaseRecyclerAdapter in Android, but the problem is that when I send a message throug

相关标签:
2条回答
  • 2021-01-07 16:23

    public Class MyAdapter extends FirebaseRecyclerAdapter<ModelClass, ViewHolderClass> {
    
        private static final int LAYOUT_ONE = 1;
    
        public MyAdapter(Class<ModelClass> modelClass,
                           int modelLayout,
                           Class<ViewHolderClass> viewHolderClass,
                           Query ref) {
    
            super(modelClass, modelLayout, viewHolderClass, ref);
    
        }
        
        @Override
        public ViewHolderClass onCreateViewHolder(ViewGroup parent, int viewType) {
        
            View view;
            
            if (viewType == LAYOUT_ONE) {
    
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_one, parent, false);
                return new ViewHoldeClassr(view);
    
            } 
            
            else {
    
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_two, parent, false);
                return new ViewHolderClass(view);
    
            }
        }
        
        @Override
        public int getItemViewType(int position) 
        
            ModelClass model = getItem(position);
            
            if (model.caseWhenYouWentToUseLayoutOne)) {
                return LAYOUT_ONE;
            }
            
            return position;
        }
    
    }

    0 讨论(0)
  • 2021-01-07 16:24

    The FirebaseUI adapters were made for a homogenous lists with a single layout. While having heterogenous list with multiple layouts were always considered an interesting feature to add (see this original feature request), it hasn't been added so far.

    There is an open feature request for specifically adding multi-layout support on the FirebaseUI library: https://github.com/firebase/FirebaseUI-Android/issues/305.

    You might want to add your vote/+1 to that.

    0 讨论(0)
提交回复
热议问题