项目中内层列表的快速赋值

2020-08-12  本文已影响0人  朱_c713
  private void initRoomConfigRecyclerView() {
        binding.rvRoomConditionList.setLayoutManager(new DisableScrollGridLayoutManager(this, 5));
        binding.rvRoomConditionList.addItemDecoration(new DividerGridItemDecoration(this, 10));
        binding.rvRoomConditionList.setAdapter(new CommomRvAdapter<RoomConfigurationObj>(mViewModel.roomConditionList, R.layout.room_condition_list_item) {
            @Override
            public void onBind(CommomViewHolder holder, RoomConfigurationObj item, int position) {
                RoomConditionListItemBinding itemBinding = (RoomConditionListItemBinding) holder.getItemBinding();
                itemBinding.setItem(item);
                itemBinding.cbSelect.setVisibility(View.GONE);
            }
        });
    }

想想还是分装的很好的,不需要分页的列表,我们的这个框架还是能快速适应的。
基类的adapter

 
public abstract class CommomRvAdapter<T> extends RecyclerView.Adapter<CommomViewHolder> {

    protected  List<T> mData ;
    protected int mLayoutId;

    protected CommomRvAdapter (List list,int layoutId){
        mLayoutId = layoutId;
        replaceData(list);
    }

    @Override
    public CommomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CommomViewHolder viewHolder = CommomViewHolder.get(parent,mLayoutId);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CommomViewHolder holder, int position) {
        onBind(holder,mData.get(position),position);
    }

    public abstract void onBind(CommomViewHolder holder,T item,int position);

    public void replaceData(List<T> list){
        if(list == null){
            list = new ArrayList<>();
        }
        mData = list;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }
}

内部的话,只需要为recyclerview 赋值就可以了。:

      <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_room_condition_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:paddingBottom="@dimen/activity_horizontal_margin"
                    android:visibility="@{viewModel.roomConditionList.isEmpty ? View.GONE : View.VISIBLE}"
                    app:items="@{roomInfo.chamberConfiguration}"
                    app:layout_constraintTop_toBottomOf="@id/tv_room_condition_title"/>
上一篇 下一篇

猜你喜欢

热点阅读