使用ViewPager+Fragment实现选项卡切换效果

原博客:http://blog.csdn.net/chdjj/article/details/21564503


后面我添加了页卡的点击事件,大家一起学习下。


显示效果:


实现过程:
1.创建3个选项卡中的内容,即三个Fragment。
这里仅贴出其中一个:
Tab2:
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.viewpagerdemo3;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Button;  
  10. import android.widget.Toast;  
  11.   
  12. public class Tab2 extends Fragment  
  13. {  
  14.     private Button but = null;  
  15.     @Override  
  16.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  17.             Bundle savedInstanceState)  
  18.     {  
  19.         View view = inflater.inflate(R.layout.tab2,null);//注意不要指定父视图  
  20.         but = (Button) view.findViewById(R.id.but);  
  21.         return view;  
  22.     }  
  23.     @Override  
  24.     public void onActivityCreated(Bundle savedInstanceState)  
  25.     {  
  26.         super.onActivityCreated(savedInstanceState);  
  27.         but.setOnClickListener(new OnClickListener()  
  28.         {  
  29.             @Override  
  30.             public void onClick(View v)  
  31.             {  
  32.                 Toast.makeText(Tab2.this.getActivity(),"hahah"0).show();  
  33.             }  
  34.         });  
  35.     }  
  36. }  

布局:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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:background="#00ff00"  
  6.     android:orientation="vertical" >  
  7.       
  8.     <TextView   
  9.         android:id="@+id/tab1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="我是第二个界面"  
  13.         />  
  14.     <Button   
  15.         android:id="@+id/but"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="我是按钮"  
  19.         />  
  20. </LinearLayout>    
剩下两个Fragment类似。
2.主界面布局
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.     tools:context=".MainActivity" >  
  7.     <LinearLayout  
  8.         android:id="@+id/linearLayout1"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="#FFFFFF"  
  12.         android:orientation="horizontal" >  
  13.         <TextView  
  14.             android:id="@+id/text1"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="fill_parent"  
  17.             android:layout_weight="1.0"  
  18.             android:gravity="center"  
  19.             android:text="页卡1"  
  20.             android:textColor="#000000"  
  21.             android:textSize="22.0dip" />  
  22.         <TextView  
  23.             android:id="@+id/text2"  
  24.             android:layout_width="fill_parent"  
  25.             android:layout_height="fill_parent"  
  26.             android:layout_weight="1.0"  
  27.             android:gravity="center"  
  28.             android:text="页卡2"  
  29.             android:textColor="#000000"  
  30.             android:textSize="22.0dip" />  
  31.         <TextView  
  32.             android:id="@+id/text3"  
  33.             android:layout_width="fill_parent"  
  34.             android:layout_height="fill_parent"  
  35.             android:layout_weight="1.0"  
  36.             android:gravity="center"  
  37.             android:text="页卡3"  
  38.             android:textColor="#000000"  
  39.             android:textSize="22.0dip" />  
  40.     </LinearLayout>  
  41.     <ImageView  
  42.         android:id="@+id/cursor"  
  43.         android:layout_width="fill_parent"  
  44.         android:layout_height="wrap_content"  
  45.         android:scaleType="matrix"  
  46.         android:src="@drawable/line" />  
  47.     <android.support.v4.view.ViewPager  
  48.         android:id="@+id/vPager"  
  49.         android:layout_width="wrap_content"  
  50.         android:layout_height="wrap_content"  
  51.         android:layout_gravity="center"  
  52.         android:background="#000000"  
  53.         android:flipInterval="30"  
  54.         android:persistentDrawingCache="animation"  
  55.          />  
  56. </LinearLayout>  
