Switching between recycleview linear layout and grid layout

Step 1: override getItemViewType() method in adapter; add setType(int type) method

  1. //Use this method to set type when clicking switch layout  
  2. public void setType(int type) {  
  3.     this.type = type;  
  4. }  
  5.   
  6. @Override  
  7. //Which type of layout is used to get the current Item  
  8. public int getItemViewType(int position) {  
  9.     return type;  
  10. }  
Step 2: three steps when clicking layout to switch pictures
  1. if (goodsType==0){  
  2.     ivGoodsType.setImageResource(R.mipmap.good_type_grid);  
  3.     //1: Set layout type  
  4.     adapter.setType(1);  
  5.     //2: Set the corresponding layout manager  
  6.     recyclerView.setLayoutManager(new GridLayoutManager(context,2));  
  7.     //3: Refresh adapter  
  8.     adapter.notifyDataSetChanged();  
  9.     goodsType=1;  
  10. }else {  
  11.     ivGoodsType.setImageResource(R.mipmap.good_type_linear);  
  12.     adapter.setType(0);  
  13.     recyclerView.setLayoutManager(new LinearLayoutManager(context));  
  14.     adapter.notifyDataSetChanged();  
  15.     goodsType=0;  
  16. }  
Step 3: in the onCreateViewHolder() method of adapter, switch the item layout according to the layout type you set
  1. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
  2.     View baseView;  
  3.     if (viewType == 0) {  
  4.         baseView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_listview_goods_list, parent, false);  
  5.         LinearViewHolder linearViewHolder = new LinearViewHolder(baseView);  
  6.         return linearViewHolder;  
  7.     } else {  
  8.         baseView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gridview_goods_list, parent, false);  
  9.         GridViewHolder gridViewHolder = new GridViewHolder(baseView);  
  10.         return gridViewHolder;  
  11.     }  
  12. }  
Next, we will know that the top picture appears at the bottom sliding position, and click the picture to realize the top function of RecyclerView;

The top picture is not always displayed on the screen, but can only be displayed by sliding to a certain distance through monitoring; we need to rewrite the sliding monitoring of RecyclerView;

  1. //Set sliding monitor  
  2.         recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {  
  3.             @Override  
  4.             public void onScrolled(RecyclerView recyclerView, int dx, int dy) {  
  5.                 super.onScrolled(recyclerView, dx, dy);  
  6.                 RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();  
  7.                 if (layoutManager instanceof LinearLayoutManager) {  
  8.                     LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;  
  9.                     //Get the first visible location  
  10.                     int firstVisibleItemPosition = linearManager.findFirstVisibleItemPosition();  
  11.                     //Show top icons when sliding to more than 10  
  12.                     if (firstVisibleItemPosition>10) {  
  13.                         ivStick.setVisibility(View.VISIBLE);  
  14.                     }else {  
  15.                         ivStick.setVisibility(View.GONE);  
  16.                     }  
  17.                 }  
  18.             }  
  19.         }); 

Posted by lisa71283 on Sat, 23 May 2020 09:09:17 -0700