-1

On button click I want to change list view to gridview as on shopping-cart pages but my layout looks like link shown below.

//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

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
abbasalid
  • 43
  • 14

2 Answers2

3

Just use a GridLayoutManager and change the span count from 1 to 2 and back again.

view.setOnClickListener(new View.OnClickListener(){
    public void onClick(View view){
        GridLayoutManager layoutManager = (GridLayoutManager) grid.getLayoutManger();
        layoutMananger.setSpanCount(layoutManager.getSpanCount() == 2 ? 1 : 2);
    }
})
alex
  • 6,359
  • 1
  • 23
  • 21
-1
  1. Maintain 2 adapters, one for ListView and one for GridView.
  2. On button click, change the adapter to RecyclerView.
  3. Listview.setAdapter(gridAdapter) (or) ListView.setAdapter(listAdapter)
Logo
  • 1,366
  • 2
  • 11
  • 16
  • If I am not wrong, its layout manager that is responsible for deciding the format of recyclerview. Adapters just help to put the data into views. – Mohammed Atif May 12 '17 at 05:06
  • This is a bad practice of creating two adapters, What if the data is dynamic, You;ll have to update both, that's a time consuming process, This is totally a wrong approach!! – Sanoop Surendran May 12 '17 at 05:22