1. 程式人生 > >Android肝帝戰紀之基於上篇單Activity+多Fragment框架,開發電商式導航欄,多Fragment切換

Android肝帝戰紀之基於上篇單Activity+多Fragment框架,開發電商式導航欄,多Fragment切換

電商式導航欄,多Fragment切換

  • 介面構思示意圖
    這裡寫圖片描述

  • 設計思路
    在底部的LinearLayout中新增相應的圖示,然後設定tag繫結相應的 Fragment陣列對應的下標值,再點選切換的時候,根據獲取到的tag值,取出陣列中對應下標的Fragment,在通過相關方法顯示。
    封裝目標:只需新增相應的按鈕物件和相應的檢視即可,然後在Activity中顯示即可。

  • 先建立可切換的Fragment的基類BottomItemFragment(實現一個雙擊退出功能)

// 定義為抽象類即每個itemfragment的基類
public abstract class BottomItemFragment
extends MyFragment implements View.OnKeyListener{
// 當前按下返回按鈕的時間 private long mExitTime = 0; // 雙擊返回鍵之間的延遲 private static final int EXIT_TIME = 2000; @Override public void onResume(){ super.onResume(); View rootView = getView(); // 防止雙擊退出失效 if (rootView!=null
){ rootView.setFocusableInTouchMode(true); rootView.requestFocus(); rootView.setOnKeyListener(this); } } @Override public boolean onKey(View v, int keyCode, KeyEvent event){ if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){ if
((System.currentTimeMillis() - mExitTime) > EXIT_TIME) { Toast.makeText(getContext(), "雙擊退出", Toast.LENGTH_SHORT).show(); mExitTime = System.currentTimeMillis(); } else { _mActivity.finish(); if (mExitTime != 0) { mExitTime = 0; } } return true; } } }
  • 底部導航欄按鈕物件的封裝(這邊的icon用的是字型圖示,不明白的請移步我的部落格)
public final class BottomTabBean {
    private final CharSequence ICON;
    private final CharSequence TITLE;

    public BottomTabBean(CharSequence ICON, CharSequence TITLE){
        this.ICON = ICON;
        this.TITLE = TITLE;
    }

    public CharSequence getICON(){
        return ICON;
    }
    public CharSequence getTITLE(){
        return TITLE;
    }
}
  • 新建一個ItemBuilder,目的是用來把每一個Fragment和底部的按鈕物件進行繫結組裝進一個LinkedHashMap的集合
public final class ItemBuilder{
    private final LinkedHashMap<BottomTabBean,BottomItemDelegate> ITEMS = new LinkedHashMap<>();

    static ItemBuilder builder(){
        return new ItemBuilder();
    }

    public final ItemBuilder addItem(BottomTabBean bean, BottomItemDelegate delegate){
        ITEMS.put(bean, delegate);
        return this;
    }

    public final ItemBuilder addItems(LinkedHashMap<BottomTabBean, BottomItemDelegate> items){
        ITEMS.putAll(items);
        return this;
    }

    public final LinkedHashMap<BottomTabBean, BottomItemDelegate> build(){
        return ITEMS;
    }
}
  • 接下來貼兩個佈局檔案,一個是根Fragment的佈局,其實就是一個FrameLayout加上一個底部按鈕的LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.ContentFrameLayout
        android:id="@+id/bottom_bar_delegate_container"
        android:layout_above="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.v7.widget.LinearLayoutCompat
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_height="60dp"/>

</RelativeLayout>
  • 然後是底部按鈕的item佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:paddingBottom="6dp"
    android:paddingTop="6dp">

    <com.joanzapata.iconify.widget.IconTextView
        android:id="@+id/icon_bottom_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:textSize="25sp" />

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/tv_bottom_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center" />

</RelativeLayout>
  • 萬事俱備,接下來就是新建一個BaseBottomFragment,將頁面進行組裝
public abstract class BaseBottomFragment extends MyFragment implements View.OnClickListener {
    // 將傳過來的LinkedHashMap集合裡面的鍵值對拆分之後,分別存入兩個ArrayList
    private final ArrayList<BottomTabBean> TAB_BEAN = new ArrayList<>();
    private final ArrayList<BottomItemFragment> ITEM_FRAGMENT = new ArrayList<>();

