0

I'm trying to display a ListView inside of a ViewPager, but it simply doesn't show up. The only somewhat similar answer that i found is Update ViewPager dynamically?, however this uses a FragmentPagerAdapter and Fragments, which i don't.

A screenshot of the app can be found here.

I colored the Views differently to better see what's being displayed and what isn't. The light-green quare below Button1 is the ListView - so it does get shown! It just doesn't display its items.. why is that?

Thanks in advance.

My Code

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] items = {"one", "two", "tree", "four", "five", "six", "seven"};

    ViewPager myViewPager = findViewById(R.id.v_ViewPager_Content);
    myViewPager.setAdapter(new PagerAdapter()
    {
        @Override
        public int getCount()
        {
            return 2;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
        {
            return view == object;
        }

        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object)
        {
            container.removeView((ConstraintLayout) object);
        }

        @Override
        @NonNull
        public Object instantiateItem(@NonNull ViewGroup container, int position)
        {
            if (position == 0)
            {
                View myView = Objects.requireNonNull(getSystemService(LayoutInflater.class)).inflate(R.layout.viewpager_page1, container, false);
                container.addView(myView);
                return myView;
            } else if (position == 1)
            {
                View myView = Objects.requireNonNull(getSystemService(LayoutInflater.class)).inflate(R.layout.viewpager_page2, container, false);
                container.addView(myView);
                return myView;
            } else
            {
                throw new IllegalArgumentException("There are only 2 pages, man.");
            }
        }
    });
    Objects.requireNonNull(myViewPager.getAdapter()).instantiateItem(myViewPager, 0);
    Objects.requireNonNull(myViewPager.getAdapter()).instantiateItem(myViewPager, 1);

    ListView myListView = myViewPager.findViewById(R.id.v_ListView_List1);
    myListView.setAdapter(new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, items));
}

The activity_main.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v4.view.ViewPager
    android:id="@+id/v_ViewPager_Content"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@android:color/holo_blue_bright"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Both pages look like this

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/v_Button_Page1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:background="@android:color/holo_green_dark"
    android:text="Page 1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ListView
    android:id="@+id/v_ListView_List1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@android:color/holo_green_light"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/v_Button_Page1" />

  • Setting layout_width and layout_height to 0dp makes your ListView's view pretty much non-existent. Try either "match_parent" or "wrap_content" for those settings. – ZengoTim May 17 '18 at 19:03
  • @ZengoTim This is incorrect. I am using a constraintlayout and setting the toBottomOf, toEndOf and the toStartOf constraint to "parent", and the toBottomOf to the bottom of the Button. This is somewhat similar to using "math_parent". Additionally you can see in the screenshot that the ListView **does** get shown in the way it is supposed to. Just the items are missing. – Daniel Kargl May 17 '18 at 19:36
  • Yes, you are correct. All the examples I am seeing for using the default ArrayAdapter with the built in textView specify the generic of the adapter. Have you tried using new ArrayAdapter ... ? – ZengoTim May 18 '18 at 16:16
  • @ZengoTim What is the 'generic' of the adapter? Anyway, the ArrayAdapter works fine. It doesn't matter wheter i define it first as ArrayAdapter foo = new ArrayAdapter<>(...); and then use it like setAdapter(foo); or if i just use setAdapter(new ArrayAdapter<>(...)); since both variants work fine when used outside of a ViewPager. – Daniel Kargl May 18 '18 at 16:38
  • Here's a link to the whole Project if anyone want's to try and mess around with it without having to set it up (open with android studio) [ViewPagerTest](https://www.dropbox.com/s/pkre1j4wdta75f0/ViewPagerTest.rar?dl=0) – Daniel Kargl May 18 '18 at 16:41

0 Answers0