(原创)安卓快速使用简单的ViewPager

发布时间 2023-11-14 22:47:31作者: lmj625

原创声明:本文所有图片和代码皆由本人制作和编写。

前言

这学期刚开的安卓课程,为了写实验上网查资料,只想找简明扼要的教程来让我快速写完实验,不过大多数教程会先进行长篇介绍,对于赶ddl的我有点太详细了。通过实验后,我写下这篇简洁的文章作为备忘录,最后还有个大作业要写。

本文仅涉及最简ViewPager使用方法,适合用于刚接触安卓的新手。



目标与效果

使用一个通用ViewPager布局来对题目和答案进行展示。


滑动翻页效果:



4步走

第一:在布局文件添加ViewPager组件

哪个活动需要ViewPager组件就在该活动的.xml布局文件添加如下代码:

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

第二:为ViewPager设计布局

创建一个新的.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/pageNumberTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/questionTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/answerTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="16sp" />

</LinearLayout>

第三:创建适配器类

至少需要实现以下两个方法:
getItemCount(): 返回ViewPager中包含的项(即页面)的总数。
createItem(int position): 返回给定位置的页面。

我这里主要是为了显示当前页的页码,所以单独创建了一个类。

public class CustomPagerAdapter extends PagerAdapter {
    private Context context;
    private List<Questions.Question> questionList;

    public CustomPagerAdapter(Context context) {
        this.context = context;
        this.questionList = new Questions().getQuestionList();
    }

    @Override
    public int getCount() {
        return questionList.size();
    }

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

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_question, container, false);

        TextView pageNumberTextView = view.findViewById(R.id.pageNumberTextView);
        TextView questionTextView = view.findViewById(R.id.questionTextView);
        TextView answerTextView = view.findViewById(R.id.answerTextView);

        Questions.Question question = questionList.get(position);

        pageNumberTextView.setText("第 " + (position + 1) + " 页"); // 设置页码
        questionTextView.setText(question.getText());
        answerTextView.setText(question.getAnswer());

        container.addView(view);

        return view;
    }

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



第四:把布局文件和适配器跟主活动联系起来

public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_pager);

            ViewPager viewPager = findViewById(R.id.viewPager);
            CustomPagerAdapter adapter = new CustomPagerAdapter(this);
            viewPager.setAdapter(adapter);
        }

}


后记

感谢你看到这里。