ViewPager和View组合 实现页面的切换

 

//--------------主页面-------------------------------

package com.bw.test;

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

import com.bw.test.adapter.MyViewPagerAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private ViewPager vp;
    private List<View> views=new ArrayList<View>();
    private TextView tv_textview1;
    private TextView tv_textview2;
    private TextView tv_textview3;
    private TextView tv_textview4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //找到button按钮控件
        tv_textview1 = (TextView) findViewById(R.id.tv_textview1);
        tv_textview2 = (TextView) findViewById(R.id.tv_textview2);
        tv_textview3 = (TextView) findViewById(R.id.tv_textview3);
        tv_textview4 = (TextView) findViewById(R.id.tv_textview4);
        //找到ViewPager控件
        vp = (ViewPager) findViewById(R.id.vp);
        //设置ViewPager的适配器
        MyViewPagerAdapter adapter=new MyViewPagerAdapter(views, this);
        vp.setAdapter(adapter);
        //初始化view
        initView();
        //设置ViewPager滑动监听
        vp.setOnPageChangeListener(new OnPageChangeListener() {
            
            @Override
            public void onPageSelected(int arg0) {
                switch (arg0) {
                case 0:
                    //设置textview的背景颜色
                    tv_textview1.setBackgroundColor(Color.RED);                    
                    tv_textview2.setBackgroundColor(Color.WHITE);
                    tv_textview3.setBackgroundColor(Color.WHITE);
                    tv_textview4.setBackgroundColor(Color.WHITE);
                    break;
                case 1:
                    tv_textview1.setBackgroundColor(Color.WHITE);
                    tv_textview2.setBackgroundColor(Color.RED);    
                    tv_textview3.setBackgroundColor(Color.WHITE);
                    tv_textview4.setBackgroundColor(Color.WHITE);
                    break;
                case 2:
                    tv_textview1.setBackgroundColor(Color.WHITE);
                    tv_textview2.setBackgroundColor(Color.WHITE);
                    tv_textview3.setBackgroundColor(Color.RED);    
                    tv_textview4.setBackgroundColor(Color.WHITE);
                    break;
                case 3:
                    tv_textview1.setBackgroundColor(Color.WHITE);
                    tv_textview2.setBackgroundColor(Color.WHITE);
                    tv_textview3.setBackgroundColor(Color.WHITE);
                    tv_textview4.setBackgroundColor(Color.RED);    
                    break;
                

                default:
                    break;
                }
                
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                
                
            }
            
            @Override
            public void onPageScrollStateChanged(int arg0) {
                
                
            }
        });
        //设置监听
        tv_textview1.setOnClickListener(this);
        tv_textview2.setOnClickListener(this);
        tv_textview3.setOnClickListener(this);
        tv_textview4.setOnClickListener(this);
        //设置默认页面
        tv_textview1.setBackgroundColor(Color.RED);                            
        vp.setCurrentItem(0);
    }

//初始化view
    private void initView() {
        //创建view布局
        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);
        //把view布局添加到集合
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view4);
        
        

        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.tv_textview1:
            vp.setCurrentItem(0);
            break;
        case R.id.tv_textview2:
            vp.setCurrentItem(1);
            break;
        case R.id.tv_textview3:
            vp.setCurrentItem(2);
            break;
        case R.id.tv_textview4:
            vp.setCurrentItem(3);
            break;

        default:
            break;
        }
        
    }
    
}

//=============主布局文件    main.xml===========================

<LinearLayout 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:orientation="vertical"
     >

   <android.support.v4.view.ViewPager
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:id="@+id/vp"></android.support.v4.view.ViewPager>
   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <TextView
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="40dp"
           android:text="首页"
           android:gravity="center"
           android:id="@+id/tv_textview1"/>
       <TextView
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="40dp"
           android:text="详情"
           android:gravity="center"
           android:id="@+id/tv_textview2"/>
       <TextView
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="40dp"
           android:text="购物车"
           android:gravity="center"
           android:id="@+id/tv_textview3"/>
       <TextView
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="40dp"
           android:text="我的"
           android:gravity="center"
           android:id="@+id/tv_textview4"/>
   </LinearLayout>

</LinearLayout>

//============创建一个MyViewPagerAdapter,继承PagerAdapter并实现里面的方法=========================

package com.bw.test.adapter;

import java.util.List;

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

//创建一个MyViewPagerAdapter,继承PagerAdapter并实现里面的方法


public class MyViewPagerAdapter extends PagerAdapter{
    private List<View> views;
    private Context context;
    
    //有参构造
    public MyViewPagerAdapter(List<View> views, Context context) {
        super();
        this.views = views;
        this.context = context;
    }
    //获得长度
    @Override
    public int getCount() {
        
        return views.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        
        return arg0==arg1;
    }
    //展示的view
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //获得展示的view
        View view=views.get(position);
        //添加到容器
        container.addView(view);
        //返回显示的view
        return view;
    }
    //销毁view
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //从容器中移除view
        container.removeView((View) object);
    }

}

//============创建   view1.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"
    android:background="#ff00ff">
    
</LinearLayout>

 

//=======创建   view2 .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"
    android:background="#ffff00" >
    

</LinearLayout>

 

//=====创建  view3.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"
    android:background="#0000ff" >
    

</LinearLayout>

 

//=========创建  view4.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"
    android:background="#00ffff" >
    

</LinearLayout>

 

//=======完了,做完之后就是页面的切换========

posted on 2016-08-25 20:35  巫山老妖  阅读(182)  评论(0编辑  收藏  举报