android recycleView嵌套recycleView下拉二级列表显示(带动画)

不多说,直接上代码

1、调用方式:

MyAdapter myAdapter = new MyAdapter(this);
myAdapter.setmList(list);//list自定义数据

2、MyAdapter类

public class MyAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List mList;
    private ExpandableViewHoldersUtil.KeepOneHolder keepOne;

    public void setmList(List mList) {
        this.mList = mList;
    }

    public MyAdapter(Context context, UpDateListener listener) {
        super();
        mContext = context;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_add_custom_layout, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
        Log.d("Logger", "MyAdapter onBindViewHolder: " + position);
        keepOne.bind(viewHolder, position);
        if (mList == null) {
            return;
        }
        //TODO第一层显示样式处理
        viewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                keepOne.toggle(viewHolder);
            }
        });

        viewHolder.iv_editone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onEdit(bean);
            }
        });

        viewHolder.img_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                keepOne.toggle(viewHolder);
            }
        });

      
        ItemIconAddListAdapter menuAdapter = new ItemIconAddListAdapter(listItemBaseBean);
        //第二层展示样式
        viewHolder.rc_item.setLayoutManager(new GridLayoutManager(mContext, 5));
        menuAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                //TODO 自己的逻辑
            }
        });
        viewHolder.rc_item.setAdapter(menuAdapter);
    }

    class ViewHolder extends RecyclerView.ViewHolder implements ExpandableViewHoldersUtil.Expandable, View.OnClickListener, View.OnLongClickListener {
        TextView tv_icon_name;
        ImageView img_select;
        ImageView img_icon;
        ImageView iv_editone;
        RecyclerView rc_item;
        RelativeLayout relativeLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            img_icon = itemView.findViewById(R.id.img_icon);
            img_select = itemView.findViewById(R.id.img_select);
            rc_item = itemView.findViewById(R.id.rc_item);
            tv_icon_name = itemView.findViewById(R.id.tv_icon_name);
            relativeLayout = itemView.findViewById(R.id.ll_item);
            keepOne = ExpandableViewHoldersUtil.getInstance().getKeepOneHolder();
            iv_editone = itemView.findViewById(R.id.iv_editone);
            rc_item.setVisibility(View.GONE);
            rc_item.setAlpha(0);
        }

        @Override
        public View getExpandView() {
            return rc_item;
        }

        @Override
        public void doCustomAnim(boolean isOpen) {//旋转指定图片
            if (isOpen) {
                ExpandableViewHoldersUtil.getInstance().rotateExpandIcon(img_select, 90, 0);
            } else {
                ExpandableViewHoldersUtil.getInstance().rotateExpandIcon(img_select, 0, 90);
            }
        }

        @Override
        public void onClick(View view) {
            int position = (int) view.getTag();
            Log.d("Logger", "ViewHolder onClick: ");

        }

        @Override
        public boolean onLongClick(View view) {
            int position = (int) view.getTag();
            Log.d("Logger", "ViewHolder onLongClick: ");

            return false;
        }
    }

}



布局文件item_add_custom_layout.xml



    

        

        

            
        

        


        


    

    

3、第二层Adapter

public class ItemIconAddListAdapter extends BaseQuickAdapter {
    public ItemIconAddListAdapter(List list) {
        super(R.layout.item_two_icon_layout,list);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, ItemBaseBean data) {
       //TODO
        显示样式

    }

}

效果图:

android recycleView嵌套recycleView下拉二级列表显示(带动画)_第1张图片

 

你可能感兴趣的:(android)