    // 定義一個LinkedHashMap接受傳過來的頁面資料
    private final LinkedHashMap<BottomTabBean, BottomItemFragment> ITEMS = new LinkedHashMap<>();

    // 當前顯示fragment的頁面標記
    private int mCurrentFragment = 0;
    // 首次載入頁面時顯示的主頁標記 = 0
    private int mIndexFragment = 0;

    // 點選之後按鈕顯示的顏色
    private int mClickColor = Color.RED;

    //對子類提供的設定的抽象方法
    // 讓子類傳入佈局所需要的按鈕和佈局
    public abstract LinkedHashMap<BottomTabBean, BottomItemDelegate> setItems(ItemBuilder itemBuilder);
    // 讓子類傳入設定首次載入的主頁
    public abstract int setIndexDelegate();
    // 讓子類傳入設定點選之後按鈕的顏色
    @ColorInt
    public abstract int setClickColor();

    @Override
    public Object setLayout() {
        return R.layout.delegate_bottom;
    }
    // ButterKnife繫結,自己去Github上看使用的方法,我這邊是在Android Library裡面使用的,所以是R2檔案
    @BindView(R2.id.bottom_bar)
    LinearLayoutCompat mBottomBar = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIndexDelegate = setIndexDelegate();
        if (setClickColor()!=0){
            mClickColor = setClickColor();
        }

        final ItemBuilder builder = ItemBuilder.builder();
        final LinkedHashMap<BottomTabBean, BottomItemDelegate> items = setItems(builder);

        ITEMS.putAll(items);
        // for 迴圈取出ITEMS中的鍵值對的值
        for(Map.Entry<BottomTabBean, BottomItemDelegate> item:ITEMS.entrySet()){
            final BottomTabBean key = item.getKey();
            final BottomItemDelegate value = item.getValue();
            TAB_BEANS.add(key);
            ITEM_DELEGATES.add(value);
        }
    }

    @Override
    public void onBindView(@Nullable Bundle saveInstanceState, View rootView){
        final size = ITEMS.size();
        // for迴圈填充底部佈局,比較核心的內容
        for(int i = 0 ;i < size; i++){
            LayoutInflater.from(getContext()).inflate(R.layout.bottom_item_icon_text_layout, mBottomBar);
            final RelativeLayout item = mBottomBar.getChildAt(i);
            // 設定tag
            item.setTag(i);
            item.setOnClickListener(this);
            final IconTextView itemIcon = (IconTextView)item.getChildAt(0);
            final AppCompatTextView itemTitle = (AppCompatTextView)item.getChildAt(1);
            final BottomTabBean bean = TAB_BEAN.get(i);
            itemIcon.setText(bean.getICON());
            itemTitle.setText(bean.getTITLE());
            if (i == mIndexFragment){
                itemIcon.setTextColor(mClickColor);
                itemTitle.setTextColor(mClickColor);
            }
        }
        // 將fragments的ArrayList轉化為SupportFragment的陣列,框架需要
        final SupportFragment[] fragmentArray = ITEM_FRAGMENTS.toArray(new SupportFragment[size]);
        // Fragmentation提供的設定方法
        loadMultipleRootFragment(R.id.bottom_bar_delegate_container, mIndexFragment, fragmentArray);
    }

    // 重置按鈕的顏色
    private void resetColor() {
        final int count = mBottomBar.getChildCount();
        for (int i = 0; i < count; i++) {
            final RelativeLayout item = (RelativeLayout) mBottomBar.getChildAt(i);
            final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
            itemIcon.setTextColor(Color.GRAY);
            final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);
            itemTitle.setTextColor(Color.GRAY);
        }
    }

    // 點選之後重新匯入delegate,設定按鈕的顏色
    @Override
    public void onClick(View view) {
        final int tag = (int) view.getTag();
        resetColor();
        final RelativeLayout item = (RelativeLayout) view;
        final IconTextView itemIcon = (IconTextView) item.getChildAt(0);
        itemIcon.setTextColor(mClickColor);
        final AppCompatTextView itemTitle = (AppCompatTextView) item.getChildAt(1);
        itemTitle.setTextColor(mClickColor);
        // Fragmentation提供的方法
        showHideFragment(ITEM_DELEGATES.get(tag), ITEM_DELEGATES.get(mCurrentDelegate));
        mCurrentDelegate = tag;
    }
}
  • 至此,封裝完成,底部帶導航欄的多Fragment介面已經ok了,接下來就是新建一個底部導航欄的例項,將資料填充進去就OK了
