Let's start with:
Name is the display content, title is the tag of parentItem, name is the title of childitem, and then set a map < string in the adapter, Boolean > if we click parentItem, we will save its name as the key folded / unfolded state as value, and then judge whether to hide the item according to the state
That's what I thought. I just did it. We have good practices that can be optimized. After reading some examples on the Internet, I didn't speak in a comprehensive way, so I wrote a little by myself, hoping to help you,
The effect is as shown in the figure above. Click the title display part, and then click to put it away. Without much nonsense, go to the code directly:
1. Recycle adapter core code:
List<GroundBean> mDatas; Map<String,Boolean> map; public GroundAdapter(Context context, List<GroundBean> mDatas) { super(context); this.mDatas = mDatas; map = new HashMap<>(); } @NonNull @Override public GroundHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new GroundHolder(initViewHolder(parent, R.layout.item_grount)); } @Override public int getItemCount() { return mDatas.size(); } @Override public void onBindViewData(GroundHolder holder, int position) { final GroundBean bean = mDatas.get(position); boolean isGone= map.get(bean.getTitle())==null?true:map.get(bean.getTitle()); holder.itemName.setText(bean.getName()); if (!"".equals(bean.getTitle())){ holder.setVisibility(isGone); holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.white)); }else { //This must be set in the displayed item. Otherwise, some items will be hidden after refreshing (probably due to reuse) holder.setVisibility(false); holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.colorAccent)); } holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if ("".equals(bean.getTitle())){ boolean isSelect = map.get(bean.getName())==null?true:map.get(bean.getName()); map.put(bean.getName(),!isSelect); notifyDataSetChanged(); } } }); } //This is to control how many columns an item occupies in the grid layout @Override public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); RecyclerView.LayoutManager manager = recyclerView.getLayoutManager(); if(manager instanceof GridLayoutManager) { final GridLayoutManager gridManager = ((GridLayoutManager) manager); gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { if (!"".equals(mDatas.get(position).getTitle())){ return 1; }else { return 3; } } }); } }
2,Holder :
public class GroundHolder extends RecycleAdapter.ViewHolder{ @BindView(R.id.item_name) TextView itemName; public GroundHolder(View itemView) { super(itemView); } //This is mainly to set the size of the item to hide and show public void setVisibility(boolean isGone){ RecyclerView.LayoutParams param = (RecyclerView.LayoutParams)itemView.getLayoutParams(); if (isGone){ param.height = 0; param.width = 0; }else{ param.height = LinearLayout.LayoutParams.WRAP_CONTENT; param.width = LinearLayout.LayoutParams.MATCH_PARENT; } itemView.setLayoutParams(param); } }
4. Entity class Bean:
private boolean isSelect; //This is used to mark that some item s are selected (the given code is not used for the time being, but for later extension) private String name;//item display content private String title;//Mark with a name at will (defined by everyone)
5,activity
//Main code this is the data source private void initRecycle() { mDatas.add(new GroundBean("coco1","")); mDatas.add(new GroundBean("item1","coco1")); mDatas.add(new GroundBean("item2","coco1")); mDatas.add(new GroundBean("item3","coco1")); mDatas.add(new GroundBean("item4","coco1")); mDatas.add(new GroundBean("item5","coco1")); mDatas.add(new GroundBean("item6","coco1")); mDatas.add(new GroundBean("item7","coco1")); mDatas.add(new GroundBean("coco2","")); mDatas.add(new GroundBean("item1","coco2")); mDatas.add(new GroundBean("item2","coco2")); mDatas.add(new GroundBean("item3","coco2")); mDatas.add(new GroundBean("item4","coco2")); mDatas.add(new GroundBean("item5","coco2")); mDatas.add(new GroundBean("item6","coco2")); mDatas.add(new GroundBean("item7","coco2")); mDatas.add(new GroundBean("coco3","")); mDatas.add(new GroundBean("item1","coco3")); mDatas.add(new GroundBean("item2","coco3")); mDatas.add(new GroundBean("item3","coco3")); mDatas.add(new GroundBean("item4","coco3")); mDatas.add(new GroundBean("item5","coco3")); mDatas.add(new GroundBean("item6","coco3")); mDatas.add(new GroundBean("item7","coco3")); adapter = new GroundAdapter(mContext, mDatas); mRvGround.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL)); mRvGround.setLayoutManager(new GridLayoutManager(mContext,3)); mRvGround.setAdapter(adapter); }
This function code is very simple. I can't go to the source code. You can send me messages if you need
contact information:
QQ: 915832847
Email: wx_bin@sina.com