码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager可滑动页面+点击标题栏切换视图

时间:2015-09-21 21:24:24      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

功能:

  1. 滑动切换视图、标题栏。
  2. 标题栏字体颜色随当前视图改变而改变。
  3. 点击标题栏可切换视图。

1.主页面布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidui.ViewPagerFixedTitleActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="标题1" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="标题2" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="标题3" />
    </LinearLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/vp_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="15dp" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

 

2.为显示每个ViewPager页面新建xml文件:vp_page1.xml,vp_page2.xml,vp_page3.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="TextView2" />

</LinearLayout>

 

3.字体颜色配置文件colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="selected">#99D7FF</color>
    <color name="unselected">#FA646C</color>

</resources>

 

4.Activity类:

public class ViewPagerFixedTitleActivity extends Activity implements OnClickListener {
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private ViewPager viewPager;
    private int selectedColor;
    private int unSelectedColor;

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

        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);

        // 获取颜色资源
        selectedColor = getResources().getColor(R.color.selected);
        unSelectedColor = getResources().getColor(R.color.unselected);

        // 设置初次显示时字体颜色
        tv1.setBackgroundColor(unSelectedColor);
        tv2.setBackgroundColor(unSelectedColor);
        tv3.setBackgroundColor(unSelectedColor);

        viewPager = (ViewPager) findViewById(R.id.vp_pager);

        // 初始化子页面
        LayoutInflater inflater = getLayoutInflater();
        View view1 = inflater.inflate(R.layout.vp_page1, null);
        View view2 = inflater.inflate(R.layout.vp_page2, null);
        View view3 = inflater.inflate(R.layout.vp_page3, null);

        // 添加进list
        List<View> views = new ArrayList<View>();
        views.add(view1);
        views.add(view2);
        views.add(view3);

        MyViewPagerAdapter adapter = new MyViewPagerAdapter(views);
        viewPager.setAdapter(adapter);

        // 为ViewPager添加监听器
        viewPager.addOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    // 设置标题1被选中,其他的不选中
                    tv1.setBackgroundColor(selectedColor);
                    tv2.setBackgroundColor(unSelectedColor);
                    tv3.setBackgroundColor(unSelectedColor);
                } else if (position == 1) {
                    // 标题2
                    tv2.setBackgroundColor(selectedColor);
                    tv1.setBackgroundColor(unSelectedColor);
                    tv3.setBackgroundColor(unSelectedColor);

                } else if (position == 2) {
                    // 标题3
                    tv3.setBackgroundColor(selectedColor);
                    tv2.setBackgroundColor(unSelectedColor);
                    tv1.setBackgroundColor(unSelectedColor);
                }
            }

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

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class MyViewPagerAdapter extends PagerAdapter {
        private List<View> views;

        public MyViewPagerAdapter() {

        }

        public MyViewPagerAdapter(List<View> views) {
            this.views = views;
        }

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

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

        @Override
        public Object instantiateItem(View container, int position) {
            View view = views.get(position);
            ViewPager viewPager = (ViewPager) container;
            viewPager.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = views.get(position);
            ViewPager viewPager = (ViewPager) container;
            viewPager.removeView(view);
        }

    }

    // 标题栏点击事件
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
        case R.id.textView1:
            viewPager.setCurrentItem(0);
            break;
        case R.id.textView2:
            viewPager.setCurrentItem(1);
            break;
        case R.id.textView3:
            viewPager.setCurrentItem(2);
            break;
        default:
            break;
        }
    }
}

 

重写public Object instantiateItem(View container, int position)方法还是重写 public Object instantiateItem(ViewGroup container, int position)方法没有实际区别,因为public Object instantiateItem(ViewGroup container, int position)方法内部调用的还是public Object instantiateItem(View container, int position)

ViewPager可滑动页面+点击标题栏切换视图

标签:

原文地址:http://www.cnblogs.com/mada0/p/4827161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!