简单的viewPager

总结,无非就是一个viewpager控件,然后构建一个pageradater适配器,通过addview方法,把View放进list数组里面,然后把list里面的view,根据position逐个添加进去

package comtest.example.admin.ztest.thread;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import comtest.example.admin.ztest.R;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;

public class TwwoActivity extends Activity{
    private View view1,view2,view3;
    private ViewPager mViewPager;
    private List mList; //list数组
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉顶部栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_viewpager);
        //控件绑定
        mViewPager = (ViewPager)findViewById(R.id.viewpager);
        LayoutInflater inflater = getLayoutInflater();
        //绑定view布局
        view1 = inflater.inflate(R.layout.layout1, null);
        view2 = inflater.inflate(R.layout.layout2, null);
        view3 = inflater.inflate(R.layout.layout3, null);
        btn = (Button)view3.findViewById(R.id.btn_layout3);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), RoadEnvirActivity.class);
                startActivity(intent);
            }
        });

        //添加view到list数组
        mList = new ArrayList();
        mList.add(view1);
        mList.add(view2);
        mList.add(view3);
        //pager的适配器
        PagerAdapter mPagerAdapter = new PagerAdapter() {
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0==arg1;
            }

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

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

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mList.get(position));
                return mList.get(position);
            }
        };
        mViewPager.setAdapter(mPagerAdapter);
    }
}

布局效果

  

你可能感兴趣的:(简单的viewPager)