developer developer - 1 year ago 90
Android Question

Dynamically change to Listview frm Gridview

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 {

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(;

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
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(;




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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download