Android实战简易教程<二十六>(基于ViewPager实现微信页面切换效果)

来源:互联网 发布:淘宝号可以改名字吗 编辑:程序博客网 时间:2024/05/16 18:42

1.头部布局文件top.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="40dp"  
  5.     android:background="@drawable/title_bar"  
  6.     android:gravity="center"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/title"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="30dp"  
  13.         android:layout_gravity="center"  
  14.         android:text="WeChat"  
  15.         android:textColor="#ffffffff"  
  16.         android:textSize="25dp" />  
  17.   
  18. </LinearLayout>  

2.底部布局文件bottom.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="50dp"  
  5.     android:background="@drawable/bottom_bar"  
  6.     android:orientation="horizontal" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/ll_chat"  
  10.         android:layout_width="0dp"  
  11.         android:layout_height="fill_parent"  
  12.         android:layout_weight="1"  
  13.         android:gravity="center"  
  14.         android:orientation="vertical" >  
  15.   
  16.         <ImageButton  
  17.             android:id="@+id/ibtn_chat"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:background="#0000"  
  21.             android:clickable="false"  
  22.             android:src="@drawable/tab_weixin_pressed" />  
  23.   
  24.         <TextView  
  25.             android:layout_width="wrap_content"  
  26.             android:layout_height="wrap_content"  
  27.             android:text="聊天"  
  28.             android:textColor="#ffffffff" />  
  29.     </LinearLayout>  
  30.   
  31.     <LinearLayout  
  32.         android:id="@+id/ll_pengyouquan"  
  33.         android:layout_width="0dp"  
  34.         android:layout_height="fill_parent"  
  35.         android:layout_weight="1"  
  36.         android:gravity="center"  
  37.         android:orientation="vertical" >  
  38.   
  39.         <ImageButton  
  40.             android:id="@+id/ibtn_pengyouquan"  
  41.             android:layout_width="wrap_content"  
  42.             android:layout_height="wrap_content"  
  43.             android:background="#0000"  
  44.             android:clickable="false"  
  45.             android:src="@drawable/tab_find_frd_normal" />  
  46.   
  47.         <TextView  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:text="朋友圈"  
  51.             android:textColor="#ffffffff" />  
  52.     </LinearLayout>  
  53.   
  54.     <LinearLayout  
  55.         android:id="@+id/ll_tongxunlu"  
  56.         android:layout_width="0dp"  
  57.         android:layout_height="fill_parent"  
  58.         android:layout_weight="1"  
  59.         android:gravity="center"  
  60.         android:orientation="vertical" >  
  61.   
  62.         <ImageButton  
  63.             android:id="@+id/ibtn_tongxunlu"  
  64.             android:layout_width="wrap_content"  
  65.             android:layout_height="wrap_content"  
  66.             android:background="#0000"  
  67.             android:clickable="false"  
  68.             android:src="@drawable/tab_address_normal" />  
  69.   
  70.         <TextView  
  71.             android:layout_width="wrap_content"  
  72.             android:layout_height="wrap_content"  
  73.             android:text="好友"  
  74.             android:textColor="#ffffffff" />  
  75.     </LinearLayout>  
  76.   
  77.     <LinearLayout  
  78.         android:id="@+id/ll_set"  
  79.         android:layout_width="0dp"  
  80.         android:layout_height="fill_parent"  
  81.         android:layout_weight="1"  
  82.         android:gravity="center"  
  83.         android:orientation="vertical" >  
  84.   
  85.         <ImageButton  
  86.             android:id="@+id/ibtn_set"  
  87.             android:layout_width="wrap_content"  
  88.             android:layout_height="wrap_content"  
  89.             android:background="#0000"  
  90.             android:clickable="false"  
  91.             android:src="@drawable/tab_settings_normal" />  
  92.   
  93.         <TextView  
  94.             android:layout_width="wrap_content"  
  95.             android:layout_height="wrap_content"  
  96.             android:text="设置"  
  97.             android:textColor="#ffffffff" />  
  98.     </LinearLayout>  
  99.   
  100. </LinearLayout>  

