1

i want to change view frm gridview to listview after view gets clicked by user in a Fragment.

But i am not being able to do that . Searched a lot on google Quora and SO but didnt finad any solution

Here is my Fragment code

public class FragmentAlbum extends Fragment  {

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,Bundle savedInstanceState) {
   root = (ViewGroup) inflater.inflate(R.layout.album,container, false);


    activity = getActivity();
        lv=(GridView) root.findViewById(R.id.gridview);
        lv.setAdapter(adaptor);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

          root = (ViewGroup) inflater.inflate(R.layout.album_list,container, false);
         listview=(ListView) root.findViewById(android.R.id.list);

                  listview.setAdapter(adaptor1);    
                  break;

         }

             }}
);

        return root;
}

. When i click view remains in Gridview initial view doesnt goes off.

album is gridview layout.

album_list is listview layout.

i am not getting any error but logically its not working.

Can anyone suggest me my mistake?.

Thanx in advance

Animesh Mangla
  • 773
  • 7
  • 31

4 Answers4

4

You can use recyclerview for this.

Set layoutmanager as gridlayout manager for gridview and linearlayout manager for listview.

use your logics according to conditions.

  • Heyy @Shashank Garg can you tell me that how can I change layouts in the gridview. I mean to say that I have different layout for gridview and listview. – MashukKhan Jun 26 '18 at 10:30
2

One option available to you is to use a GridView and change the number of columns dynamically depending on state. GridView has a numColumns property which you can change at runtime and then apply by calling notifyDataSetChanged on your adapter.

Ivan Wooll
  • 4,145
  • 3
  • 23
  • 34
2

You can use RecyclerView and change the ListView to GridView based on your column count like following, this is very easy while compared to other posts.

private void setLayoutManager() {
    if (mColumnCount <= 1) {
        mColumnCount = 1;
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
    } else {
        recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
    }
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
1

If you want to change the GridView-style to ListView-style and ListView-style to GridView progamatically. Then just change the NO-OF-COLUMNS value of gridview programmatically . Like here

  switchIV.setOnClickListener(new OnClickListener()
    {
        public void onClick(View arg0) 
        {

            if(style==0)
            {

                //Change gridview to listview style
                gridview.setNumColumns(1);
                Update_with_ListView_Style();
                style=1;

            }
            else
            {
                //change gridview back to gridview style with 3 columns
                gridview.setNumColumns(3);
                 Update_with_GridView_Style();
                 style=0;

            }
        }

    });
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81