三个TextView代表选项卡标题,ImageView代表选项卡下的下划线。
3.实现activity。
[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.viewpagerdemo3;  
  2.   
  3. import android.graphics.BitmapFactory;  
  4. import android.graphics.Color;  
  5. import android.graphics.Matrix;  
  6. import android.os.Bundle;  
  7. import android.support.v4.app.Fragment;  
  8. import android.support.v4.app.FragmentActivity;  
  9. import android.support.v4.app.FragmentPagerAdapter;  
  10. import android.support.v4.view.ViewPager;  
  11. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  12. import android.util.DisplayMetrics;  
  13. import android.view.animation.Animation;  
  14. import android.view.animation.TranslateAnimation;  
  15. import android.widget.ImageView;  
  16. import android.widget.TextView;  
  17.   
  18. public class MainActivity extends FragmentActivity  
  19. {  
  20.     private ViewPager vPager = null;  
  21.     /**  
  22.      * 代表选项卡下的下划线的imageview  
  23.      */  
  24.     private ImageView cursor = null;  
  25.     /**  
  26.      * 选项卡下划线长度  
  27.      */  
  28.     private static int lineWidth = 0;  
  29.       
  30.     /**  
  31.      * 偏移量  
  32.      *         (手机屏幕宽度/3-选项卡长度)/2  
  33.      */  
  34.     private static int offset = 0;  
  35.       
  36.     /**  
  37.      * 选项卡总数  
  38.      */  
  39.     private static final int TAB_COUNT = 3;  
  40.     /**  
  41.      * 当前显示的选项卡位置  
  42.      */  
  43.     private int current_index = 0;  
  44.       
  45.     /**  
  46.      * 选项卡标题  
  47.      */  
  48.     private TextView text1,text2,text3;  
  49.      
  50.     @Override  
  51.     protected void onCreate(Bundle savedInstanceState)  
  52.     {  
  53.         super.onCreate(savedInstanceState);  
  54.         setContentView(R.layout.activity_main);  
  55.   
  56.         vPager = (ViewPager) findViewById(R.id.vPager);  
  57.           
  58.         initImageView();  
  59.         text1 = (TextView) findViewById(R.id.text1);  
  60.         text2 = (TextView) findViewById(R.id.text2);  
  61.         text3 = (TextView) findViewById(R.id.text3);  
  62.         final TextView[] titles = {text1,text2,text3};  
  63.         vPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())  
  64.         {  
  65.             @Override  
  66.             public int getCount()  
  67.             {  
  68.                 return TAB_COUNT;  
  69.             }  
  70.              
  71.             @Override  
  72.             public Fragment getItem(int index)//直接创建fragment对象并返回  
  73.             {  
  74.                 switch (index)  
  75.                 {  
  76.                 case 0:  
  77.                     return new Tab1();  
  78.                 case 1:  
  79.                     return new Tab2();  
  80.                 case 2:  
  81.                     return new Tab3();  
  82.                 }  
  83.                 return null;  
  84.             }  
  85.         });  
  86.         vPager.setOnPageChangeListener(new OnPageChangeListener()  
  87.         {  
  88.             int one = offset * 2 + lineWidth;// 页卡1 -> 页卡2 偏移量  
  89.             @Override  
  90.             public void onPageSelected(int index)//设置标题的颜色以及下划线的移动效果  
  91.             {  
  92.                 Animation animation = new TranslateAnimation(one*current_index,one*index, 0,0);  
  93.                 animation.setFillAfter(true);  
  94.                 animation.setDuration(300);  
  95.                 cursor.startAnimation(animation);  
  96.                 titles[current_index].setTextColor(Color.BLACK);  
  97.                 titles[index].setTextColor(Color.RED);  
  98.                 current_index = index;  
  99.             }  
  100.              
  101.             @Override  
  102.             public void onPageScrolled(int arg0, float arg1, int arg2)  
  103.             {  
  104.             }  
  105.              
  106.             @Override  
  107.             public void onPageScrollStateChanged(int index)  
  108.             {  
  109.             }  
  110.         });  
  111.     }  
  112.     private void initImageView()  
  113.     {  
  114.         cursor = (ImageView) findViewById(R.id.cursor);  
  115.         //获取图片宽度  
  116.         lineWidth = BitmapFactory.decodeResource(getResources(),R.drawable.line).getWidth();  
  117.         DisplayMetrics dm = new DisplayMetrics();  
  118.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  119.         //获取屏幕宽度  
  120.         int screenWidth = dm.widthPixels;  
  121.         Matrix matrix = new Matrix();  
  122.         offset = (int) ((screenWidth/(float)TAB_COUNT - lineWidth)/2);  
  123.         matrix.postTranslate(offset, 0);  
  124.         //设置初始位置  
  125.         cursor.setImageMatrix(matrix);  
  126.     }  
  127. }  



下面是选项卡的点击事件:
//首先先findviewbyid   然后设监听事件
tvTabActivity = (TextView) findViewById(R.id.tv_tab_activity);
         tvTabGroups = (TextView) findViewById(R.id.tv_tab_groups);
         tvTabFriends = (TextView) findViewById(R.id.tv_tab_friends);
       
 
         tvTabActivity.setOnClickListener( new MyOnClickListener( 0 ));
         tvTabGroups.setOnClickListener( new MyOnClickListener( 1 ));
         tvTabFriends.setOnClickListener( new MyOnClickListener( 2 ));
       

//接下来是监听
public class MyOnClickListener implements View.OnClickListener {
         private int index = 0 ;
 
         public MyOnClickListener( int i) {
             index = i;
         }
 
         @Override
         public void onClick(View v) {
             mPager.setCurrentItem(index);//mpager是我们自己的viewpage
         }
     }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值