这里使用 ViewPager+Fragment 实现滑动效果
当然首先要先上效果图

总共需要的文件:
- 2个Layou文件
- 1个Adapter文件
- 3个Fragment文件
- 1个Activity文件
Layout文件
- activity_main.xml
- fragment_layout.xml
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
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="com.example.a25086.viewpagetest.MainActivity">
<!--实现滑动切换的ViewPager-->
<android.support.v4.view.ViewPager
android:layout_weight="1"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<!--以下是小圆点,就是使用RadioButton实现的-->
<RadioGroup
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_weight="13"
android:orientation="horizontal"
android:layout_width="match_parent"
android

本文介绍了如何利用ViewPager和Fragment来实现页面滑动效果,详细讲解了所需的布局文件、Adapter、Fragment以及MainActivity的设置。在实践中提到了可能出现的问题,如初始化ArrayList、忘记设置Adapter以及getCount方法的正确写法。
最低0.47元/天 解锁文章
1185

被折叠的 条评论
为什么被折叠?



