不显示 ViewPager 布局

ViewPager layouts are not displayed

我正在尝试使用没有片段的 viewpager,仅使用布局文件,但布局不只显示制表点,我做错了什么?

activity_ponto_biometria.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
     <androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/photos_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabDots"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:layout_constraintBottom_toBottomOf="@+id/photos_viewpager"
        app:tabBackground="@drawable/tab_selector"
        app:tabGravity="center"
        app:tabIndicatorHeight="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

AdapterAjudaMarcacao.java

public class AdapterAjudaMarcacao extends PagerAdapter {


@Override
public int getCount() {
    return 3;
}

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

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    int layout;
    if (position == 0){
        layout = R.layout.pop_up_ajuda_1;
    } else if (position == 1){
        layout = R.layout.pop_up_ajuda_2;
    } else {
        layout = R.layout.pop_up_ajuda_3;
    }
    View view = LayoutInflater.from(container.getContext())
            .inflate(layout, container, false);
    container.addView(view);
    return view;
}

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

PontoBiometriaActivity.java

public class PontoBiometriaActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ponto_biometria);
    Objects.requireNonNull(getSupportActionBar()).hide();

    setTitle("Ponto Coletivo");

    ViewPager pager = findViewById(R.id.photos_viewpager);
    PagerAdapter adapter = new AdapterAjudaMarcacao();
    pager.setAdapter(adapter);

    TabLayout tabLayout = findViewById(R.id.tabDots);
    tabLayout.setupWithViewPager(pager, true);

}

显示了tabdots的数量,但是没有显示内容,我做错了什么?

是对的。 而不是:

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

试试这个:

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

根据 documentation,PagerAdapter 需要此方法才能正常运行。

post说明:

The method instantiateItem(ViewGroup, int) returns Object for a particular view. PagerAdapter implementation is considering this Object as a key value when viewpager changes a page.

So, if we return the view itself from instantiateItem(ViewGroup, int), then our key for that page becomes the view itself. We can check return view == object; from isViewFromObject (View view, Object object) which will always return true and our pages will display.