68、 FragmentPagerAdapter+ViewPager实现Tab

<LinearLayout 
***
    <android.support.v4.view.ViewPager
        android:id="@+id/id_viewpager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    <include layout="@layout/bottom" />
</LinearLayout>
  1 package com.imooc.tab03;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  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.view.View;
 13 import android.view.View.OnClickListener;
 14 import android.view.Window;
 15 import android.widget.ImageButton;
 16 import android.widget.LinearLayout;
 17 
 18 public class MainActivity extends FragmentActivity implements OnClickListener {
 19     
 20     private ViewPager mViewPager;
 21     private FragmentPagerAdapter mAdapter;
 22     private List<Fragment> mFragments;
 23 
 24     private LinearLayout mTabWeixin;
 25     private LinearLayout mTabFrd;
 26     private LinearLayout mTabAddress;
 27     private LinearLayout mTabSettings;
 28 
 29     private ImageButton mImgWeixin;
 30     private ImageButton mImgFrd;
 31     private ImageButton mImgAddress;
 32     private ImageButton mImgSettings;
 33 
 34     @Override
 35     protected void onCreate(Bundle savedInstanceState) {
 36         super.onCreate(savedInstanceState);
 37         requestWindowFeature(Window.FEATURE_NO_TITLE);
 38         setContentView(R.layout.activity_main);
 39 
 40         initView();
 41         initEvent();
 42 
 43         setSelect(1);
 44     }
 45 
 46     private void initEvent() {
 47         mTabWeixin.setOnClickListener(this);
 48         mTabFrd.setOnClickListener(this);
 49         mTabAddress.setOnClickListener(this);
 50         mTabSettings.setOnClickListener(this);
 51     }
 52 
 53     private void initView() {
 54         mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
 55 
 56         mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);
 57         mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
 58         mTabAddress = (LinearLayout) findViewById(R.id.id_tab_address);
 59         mTabSettings = (LinearLayout) findViewById(R.id.id_tab_settings);
 60 
 61         mImgWeixin = (ImageButton) findViewById(R.id.id_tab_weixin_img);
 62         mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
 63         mImgAddress = (ImageButton) findViewById(R.id.id_tab_address_img);
 64         mImgSettings = (ImageButton) findViewById(R.id.id_tab_settings_img);
 65 
 66         mFragments = new ArrayList<Fragment>();
 67         Fragment mTab01 = new WeixinFragment();
 68         Fragment mTab02 = new FrdFragment();
 69         Fragment mTab03 = new AddressFragment();
 70         Fragment mTab04 = new SettingFragment();
 71         mFragments.add(mTab01);
 72         mFragments.add(mTab02);
 73         mFragments.add(mTab03);
 74         mFragments.add(mTab04);
 75 
 76         mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
 78             @Override
 79             public int getCount() {
 80                 return mFragments.size();
 81             }
 83             @Override
 84             public Fragment getItem(int arg0) {
 85                 return mFragments.get(arg0);
 86             }
 87         };
 88         mViewPager.setAdapter(mAdapter);
 89 
 90         mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
 92             @Override
 93             public void onPageSelected(int arg0) {
 94                 int currentItem = mViewPager.getCurrentItem();
 95                 setTab(currentItem);
 96             }
 98             @Override
 99             public void onPageScrolled(int arg0, float arg1, int arg2) {
100                 // TODO Auto-generated method stub
101             }
103             @Override
104             public void onPageScrollStateChanged(int arg0) {
105                 // TODO Auto-generated method stub
106             }
107         });
108     }
109 
110     @Override
111     public void onClick(View v) {
112         switch (v.getId()) {
113         case R.id.id_tab_weixin:
114             setSelect(0);
115             break;
116         case R.id.id_tab_frd:
117             setSelect(1);
118             break;
119         case R.id.id_tab_address:
120             setSelect(2);
121             break;
122         case R.id.id_tab_settings:
123             setSelect(3);
124             break;
125 
126         default:
127             break;
128         }
129     }
130 
131     private void setSelect(int i) {
132         setTab(i);
133         mViewPager.setCurrentItem(i);
134     }
135 
136     private void setTab(int i) {
137         resetImgs();   // 切换图片至暗色
138         // 设置图片为亮色
139         // 切换内容区域
140         switch (i) {
141         case 0:
142             mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
143             break;
144         case 1:
145             mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
146             break;
147         case 2:
148             mImgAddress.setImageResource(R.drawable.tab_address_pressed);
149             break;
150         case 3:
151             mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
152             break;
153         }
154     }
155 
156     /**
157      * 切换图片至暗色
158      */
159     private void resetImgs() {
160         mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
161         mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
162         mImgAddress.setImageResource(R.drawable.tab_address_normal);
163         mImgSettings.setImageResource(R.drawable.tab_settings_normal);
164     }
165 
166 }

DEMO:http://download.csdn.net/detail/androidsj/9354313

posted on 2015-12-14 15:38  大米稀饭  阅读(201)  评论(0编辑  收藏  举报