简体   繁体   English

Android Recyclerview:在单击按钮时将行从线性更改为网格

[英]Android Recyclerview : change row from linear to grid on button click

On button click I want to change list view to gridview as on shopping-cart pages but my layout looks like link shown below. 在按钮上单击,我想将列表视图更改为购物车页面上的gridview,但是我的布局看起来像下面所示的链接。

//For displaying the row as linear list view        
case R.id.ivGrid:
    ivList.setVisibility(View.VISIBLE);
    ivGrid.setVisibility(View.GONE);
    LinearLayoutManager llm = new LinearLayoutManager(context);
    rcvProducts.setLayoutManager(llm);
    break;

//For displaying the row as gridview
case R.id.ivList:
    ivList.setVisibility(View.GONE);
    ivGrid.setVisibility(View.VISIBLE);
    GridLayoutManager glm = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL, false);
    rcvProducts.setLayoutManager(glm);
    break;

GRIDview 网格视图

LISTview 列表显示

Just use a GridLayoutManager and change the span count from 1 to 2 and back again. 只需使用GridLayoutManager并将范围计数从1更改为2,然后再次更改即可。

view.setOnClickListener(new View.OnClickListener(){
    public void onClick(View view){
        GridLayoutManager layoutManager = (GridLayoutManager) grid.getLayoutManger();
        layoutMananger.setSpanCount(layoutManager.getSpanCount() == 2 ? 1 : 2);
    }
})
  1. Maintain 2 adapters, one for ListView and one for GridView. 维护2个适配器,一个用于ListView,一个用于GridView。
  2. On button click, change the adapter to RecyclerView. 单击按钮后,将适配器更改为RecyclerView。
  3. Listview.setAdapter(gridAdapter) (or) ListView.setAdapter(listAdapter) Listview.setAdapter(gridAdapter)(或)ListView.setAdapter(listAdapter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在LayoutManger类型(网格-线性)之间进行切换时,更改Recyclerview行设计 - Change Recyclerview row design when swiching between LayoutManger types (Grid - Linear) 如何在android中的recyclerView行中单击按钮以打开alertDialog? - How to open a alertDialog on click of the button in the row of a recyclerView in android? Android RecyclerView:点击可在行上滑动 - Android RecyclerView: row swipe on click 自动旋转设备时更改 RecyclerView 布局(从线性到网格和反向) - Change RecyclerView Layout (from Linear to Grid and reverse) when rotate device automatically 处理按钮在RecyclerView中的行内单击 - Handle Button click inside a row in RecyclerView Robotium:如何自动在RecyclerView行中单击按钮? - Robotium: How to automate button click in the RecyclerView row? Recyclerview with Tablelayout On row click listner not working in android - Recyclerview with Tablelayout On row click listner not working in android Android单击按钮即可删除所有带有动画的recyclerview - Android remove all recyclerview with animation on a button click 在Android Recyclerview中单击单个单选按钮 - Single Radio Button click in Android Recyclerview 在android上单击按钮在recyclerview的2个视图之间切换 - Switch between 2 views of a recyclerview on a button click in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM