两种方式实现RecyclerView多布局

方式一如图:

 两种方式实现RecyclerView多布局_第1张图片

 

具体思路步骤:

  1. 子项有3种类型, 分别是TextView+EditView, TextView+RadioGroup, TextView+imageView. 且除第1、2项外还有点击事件
  2. 新建3个item.xml供不同种子项使用
  3. createHolder时判断子项类型而调用不同的item.xml

 

代码实现: 

  1. 首先创建Item实体并加入自定义属性
    public class ItemEntity {
    	public String title;
    	public int type; //1代表子项1, 2代表子项2, 3代表子项剩余其他
    }
  2. Activity中添加数据
    List itemList = new ArrayList<>();
    
    ItemEntity i0 = new ItemArrange();
    i0.title = "方案名称";
    i0.type = 0;
    itemList.add(i0);
    
    ItemEntity i1 = new ItemArrange();
    i1.title = "性别";
    i1.type = 1;
    itemList.add(i1);
    
    ...
  3. ItemAdapter
    public class ItemAdapter extends RecyclerView.Adapter{
        private List itemList = new ArrayList<>();
        public ItemAdapter(List mItemList) {
            itemList = mItemList;
        }
    
        @Override
        public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            ItemAdapter.ViewHolder holder = null;
            switch (viewType){
                case 1:
                    holder = new ItemAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_1, parent, false));
                    break;
                case 2:
                    holder = new ItemAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_2, parent, false));
                    break;
                case 3:
                    holder = new ItemAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_3, parent, false));
                    break;
                }
            return holder;
        }
    
        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            final ItemEntity item = itemList.get(position);
            holder.tvTitle.setText(itemList.get(position).title);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                ... 
                //除了第一、二项外,其他项需要点击事件,可以ItemEntity再加个属性进行判断
            }
        }
    
        @Override
        public int getItemViewType(int position) {
            //在第一次createHolder时会调用
            return itemList.get(position).type;
        }
    
    
        ...
    
    }

 

方式二如图: 

两种方式实现RecyclerView多布局_第2张图片

方式一适用于子项部件差异不大的情况, 代码比较简略

如果出现的组件种类过多, 不适合创建一个实例将所有种类写进去. 则可以用方式二

static List itemList;
public void initList(){
    itemList = new ArrayList<>();
    item i1 = new item(R.drawable.ic_reminder,"声音与提醒");
    itemList.add(i1);
    item i2 = new item(R.drawable.ic_help,"帮助");
    itemList.add(i2);
    item i3 = new item(R.drawable.ic_info,"关于");
    itemList.add(i3);
}
class item{
    int itemIconId;
    String itemText;
    public item(int itemIconId,String itemText){
        this.itemIconId = itemIconId;
        this.itemText = itemText;
    }
}


class itemAdapter extends RecyclerView.Adapter{
    //总共有3种类型布局,登录、间隔、设置项
    final static int AVATAR = 0;
    final static int SETTING = 1;
    final static int INTERVAL = 2;

    public itemAdapter(){ }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        switch(viewType){
            case AVATAR:
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_avatar, viewGroup, false);
                return new avatarHolder(view);
            case INTERVAL:
                View view1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_interval, viewGroup, false);
                return new intervalHolder(view1);
            default:
                View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_setting, viewGroup, false);
                return new settingHolder(view2);
        }
    }
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        //因为头像布局和间隔布局是固定格式的,所以只需要给每个设置项绑定就可以了
        if(viewHolder instanceof settingHolder) setSettingHolder((settingHolder)viewHolder,position);
    }
    @Override
    public int getItemCount() {
        //设置项的个数 + 3(1个登录项 + 2个间隔项)
        return itemList.size() + 3;
    }
    @Override
    public int getItemViewType(int position) {
        switch (position){
            case 0: return AVATAR;
            case 1:
            case 3: return INTERVAL;
            default:return SETTING;
        }
    }
    class avatarHolder extends RecyclerView.ViewHolder{
        public avatarHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
    class intervalHolder extends RecyclerView.ViewHolder{
        public intervalHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
    class settingHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;
        public settingHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageview);
            textView = itemView.findViewById(R.id.textView);
        }
    }
    public void setSettingHolder(settingHolder viewHolder, int position){
        switch (position){
            //注意每个设置项在RV和在itemList中的position是不一定相同的
            case 2:
                viewHolder.imageView.setImageResource(itemList.get(0).itemIconId);
                viewHolder.textView.setText(itemList.get(0).itemText);
                break;
            case 4:
                viewHolder.imageView.setImageResource(itemList.get(1).itemIconId);
                viewHolder.textView.setText(itemList.get(1).itemText);
                break;
            case 5:
                viewHolder.imageView.setImageResource(itemList.get(2).itemIconId);
                viewHolder.textView.setText(itemList.get(2).itemText);
                break;
        }
        //viewHolder.itemView.setOnClickListener(new View.OnClickListener() {...}
    }
}

 

你可能感兴趣的:(Android)