public class MyBottomFragment extends BaseBottomFragment{
    @Override
    public LinkedHashMap<BottomTabBean, BottomItemFragment> setItems(ItemBuilder itemBuilder) {
        final LinkedHashMap<BottomTabBean, BottomItemFragment> items = new LinkedHashMap<>();
        items.put(new BottomTabBean("{fa-home}","主頁"),new IndexFragment());
        items.put(new BottomTabBean("{fa-sort}","分類"),new SortFragment());
        items.put(new BottomTabBean("{fa-compass}","發現"),new IndexFragment());
        items.put(new BottomTabBean("{fa-shopping-cart}","購物車"),new IndexFragment());
        items.put(new BottomTabBean("{fa-user}","我的"),new IndexFragment());

        return itemBuilder.addItems(items).build();
    }

    @Override
    public int setIndexDelegate() {
        return 0;
    }

    @Override
    public int setClickColor() {
        return Color.parseColor("#FF0000");
    }
}
  • 最後在單一的Activity中的setRootFragment方法中設定MyBottomFragment頁面,顯示即可。

相關推薦

Android基於Activity+Fragment框架開發導航Fragment切換

電商式導航欄,多Fragment切換 介面構思示意圖 設計思路 在底部的LinearLayout中新增相應的圖示,然後設定tag繫結相應的 Fragment陣列對應的下標值,再點選切換的時候,根據獲取到的tag值,取出陣列中對應下標的Fragment,在

Android網路請求框架封裝(Retrofit的封裝)

