ViewPager(二)

转载请注明出处ViewPager(二)_Mr_Leixiansheng的博客-CSDN博客

           

步骤:(与ListView相似)

1、List<T> list = new ArrayList<>(); 作为数据源

2、设置适配器PagerAdapter(或者自己定义适配器)

3、ViewPager添加适配

代码如下:

1、4个布局页面(因为都相同只贴一个)

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

    <TextView
        android:text="这是第一个界面"
        android:textSize="20dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

2、设置自定义适配器

package com.example.leixiansheng.myviewpager;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by Leixiansheng on 2017/3/18.
 */

public class MyPagerAdapter extends PagerAdapter {

    List<View> viewList;
    List<String> titleList;

    public MyPagerAdapter( List<View> viewList,List<String> titleList) {
        this.viewList = viewList;
        this.titleList = titleList;
    }

    /**
     * 页面数量
     */
    @Override
    public int getCount() {
        return viewList.size();
    }

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

    /**
     * 实例化页面
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(viewList.get(position));
        return viewList.get(position);
    }

    /**
     * 销毁页面
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position));
    }

    /**
     * 返回标题
     */
    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}

3、主程序实现ViewPager

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title"
            android:layout_gravity="bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </android.support.v4.view.PagerTitleStrip>


        <android.support.v4.view.PagerTabStrip
            android:id="@+id/tab"
            android:layout_marginTop="100dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </android.support.v4.view.PagerTabStrip>


    </android.support.v4.view.ViewPager>
</LinearLayout>

package com.example.leixiansheng.myviewpager;

import android.graphics.Color;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
    private ViewPager viewPager;
    private List<View> viewList;
    private List<String> titleList;
    private MyPagerAdapter adapter;
    //自行选择使用PagerTitleStrip、还是PagerTabStrip
    private PagerTitleStrip titleStrip;
    private PagerTabStrip tabStrip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        //监听
        viewPager.setOnPageChangeListener(this);
//        titleStrip = (PagerTitleStrip) findViewById(R.id.pager_title);
        tabStrip = (PagerTabStrip) findViewById(R.id.tab);


        viewList = new ArrayList<>();
        View view1 = View.inflate(this, R.layout.view1, null);
        View view2 = View.inflate(this, R.layout.view2, null);
        View view3 = View.inflate(this, R.layout.view3, null);
        View view4 = View.inflate(this, R.layout.view4, null);

        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);

        titleList = new ArrayList<>();
        titleList.add("第一页");
        titleList.add("第二页");
        titleList.add("第三页");
        titleList.add("第四页");

        /**
         * PagerTabStrip 属性设置
         */
        tabStrip.setTextColor(Color.RED);
        tabStrip.setTabIndicatorColor(Color.BLUE);
        tabStrip.setDrawFullUnderline(false);

        adapter = new MyPagerAdapter(viewList,titleList);

        viewPager.setAdapter(adapter);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Toast.makeText(this, "当前是第"+(position+1)+"个页面",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值