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