Fragment配合RadioGroup實現點擊切換佈局

来源:http://www.cnblogs.com/liunanjava/archive/2016/08/28/5814873.html
-Advertisement-
Play Games

這裡用了 MainActivity佈局 主要代碼 源碼: https://github.com/ln0491/RadioButtonAndFragment ...


這裡用了

 compile 'com.jakewharton:butterknife:7.0.1'
compile 'org.greenrobot:eventbus:3.0.0'

 

MainActivity佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="liu.radiobuttonandfragment.MainActivity">


    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

    </FrameLayout>

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@mipmap/nav_footer_white"
        android:gravity="center"

        android:orientation="horizontal">


        <RadioGroup
            android:id="@+id/rgTools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"


            android:gravity="center"
            android:orientation="horizontal">


            <RadioButton
                android:id="@+id/rbHome"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:button="@null"
                android:checked="true"
                android:drawableTop="@drawable/selector_main_btn_home"
                android:gravity="center"
                android:paddingLeft="0dp"
                android:text="首頁"/>

            <RadioButton
                android:id="@+id/rbShop"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:checked="false"
                android:drawableTop="@drawable/selector_main_btn_shopcart"
                android:gravity="center"
                android:text="購物車"/>

            <RadioButton
                android:checked="false"
                android:id="@+id/rbMessage"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/selector_main_btn_message"
                android:gravity="center"
                android:text="消息"/>

            <RadioButton
                android:checked="false"
                android:id="@+id/rbMine"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/selector_main_btn_mine"
                android:gravity="center"
                android:text="我的"/>

        </RadioGroup>

    </LinearLayout>


</LinearLayout>

主要代碼

 private void initFragment() {
        //首頁
        HomeFragment homeFragment =new HomeFragment();
        //購物車
        ShopcartFragment shopcartFragment =new ShopcartFragment();

        //消息
        MessageFragment messageFragment =new MessageFragment();
        //個人中心

        MineFragment mineFragment =new MineFragment();

        //添加到數組
        mFragments = new Fragment[]{homeFragment,shopcartFragment,messageFragment,mineFragment};

        //開啟事務

        FragmentTransaction ft =
                getSupportFragmentManager().beginTransaction();

        //添加首頁
        ft.add(R.id.content,homeFragment).commit();

        //預設設置為第0個
        setIndexSelected(0);


    }



    private void setIndexSelected(int index) {

        if(mIndex==index){
            return;
        }
        FragmentManager    fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft              = fragmentManager.beginTransaction();


        //隱藏
        ft.hide(mFragments[mIndex]);
        //判斷是否添加
        if(!mFragments[index].isAdded()){
            ft.add(R.id.content,mFragments[index]).show(mFragments[index]);
        }else {
            ft.show(mFragments[index]);
        }

        ft.commit();
        //再次賦值
        mIndex=index;

    }

    @OnClick({R.id.rbHome, R.id.rbShop, R.id.rbMessage, R.id.rbMine})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rbHome:
                setIndexSelected(0);
                break;
            case R.id.rbShop:
                setIndexSelected(1);
                break;
            case R.id.rbMessage:
                setIndexSelected(2);
                break;
            case R.id.rbMine:
                setIndexSelected(3);
                break;
        }

    }

 

源碼:

https://github.com/ln0491/RadioButtonAndFragment

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 除了使用new操作符調用構造函數以外,我們也可以拋開new操作符,只用一般函數來創建對象,這樣就能執行某些預備工作,並已對象為返回值的函數。、 然後我們調用her()來生成對象: 實際上構造函數也是可以返回對象的,只不過this的值使用上將會有所不同。 在這裡,構造其函數不是返回包含屬性name的t ...
  • 我們可以通過構造器函數(簡稱構造函數)來創建對象: 為了使用該函數來創建對象,我們需要使用new操作符,例如: 使用構造函數創建對象的好處就是她可以接受一些參數,下麵我們就來修改一下上面的例子: 現在,我們就能利用同一個構造函數創建不同的對象了: 按照慣例,我們應該將構造函數的首字母大寫,以便區分一 ...
  • 版權聲明:作者原創,轉載請註明出處! 編寫插件的兩種方式: 1.類級別開發插件(1%) 2.對象級別開發(99%) 類級別的靜態開發就是給jquery添加靜態方法,三種方式 1.添加新的全局函數 2.使用$.extend(obj) 3.使用命名空間 類級別開發插件(用的非常少,1%) 分別舉例: 調 ...
  • 或者 ...
  • 本篇主要探討的是iOS應用中各種狀態的躍遷過程,建議大家通過修改AppDelegate.swift,在每個過程中添加日誌輸出代碼,從而觀察其變化。 ...
  • 一、PackageManager的resolveActivity 返回能夠匹配的Intent,如果不能匹配就返回NULL, 二、PackageManager的queryIntentActivities 返回所有成功匹配的Activity信息 三、Intent的resolveActivity 第一個參 ...
  • Android 事件分發機制 ViewGroup dispatchTouchEvent 返回true ViewGroup自己在dispatchTouchEvent中消費這個事件 dispatchTouchEvent 返回false 回傳到Activity中onTouchEvent消費事件 dispa ...
  • 我們在《Android學習之Handler消息傳遞機制》(http://www.cnblogs.com/zhouhb/p/5812447.html)已提到過,Android只允許UI線程修改Activity里的UI組件,這樣會導致新啟動的線程無法動態改變界面組件的屬性值。為瞭解決新線程不能更新UI組 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...