PageViewer中的ListView未显示

时间:2018-05-17 15:51:12

标签: java android listview

我正在尝试在ViewPager中显示ListView,但它根本没有显示出来。 我找到的唯一有点相似的答案是Update ViewPager dynamically?,但是这使用了FragmentPagerAdapter和Fragments,但我没有。

可以找到该应用的屏幕截图here

我对视图进行了不同的着色,以便更好地了解显示的内容和不显示的内容。 Button1下方的浅绿色quare是ListView - 所以它会显示出来!它只是不显示它的项目..为什么?

提前谢谢。

我的代码

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));
}

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

这两个页​​面都是这样的

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

0 个答案:

没有答案