zoukankan      html  css  js  c++  java
  • Fragment实现底部Tab,切换可保存状态

    activity_main.xml

     1 <RelativeLayout 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 
     6     <FrameLayout
     7         android:id="@+id/mHomeContainer"
     8         android:layout_width="match_parent"
     9         android:layout_height="match_parent"
    10         android:layout_above="@+id/mRadioGroup" >
    11     </FrameLayout>
    12 
    13     <RadioGroup
    14         android:id="@+id/mRadioGroup"
    15         android:layout_width="match_parent"
    16         android:layout_height="56dp"
    17         android:layout_alignParentBottom="true"
    18         android:background="@drawable/main_bottom_bg"
    19         android:orientation="horizontal" >
    20 
    21         <ImageView
    22             android:id="@+id/Tab_Conversation"
    23             android:layout_width="wrap_content"
    24             android:layout_height="wrap_content"
    25             android:layout_weight="1"
    26             android:contentDescription="@null"
    27             android:src="@drawable/skin_tab_icon_conversation_selected" />
    28 
    29         <ImageView
    30             android:id="@+id/Tab_Contact"
    31             android:layout_width="wrap_content"
    32             android:layout_height="wrap_content"
    33             android:layout_weight="1"
    34             android:contentDescription="@null"
    35             android:src="@drawable/skin_tab_icon_contact_normal" />
    36 
    37         <ImageView
    38             android:id="@+id/Tab_Plugin"
    39             android:layout_width="wrap_content"
    40             android:layout_height="wrap_content"
    41             android:layout_weight="1"
    42             android:contentDescription="@null"
    43             android:src="@drawable/skin_tab_icon_plugin_normal" />
    44     </RadioGroup>
    45 
    46 </RelativeLayout>

     关键代码:

      1 import com.lidroid.xutils.ViewUtils;
      2 import com.lidroid.xutils.view.annotation.ContentView;
      3 import com.lidroid.xutils.view.annotation.ViewInject;
      4 import com.lidroid.xutils.view.annotation.event.OnClick;
      5 import android.support.v4.app.Fragment;
      6 import android.support.v4.app.FragmentTransaction;
      7 import android.support.v7.app.ActionBarActivity;
      8 import android.view.View;
      9 import android.view.Window;
     10 import android.widget.ImageView;
     11 import android.content.Context;
     12 import android.os.Bundle;
     13 
     14 @ContentView(R.layout.activity_main)
     15 public class MainActivity extends ActionBarActivity {
     16 
     17     @ViewInject(R.id.Tab_Conversation)
     18     private ImageView Tab_Conversation;
     19     @ViewInject(R.id.Tab_Contact)
     20     private ImageView Tab_Contact;
     21     @ViewInject(R.id.Tab_Plugin)
     22     private ImageView Tab_Plugin;
     23 
     24     private FragmentConversation mFragmentConversation;
     25     private FragmentContact mFragmentContact;
     26     private FragmentPlugin mFragmentPlugin;
     27     private Context mContext;
     28 
     29     @Override
     30     protected void onCreate(Bundle savedInstanceState) {
     31         super.onCreate(savedInstanceState);
     32         requestWindowFeature(Window.FEATURE_NO_TITLE);
     33         ViewUtils.inject(this);
     34         mContext = this;
     35         InitFragment();
     36         getSupportFragmentManager().beginTransaction()
     37                 .add(R.id.mHomeContainer, mFragmentConversation).commit();
     38     }
     39 
     40     private void InitFragment() {
     41         mFragmentConversation = new FragmentConversation();
     42         mFragmentContact = new FragmentContact();
     43         mFragmentPlugin = new FragmentPlugin();
     44     }
     45 
     46     /**
     47      * 隐藏其他所有Fragment
     48      */
     49     private void HideOtherFragments(Fragment fragment) {
     50         if (getSupportFragmentManager().getFragments() == null) {
     51             return;
     52         }
     53         for (Fragment fm : getSupportFragmentManager().getFragments()) {
     54             if (fm != fragment) {
     55                 getSupportFragmentManager().beginTransaction().hide(fm)
     56                         .commit();
     57             }
     58         }
     59     }
     60 
     61     /**
     62      * Tab切换
     63      * 
     64      * @param view
     65      */
     66     @OnClick({ R.id.Tab_Conversation, R.id.Tab_Contact, R.id.Tab_Plugin })
     67     private void OnTabSelected(View view) {
     68         ClearSelection();
     69         FragmentTransaction mTransaction = getSupportFragmentManager()
     70                 .beginTransaction();
     71         switch (view.getId()) {
     72         case R.id.Tab_Conversation:
     73             Tab_Conversation.setImageDrawable(getResources().getDrawable(
     74                     R.drawable.skin_tab_icon_conversation_selected));
     75             if (!getSupportFragmentManager().getFragments().contains(
     76                     mFragmentConversation)) {
     77                 mTransaction.add(R.id.mHomeContainer, mFragmentConversation);
     78             }
     79             mTransaction.show(mFragmentConversation);
     80             HideOtherFragments(mFragmentConversation);
     81             break;
     82         case R.id.Tab_Contact:
     83             Tab_Contact.setImageDrawable(getResources().getDrawable(
     84                     R.drawable.skin_tab_icon_contact_selected));
     85             if (!getSupportFragmentManager().getFragments().contains(
     86                     mFragmentContact)) {
     87                 mTransaction.add(R.id.mHomeContainer, mFragmentContact);
     88             }
     89             mTransaction.show(mFragmentContact);
     90             HideOtherFragments(mFragmentContact);
     91             break;
     92         case R.id.Tab_Plugin:
     93             Tab_Plugin.setImageDrawable(getResources().getDrawable(
     94                     R.drawable.skin_tab_icon_plugin_selected));
     95             if (!getSupportFragmentManager().getFragments().contains(
     96                     mFragmentPlugin)) {
     97                 mTransaction.add(R.id.mHomeContainer, mFragmentPlugin);
     98             }
     99             mTransaction.show(mFragmentPlugin);
    100             HideOtherFragments(mFragmentPlugin);
    101             break;
    102         default:
    103             break;
    104         }
    105         mTransaction.commit();
    106     }
    107 
    108     /**
    109      * 清除所有选中状态
    110      */
    111     private void ClearSelection() {
    112         Tab_Conversation.setImageDrawable(getResources().getDrawable(
    113                 R.drawable.skin_tab_icon_conversation_normal));
    114         Tab_Contact.setImageDrawable(getResources().getDrawable(
    115                 R.drawable.skin_tab_icon_contact_normal));
    116         Tab_Plugin.setImageDrawable(getResources().getDrawable(
    117                 R.drawable.skin_tab_icon_plugin_normal));
    118     }
    119 
    120 }
  • 相关阅读:
    藕益大师示念佛法门
    【丁嘉丽老师】最新心得报告——生死无常,好好念佛(视频+文字)
    【视频+文字】刘素云老师:法雨惠群生--老实念佛不拐弯 今生一定到彼岸(附净空老法师点评)
    刘素云老师复讲《无量寿经》第16集
    《认识佛教》有声书-02-净空法师-初学佛课程(音)
    《认识佛教》有声书-01-净空法师-初学佛课程(音)
    【强烈推荐】科注学习班——自了法师分享:珍惜暇满人身宝(上)
    自了法师全集 | 无量寿经科注第四回学习班分享三十篇
    《2014净土大经科注》大汇总
    释自了法师:生死心不切,若真为生死之心不发,一切开示皆为戏论
  • 原文地址:https://www.cnblogs.com/lavalike/p/4721273.html
Copyright © 2011-2022 走看看