• 67、Fragment实现Tab


    <LinearLayout 
      。。。。。。。。。。
        <FrameLayout
            android:id="@+id/id_content"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
        <include layout="@layout/bottom" />
    </LinearLayout>
      1 package com.imooc.tab02;
      2 
      3 import android.os.Bundle;
      4 import android.support.v4.app.Fragment;
      5 import android.support.v4.app.FragmentActivity;
      6 import android.support.v4.app.FragmentManager;
      7 import android.support.v4.app.FragmentTransaction;
      8 import android.view.View;
      9 import android.view.View.OnClickListener;
     10 import android.view.Window;
     11 import android.widget.ImageButton;
     12 import android.widget.LinearLayout;
     13 
     14 public class MainActivity extends FragmentActivity implements OnClickListener {
     15     private LinearLayout mTabWeixin;
     16     private LinearLayout mTabFrd;
     17     private LinearLayout mTabAddress;
     18     private LinearLayout mTabSettings;
     19 
     20     private ImageButton mImgWeixin;
     21     private ImageButton mImgFrd;
     22     private ImageButton mImgAddress;
     23     private ImageButton mImgSettings;
     24 
     25     private Fragment mTab01;
     26     private Fragment mTab02;
     27     private Fragment mTab03;
     28     private Fragment mTab04;
     29 
     30     @Override
     31     protected void onCreate(Bundle savedInstanceState) {
     32         super.onCreate(savedInstanceState);
     33 
     34         requestWindowFeature(Window.FEATURE_NO_TITLE);
     35         setContentView(R.layout.activity_main);
     36 
     37         initView();
     38         initEvent();
     39         setSelect(0);
     40     }
     41 
     42     private void initEvent() {
     43         mTabWeixin.setOnClickListener(this);
     44         mTabFrd.setOnClickListener(this);
     45         mTabAddress.setOnClickListener(this);
     46         mTabSettings.setOnClickListener(this);
     47     }
     48 
     49     private void initView() {
     50         mTabWeixin = (LinearLayout) findViewById(R.id.id_tab_weixin);
     51         mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
     52         mTabAddress = (LinearLayout) findViewById(R.id.id_tab_address);
     53         mTabSettings = (LinearLayout) findViewById(R.id.id_tab_settings);
     54 
     55         mImgWeixin = (ImageButton) findViewById(R.id.id_tab_weixin_img);
     56         mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
     57         mImgAddress = (ImageButton) findViewById(R.id.id_tab_address_img);
     58         mImgSettings = (ImageButton) findViewById(R.id.id_tab_settings_img);
     59     }
     60 
     61     private void setSelect(int i) {
     62         FragmentManager fm = getSupportFragmentManager();
     63         FragmentTransaction transaction = fm.beginTransaction();
     64         hideFragment(transaction);
     65         // 把图片设置为亮的
     66         // 设置内容区域
     67         switch (i) {
     68         case 0:
     69             if (mTab01 == null) {
     70                 mTab01 = new WeixinFragment();
     71                 transaction.add(R.id.id_content, mTab01);
     72             } else {
     73                 transaction.show(mTab01);
     74             }
     75             mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
     76             break;
     77         case 1:
     78             if (mTab02 == null) {
     79                 mTab02 = new FrdFragment();
     80                 transaction.add(R.id.id_content, mTab02);
     81             } else {
     82                 transaction.show(mTab02);
     83             }
     84             mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
     85             break;
     86         case 2:
     87             if (mTab03 == null) {
     88                 mTab03 = new AddressFragment();
     89                 transaction.add(R.id.id_content, mTab03);
     90             } else {
     91                 transaction.show(mTab03);
     92             }
     93             mImgAddress.setImageResource(R.drawable.tab_address_pressed);
     94             break;
     95         case 3:
     96             if (mTab04 == null) {
     97                 mTab04 = new SettingFragment();
     98                 transaction.add(R.id.id_content, mTab04);
     99             } else {
    100                 transaction.show(mTab04);
    101             }
    102             mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
    103             break;
    104 
    105         default:
    106             break;
    107         }
    108 
    109         transaction.commit();
    110     }
    111 
    112     private void hideFragment(FragmentTransaction transaction) {
    113         if (mTab01 != null) {
    114             transaction.hide(mTab01);
    115         }
    116         if (mTab02 != null) {
    117             transaction.hide(mTab02);
    118         }
    119         if (mTab03 != null) {
    120             transaction.hide(mTab03);
    121         }
    122         if (mTab04 != null) {
    123             transaction.hide(mTab04);
    124         }
    125     }
    126 
    127     @Override
    128     public void onClick(View v) {
    129         resetImgs();   // 切换图片至暗色
    130         switch (v.getId()) {
    131         case R.id.id_tab_weixin:
    132             setSelect(0);
    133             break;
    134         case R.id.id_tab_frd:
    135             setSelect(1);
    136             break;
    137         case R.id.id_tab_address:
    138             setSelect(2);
    139             break;
    140         case R.id.id_tab_settings:
    141             setSelect(3);
    142             break;
    143 
    144         default:
    145             break;
    146         }
    147     }
    148 
    149     /**
    150      * 切换图片至暗色
    151      */
    152     private void resetImgs() {
    153         mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
    154         mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
    155         mImgAddress.setImageResource(R.drawable.tab_address_normal);
    156         mImgSettings.setImageResource(R.drawable.tab_settings_normal);
    157     }
    158 
    159 }

    DEMO下载地址:http://download.csdn.net/detail/androidsj/9354303

  • 相关阅读:
    python生成试题库和界面 (python generate test database and layout)
    python生成数据库(python generate database)
    Go语言基础之流程控制
    Go语言基础之运算符
    Go语言基础之变量和常量
    VS Code配置Go语言开发环境
    Linux安装教程|详细
    安装Go语言及搭建Go语言开发环境
    Go语言
    Django2.0路由匹配path的用法
  • 原文地址:https://www.cnblogs.com/androidsj/p/5045387.html
Copyright © 2020-2023  润新知