3.主布局文件:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <include layout="@layout/top" />  
  8.   
  9.     <android.support.v4.view.ViewPager  
  10.         android:id="@+id/viewpager"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="0dp"  
  13.         android:layout_weight="1" >  
  14.     </android.support.v4.view.ViewPager>  
  15.   
  16.     <include layout="@layout/bottom" />  
  17.   
  18. </LinearLayout>  

4.四个ViewPager的内容页布局

tab01.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center"  
  12.         android:text="聊天页"  
  13.         android:textSize="30sp" >  
  14.     </TextView>  
  15.   
  16. </LinearLayout>  
tab02.xml:
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center"  
  12.         android:text="朋友圈"  
  13.         android:textSize="30sp" >  
  14.     </TextView>  
  15.   
  16. </LinearLayout>  

tab03.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center"  
  12.         android:text="联系人页面"  
  13.         android:textSize="30sp" >  
  14.     </TextView>  
  15.   
  16. </LinearLayout>  
tab04.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center"  
  12.         android:text="设置页面"  
  13.         android:textSize="30sp" >  
  14.     </TextView>  
  15.   
  16. </LinearLayout>  

5.MainActivity.java:

[java] view plaincopy
  1. package com.example.tabtest;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.support.v4.view.PagerAdapter;  
  9. import android.support.v4.view.ViewPager;  
  10. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.View.OnClickListener;  
  14. import android.view.ViewGroup;  
  15. import android.view.Window;  
  16. import android.widget.ImageButton;  
  17. import android.widget.LinearLayout;  
  18.   
  19. public class MainActivity extends Activity implements OnClickListener {  
  20.     private ViewPager mViewPager;  
  21.     private PagerAdapter pagerAdapter;  
  22.     private List<View> mViews = new ArrayList<View>();  
  23.     private LinearLayout mLLChat;  
  24.     private LinearLayout mLLPengyouquan;  
  25.     private LinearLayout mLLAddress;  
  26.     private LinearLayout mLLSet;  
  27.     private ImageButton mImageButtonChat;  
  28.     private ImageButton mImageButtonPengyouquan;  
  29.     private ImageButton mImageButtonAddress;  
  30.     private ImageButton mImageButtonSet;  
  31.   
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  36.         setContentView(R.layout.activity_main);  
  37.         initView();// 初始化控件  
  38.         initEvent();// 点击事件  
  39.     }  
  40.   
  41.     private void initEvent() {  
  42.         mLLChat.setOnClickListener(this);  
  43.         mLLPengyouquan.setOnClickListener(this);  
  44.         mLLAddress.setOnClickListener(this);  
  45.         mLLSet.setOnClickListener(this);  
  46.         mViewPager.setOnPageChangeListener(new OnPageChangeListener() {//ViewPager滑动切换监听  
  47.               
  48.             @Override  
  49.             public void onPageSelected(int arg0) {  
  50.                 int currentItem=mViewPager.getCurrentItem();  
  51.                 resetImag();  
  52.                 switch (currentItem) {  
  53.                 case 0:  
  54.                     mImageButtonChat.setImageResource(R.drawable.tab_weixin_pressed);  
  55.                     break;  
  56.                 case 1:  
  57.                     mImageButtonPengyouquan.setImageResource(R.drawable.tab_find_frd_pressed);  
  58.                     break;  
  59.                 case 2:  
  60.                     mImageButtonAddress.setImageResource(R.drawable.tab_address_pressed);  
  61.                     break;  
  62.                 case 3:  
  63.                     mImageButtonSet.setImageResource(R.drawable.tab_settings_pressed);  
  64.                     break;  
  65.   
  66.                 default:  
  67.                     break;  
  68.                 }  
  69.                   
  70.             }  
  71.               
  72.             @Override  
  73.             public void onPageScrolled(int arg0, float arg1, int arg2) {  
  74.                 // TODO Auto-generated method stub  
  75.                   
  76.             }  
  77.               
  78.             @Override  
  79.             public void onPageScrollStateChanged(int arg0) {  
  80.                 // TODO Auto-generated method stub  
  81.                   
  82.             }  
  83.         });  
  84.     }  
  85.   
  86.     private void initView() {  
  87.         /** 
  88.          * 初始化所有控件 
  89.          */  
  90.         mViewPager = (ViewPager) findViewById(R.id.viewpager);  
  91.         mLLChat = (LinearLayout) findViewById(R.id.ll_chat);  
  92.         mLLPengyouquan = (LinearLayout) findViewById(R.id.ll_pengyouquan);  
  93.         mLLAddress = (LinearLayout) findViewById(R.id.ll_tongxunlu);  
  94.         mLLSet = (LinearLayout) findViewById(R.id.ll_set);  
  95.         mImageButtonChat = (ImageButton) findViewById(R.id.ibtn_chat);  
  96.         mImageButtonPengyouquan = (ImageButton) findViewById(R.id.ibtn_pengyouquan);  
  97.         mImageButtonAddress = (ImageButton) findViewById(R.id.ibtn_tongxunlu);  
  98.         mImageButtonSet = (ImageButton) findViewById(R.id.ibtn_set);  
  99.         /** 
  100.          * 获取mViews 
  101.          */  
  102.         LayoutInflater layoutInflater = LayoutInflater.from(this);  
  103.         View tab01 = layoutInflater.inflate(R.layout.tab01, null);  
  104.         View tab02 = layoutInflater.inflate(R.layout.tab02, null);  
  105.         View tab03 = layoutInflater.inflate(R.layout.tab03, null);  
  106.         View tab04 = layoutInflater.inflate(R.layout.tab04, null);  
  107.         mViews.add(tab01);  
  108.         mViews.add(tab02);  
  109.         mViews.add(tab03);  
  110.         mViews.add(tab04);  
  111.         // ViewPager适配器  
  112.         pagerAdapter = new PagerAdapter() {  
  113.   
  114.             /** 
  115.              * 摧毁 
  116.              */  
  117.             @Override  
  118.             public void destroyItem(ViewGroup container, int position,  
  119.                     Object object) {  
  120.                 container.removeView(mViews.get(position));  
  121.             }  
  122.   
  123.             /** 
  124.              * 初始化 
  125.              */  
  126.   
  127.             @Override  
  128.             public Object instantiateItem(ViewGroup container, int position) {  
  129.                 View view = mViews.get(position);  
  130.                 container.addView(view);  
  131.                 return view;  
  132.             }  
  133.   
  134.             @Override  
  135.             public boolean isViewFromObject(View arg0, Object arg1) {  
  136.                 return arg0 == arg1;  
  137.             }  
  138.   
  139.             @Override  
  140.             public int getCount() {  
  141.                 return mViews.size();  
  142.             }  
  143.         };  
  144.   
  145.         mViewPager.setAdapter(pagerAdapter);  
  146.   
  147.     }  
  148.   
  149.     @Override  
  150.     public void onClick(View v) {  
  151.         resetImag();  
  152.         switch (v.getId()) {  
  153.         case R.id.ll_chat:  
  154.             mViewPager.setCurrentItem(0);  
  155.             mImageButtonChat.setImageResource(R.drawable.tab_weixin_pressed);  
  156.             break;  
  157.         case R.id.ll_pengyouquan:  
  158.             mViewPager.setCurrentItem(1);  
  159.             mImageButtonPengyouquan.setImageResource(R.drawable.tab_find_frd_pressed);  
  160.   
  161.             break;  
  162.         case R.id.ll_tongxunlu:  
  163.             mViewPager.setCurrentItem(2);  
  164.             mImageButtonAddress.setImageResource(R.drawable.tab_address_pressed);  
  165.   
  166.             break;  
  167.         case R.id.ll_set:  
  168.             mViewPager.setCurrentItem(3);  
  169.             mImageButtonSet.setImageResource(R.drawable.tab_settings_pressed);  
  170.   
  171.             break;  
  172.   
  173.         default:  
  174.             break;  
  175.         }  
  176.     }  
  177. /** 
  178.  * 将所有图片设置成未选中状态 
  179.  */  
  180.     private void resetImag() {  
  181.         mImageButtonChat.setImageResource(R.drawable.tab_weixin_normal);  
  182.         mImageButtonPengyouquan.setImageResource(R.drawable.tab_find_frd_normal);  
  183.         mImageButtonAddress.setImageResource(R.drawable.tab_address_normal);  
  184.         mImageButtonSet.setImageResource(R.drawable.tab_settings_normal);  
  185.     }  
  186.   
  187. }  

6.运行实例:

源码下载

0 0
原创粉丝点击