一、ViewPager+fragment可以实现界面的滑动与点击跳转,结合ViewPager,Fragment之间的滑动很简单,下面首先我们来看看如何实现fragment结合ViewPager的使用:
图片效果
这是程序主界面向左滑动会滑到第二个fragment上
这里写代码片:这是MainActivity布局,很简单
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.viewpagertest.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:flipInterval="30"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
这里写代码片:主活动的代码,声明一个ViewPager,为其设置Adapter,将数据传入Adapter,这里的的数据是用一个List集合装载的,里面就是Fragment实例,所以除了写主活动的布局之外还需写两个碎片的布局和实例
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private List<F