使用layout加载PagerAdapter

一.首先要四个布局;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="ylkj.com.basetest.SecondActivity"
    tools:showIn="@layout/activity_second">
    <TextView
        android:id="@+id/xx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/g10"
        android:background="@drawable/btn_ripple"
        android:gravity="center"
        android:padding="@dimen/g10"
        android:text="协议" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--相对于父容器定位-->
        <LinearLayout
            android:id="@+id/li1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:background="#000000"
            android:orientation="horizontal" />
        <!--水平居中-->
        <LinearLayout
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:background="#e41212"
            android:orientation="horizontal" />

        <!--根据兄弟组件定位-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/li1"
            android:layout_marginTop="@dimen/g10">

            <LinearLayout
                android:id="@+id/li"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="#000000"
                android:orientation="horizontal" />

            <LinearLayout
                android:id="@+id/hou"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_toRightOf="@id/li"
                android:background="#e41212"
                android:orientation="horizontal" />
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

其他的三个都是一样的简单的布局,布局比较简单,这里就不贴出来啦。

二.绑定布局;

  list.add(LayoutInflater.from(this).inflate(R.layout.activity_second,null) );

三.给个适配器。

public class ViewPagerAdapter1 extends PagerAdapter{
    private List<View> list ;
    public ViewPagerAdapter1( List<View> list){
        this.list = list;
    }
    //个数
    @Override
    public int getCount() {
        return list.size();
    }
    //判断instantiateItem(ViewGroup, int)函数所返回来的Key与一个页面视图是否是 代表的同一个视图
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    //将给定位置的view添加到ViewGroup(容器)中,创建并显示出来 ②
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(list.get(position));
        return list.get(position);
    }
    //除一个给定位置的页面。适配器从容器中删除这个视图。
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));
    }
}
四.最后写activity的实现
//最简单的viewPager
public class ViewPagerActivity1 extends AppCompatActivity {
    List<View> list ;
    private ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager1);
        initView();
        initData();
    }

    private void initData() {
        list = new ArrayList<>();
        list.add(LayoutInflater.from(this).inflate(R.layout.activity_first_first,null) );
        list.add(LayoutInflater.from(this).inflate(R.layout.activity_second,null) );
        list.add(LayoutInflater.from(this).inflate(R.layout.activity_center,null) );
        list.add(LayoutInflater.from(this).inflate(R.layout.activity_six,null) );

        ViewPagerAdapter1 viewPagerAdapter1 = new ViewPagerAdapter1(list);
        viewPager.setAdapter(viewPagerAdapter1);
    }

    private void initView() {
        viewPager = findViewById(R.id.viewPager1);

    }

}
源码地址下载:https://download.csdn.net/download/weixin_42267745/10433573





 
 

猜你喜欢

转载自blog.csdn.net/weixin_42267745/article/details/80427014