網路請求框架封裝(OkHttp3+Retrofit+loading的封裝) Retrofit的Github連結 點此連結到Github AVLoadingIndicatorView的Github連結(封裝到請求框架中,請求過程中的loading樣式框(

AndroidObjectBox移動資料庫框架探究與實現

ObjectBox移動資料庫框架 介紹 ObjectBox是移動端資料庫框架,靈感來自於NoSql,速度非常快,號稱是市面上最快的移動端資料庫框架。 為什麼要使用 快 簡單,面向物件的API 新增依賴 專案級別的Gradle中新增:

Android字型圖示庫(iconify)的簡單使用

字型圖示庫(iconify)的簡單使用 本文介紹字型圖示庫簡單的封裝和使用(以整合Fontawesome為例): Iconify的GIithub連結 點此連結到Github 匯入依賴 /

spring源碼事務

sql ram 知識 ref sdn 全部 鏈接 帳戶 pri 前言 本篇之前,已經寫過關於AOP的源碼分析,在此基礎上來閱讀事務的源碼是比較清晰的。另外,需要對數據庫有一定的了解,我們所說是事務一般指的都是狹義的事務,即數據庫事務。文中沒有特別指出的數據庫,均指的是m

Python3 與 C# 並發編程

動態 ken summary using 任務調度 影響 特征 可能 arp NetCore並發編程 示例代碼:https://github.com/lotapp/BaseCode/tree/master/netcore/4_Concurrency 先簡單說下概念(其實之前

Mybatis學習總結四基於註解的表CRUD操作

先認識一下註解概念: 定義:註解(Annotation),也叫元資料。一種程式碼級別的說明。它是JDK1.5及以後版本引入的一個特性,與類、介面、列舉是在同一個層次。它可以宣告在包、類、欄位、方法、區域性變數、方法引數等的前面,用來對這些元素進行說明,註釋。 作用分類: ①編寫文件:通過

Mybatis學習總結二基於 XML 的表CRUD操作

上一篇我們講了MyBatis配置檔案中的配置及其優化:https://blog.csdn.net/qq_38720976/article/details/84484034 本文將通過專案使用MyBatis對錶執行CRUD操作  mybatis-3.3.0.jar下載路徑:ht

網易雲捕效能踩坑解決

本文由作者餘寶虹授權網易雲社群釋出。 從零開始設計開發一個日處理資料8億的大資料高併發實時系統,哪些效能問題需要特別注意?這裡我們一起梳理一下,本文中我將以PE,SA同學戲稱的DDOS系統—網易雲捕設計開發實踐中兩年的時間裡碰到的真實問題,踩過的坑及解決問題的方法和大家一起討論如何解決這些問題。文中不會大談

Android入門基礎知識總結Layout佈局

臨近考試,做一下簡單的總結,鞏固一下; 不能面面俱到,舉的都是一些常用的基礎例子; 下一篇:Android入門基礎知識總結之常用控制元件篇 一、Layout佈局篇 通用屬性還請自行體會,這裡只說了部分特有或常用屬性; 1) LinearLayout(線性佈局

Android專案導航仿微信底部導航TabLayout+ViewPager+Fragment

一、實現效果: 二、依賴jar包: compile 'com.android.support:design:24+'三、專案工程結構: 四、XML佈局 activity_main.xml佈局: <?xml version="1.0" encoding="u

APICLOUD實現沉浸導航Android和ios的相容

最近在使用apicloud開發一款APP,專案沒有使用安卓和ios開發人員,選擇APICLOUD開發,猶豫UI設計的導航欄是沉浸式,在Android和ios上的效果是不一樣的,ios上導航欄會浮在內容上面,將內容部分遮擋,如圖: 我要達到的是不希望導航欄遮擋住內容部分,經過不懈的

android獲取imsi碼判斷運營資訊(移動聯通電信)

專案中要用到這個知識點,於是有整理這個知識點,並寫成博文,以作備忘。 一.imsi碼概念的理解 1.概念 IMSI:國際移動使用者識別碼(唯一標識),IMSI = MCC + MNC + MSIN,其中MCC是指移動臺國家程式碼(3 位,中國460),MNC是指移動網程式碼

Struts2檔案傳(檔案/檔案)

<一>簡述: Struts2的檔案上傳其實也是通過攔截器來實現的,只是該攔截器定義為預設攔截器了,所以不用自己去手工配置,<interceptor name="fileUpload" class="org.apache.struts2.intercepto

Lenovo k860i 移植Android 4.4 cm11進度記錄【已完結】

2014.5.16 為了驗證一下下載的CM11的原始碼有沒有問題,決定編譯一下cm官方支援的機器,手上正好有臺nexus7 2012,就拿它為例測試一下在mac os x平臺的整個編譯過程。 1. 最開始,是在os x下面搭編譯環境,以前有說過的,詳見xda這個帖子。 http://forum.xda-dev

Android 動態隱藏顯示導航狀態

sets tab mil repeat art gif ava hide sticky Talk is cheap, show me the code.--Linus TorvaldsOkay, here:一、導航欄:[java] view plain copyprivat

基於LVD、貝葉斯模型演算法實現的行業商品評論與情感分析案例

一、 專案需求 現在大家進行網購,在購物之前呢,肯定會看下相關商品的評論,看下好評和差評,然後再綜合衡量,最後才會決定是否購買相關的商品。對一個指定商品,生產商,賣家,買家認同該商品的哪些優點/不認同

ionic-基於angularjs實現沉浸頂部導航滾動時產生漸變效果

最近一直在研究angularjs和ionic框架,對於如何設計好的產品和使用者體驗一直都在思考,看了很多關於app設計的資料和其他的產品,覺得基於material design的設計確實是目前比較好的

【Qt 學習路---安裝】QT5.7.1+VS2013軟體開發環境配置

參考:https://blog.csdn.net/liushuiwen101423/article/details/70882534 安裝任務:完成Qt5.7.1載入到VS2013環境下,程式設計執行Qt應用程式,有詳細步驟,最後完成一個空白視窗UI執行顯示1.基本配置PC

loushang框架開發中關於BSP的使用將寫好的功能模組部署到主頁介面結構

  前言:   當我們已經開發好相應的模組或者功能的時候,需要將這個功能部署在index主頁上作為可點選直接使用的模組,而不是每次需要去瀏覽對應的url地址。   這時候就需要運用到L5的BSP。   作為剛剛入門loushang5的初學者自己做了個多表聯合查詢的