• 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(三)


    FragmentPagerAdapter+ViewPager

    与之前直接用ViewPager不同的是,数组里面放的不再是View,而是Fraagment;

    使用FragmentPagerAdapter

    xml文件没有变化,只有代码发生变化

      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     private ViewPager mViewPager;
     20     private FragmentPagerAdapter mAdapter;
     21     private List<Fragment> mFragments;
     22 
     23     private LinearLayout mTabWeixin;
     24     private LinearLayout mTabFrd;
     25     private LinearLayout mTabAddress;
     26     private LinearLayout mTabSettings;
     27 
     28     private ImageButton mImgWeixin;
     29     private ImageButton mImgFrd;
     30     private ImageButton mImgAddress;
     31     private ImageButton mImgSettings;
     32 
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         requestWindowFeature(Window.FEATURE_NO_TITLE);
     37         setContentView(R.layout.activity_main);
     38 
     39         initView();
     40         initEvent();
     41 
     42         setSelect(1);
     43     }
     44 
     45     private void initEvent() {
     46         mTabWeixin.setOnClickListener(this);
     47         mTabFrd.setOnClickListener(this);
     48         mTabAddress.setOnClickListener(this);
     49         mTabSettings.setOnClickListener(this);
     50     }
     51 
     52     private void initView() {
     53         mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
     54 
     55         mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);
     56         mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
     57         mTabAddress = (LinearLayout) findViewById(R.id.id_tab_address);
     58         mTabSettings = (LinearLayout) findViewById(R.id.id_tab_settings);
     59 
     60         mImgWeixin = (ImageButton) findViewById(R.id.id_tab_weixin_img);
     61         mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
     62         mImgAddress = (ImageButton) findViewById(R.id.id_tab_address_img);
     63         mImgSettings = (ImageButton) findViewById(R.id.id_tab_settings_img);
     64 
     65         mFragments = new ArrayList<Fragment>();
     66         Fragment mTab01 = new WeixinFragment();
     67         Fragment mTab02 = new FrdFragment();
     68         Fragment mTab03 = new AddressFragment();
     69         Fragment mTab04 = new SettingFragment();
     70         mFragments.add(mTab01);
     71         mFragments.add(mTab02);
     72         mFragments.add(mTab03);
     73         mFragments.add(mTab04);
     74 
     75         mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
     76 
     77             @Override
     78             public int getCount() {
     79                 return mFragments.size();
     80             }
     81 
     82             @Override
     83             public Fragment getItem(int arg0) {
     84                 return mFragments.get(arg0);
     85             }
     86         };
     87         mViewPager.setAdapter(mAdapter);
     88 
     89         mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
     90 
     91             @Override
     92             public void onPageSelected(int arg0) {
     93                 int currentItem = mViewPager.getCurrentItem();
     94                 setTab(currentItem);
     95             }
     96 
     97             @Override
     98             public void onPageScrolled(int arg0, float arg1, int arg2) {
     99                 // TODO Auto-generated method stub
    100 
    101             }
    102 
    103             @Override
    104             public void onPageScrollStateChanged(int arg0) {
    105                 // TODO Auto-generated method stub
    106 
    107             }
    108         });
    109     }
    110 
    111     @Override
    112     public void onClick(View v) {
    113         switch (v.getId()) {
    114         case R.id.id_tab_weixin:
    115             setSelect(0);
    116             break;
    117         case R.id.id_tab_frd:
    118             setSelect(1);
    119             break;
    120         case R.id.id_tab_address:
    121             setSelect(2);
    122             break;
    123         case R.id.id_tab_settings:
    124             setSelect(3);
    125             break;
    126 
    127         default:
    128             break;
    129         }
    130     }
    131 
    132     private void setSelect(int i) {
    133         setTab(i);
    134         mViewPager.setCurrentItem(i);
    135     }
    136 
    137     private void setTab(int i) {
    138         resetImgs();
    139         switch (i) {
    140         case 0:
    141             mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
    142             break;
    143         case 1:
    144             mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
    145             break;
    146         case 2:
    147             mImgAddress.setImageResource(R.drawable.tab_address_pressed);
    148             break;
    149         case 3:
    150             mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
    151             break;
    152         }
    153     }
    154 
    155     private void resetImgs() {
    156         mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
    157         mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
    158         mImgAddress.setImageResource(R.drawable.tab_address_normal);
    159         mImgSettings.setImageResource(R.drawable.tab_settings_normal);
    160     }
    161 
    162 }
  • 相关阅读:
    vue父子组件传值的方式
    定时任务写法
    仅仅为笔记
    consul剔除某个服务
    mybatis批量查询
    一次eureka的事故
    feign的工作原理
    JVM优化
    threadlocal应用
    秋招总结
  • 原文地址:https://www.cnblogs.com/six-moon/p/4644603.html
Copyright © 2020-2023  润新知