0

I have two ListView and I want them to share the same layout position so when I click a button one ListView hides. Maybe this is not possible or there is a better way like fragments?

user3800924
  • 407
  • 1
  • 3
  • 17

2 Answers2

1

Use FrameLayout. This layout view overlies two views over each other.

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

    <ListView
        android:id="@+id/list2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</FrameLayout>

For showing the first page (i.e. the first ListView):

findViewById(R.id.list1).setVisibility(View.VISIBLE);
findViewById(R.id.list2).setVisibility(View.GONE);

And for the second page:

findViewById(R.id.list1).setVisibility(View.GONE);
findViewById(R.id.list2).setVisibility(View.VISIBLE);
Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129
1

fragments are the easy way to do this IF you don't plan on changing the data in your views.

Make a button and

/*create fragment of the opposite view, probably through a boolean field and an if block
then*/
getSupportFragmentManager.beginTransaction().replace(/*your fragments*/).commit().

in your onclicklistener.

Tosh
  • 71
  • 7