Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自定義底部菜單框架:TabLayout 式的可複用依賴庫實作

編輯:關於 Android 開發

自定義底部菜單框架:TabLayout 式的可複用依賴庫實作

本文記錄如何封裝一個可複用的底部菜單切換框架,日後寫底部導航時直接作為依賴庫引入即可,免去重複勞力。

實作步驟概述

  1. 自定義一個類,繼承 LinearLayout(也可依喜好繼承 RelativeLayout)。
  2. 重寫兩個帶參數的構造方法。
  3. 進入內容編寫,處理布局檔與資料。
  4. 為底部容器添加 View,實現菜單的頁面切換。

框架核心程式碼

package com.weight.tabbottomlib.view;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 底部切換框架
 */
public class MyBottomLayout extends LinearLayout {
    static ArrayList<Item> itemData = new ArrayList<Item>();
    static ArrayList<View> viewArryItem = new ArrayList<View>();
    LayoutInflater inflater; // 布局管理器
    View mainView;           // 底部的容器
    View viewItem;
    int itemSize = 0;

    public MyBottomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 為底部容器添加 view
     */
    public void addBottomLayoutValue(ArrayList<Item> itemData) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        this.itemData = itemData;
        this.itemSize = itemData.size();
        for (int i = 0; i < itemData.size(); i++) {
            View viewItem = inflater.inflate(R.layout.layout_tab_item, null);
            viewItem.findViewById(R.id.tabImg).setBackgroundResource(
                    itemData.get(i).getDrawableNormalId());
            TextView textView = (TextView) viewItem.findViewById(R.id.tabText);
            textView.setText(itemData.get(i).getName());
            textView.setTextColor(Color.WHITE);
            this.addView(viewItem, new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f));
            viewItem.setId(i);
            viewArryItem.add(viewItem);
            viewItem.setOnClickListener(new lister());
        }
        changeDataState(); // 處理整體數據
    }

    /**
     * 改變數據的狀態,不足 5 項時補齊
     */
    private void changeDataState() {
        if (itemSize != 5) {
            for (int i = itemSize; i < 5; i++) {
                itemData.add(new Item());
                View viewItem = LayoutInflater.from(getContext())
                        .inflate(R.layout.layout_tab_item, null);
                viewArryItem.add(viewItem);
            }
        }
    }

    public interface ICallbackLister {
        public void click(int id);
    }

    ICallbackLister callbackLister = null;

    public void setOnCallbackLister(ICallbackLister callbackLister) {
        this.callbackLister = callbackLister;
    }

    // tab 點擊事件處理
    private class lister implements OnClickListener {
        @Override
        public void onClick(View clickView) {
            callbackLister.click(clickView.getId());
            initBottom(clickView.getId());
        }
    }

    public static void initBottom(int id) {
        switch (id) {
            case 0:
                initData(itemData.get(0).getDrawableSelectId(), itemData.get(1).getDrawableNormalId(),
                        itemData.get(2).getDrawableNormalId(), itemData.get(3).getDrawableNormalId(),
                        itemData.get(4).getDrawableNormalId());
                changeTextColor(
                        (TextView) viewArryItem.get(0).findViewById(R.id.tabText),
                        (TextView) viewArryItem.get(1).findViewById(R.id.tabText),
                        (TextView) viewArryItem.get(2).findViewById(R.id.tabText),
                        (TextView) viewArryItem.get(3).findViewById(R.id.tabText),
                        (TextView) viewArryItem.get(4).findViewById(R.id.tabText));
                break;
            // case 1~4 依此類推,切換對應的選中態圖示與文字顏色
        }
    }

    public static void initData(int resIdOne, int resIdTwo, int resIdThree,
                                int resIdFour, int resIdFive) {
        try {
            viewArryItem.get(0).findViewById(R.id.tabImg).setBackgroundResource(resIdOne);
            viewArryItem.get(1).findViewById(R.id.tabImg).setBackgroundResource(resIdTwo);
            viewArryItem.get(2).findViewById(R.id.tabImg).setBackgroundResource(resIdThree);
            viewArryItem.get(3).findViewById(R.id.tabImg).setBackgroundResource(resIdFour);
            viewArryItem.get(4).findViewById(R.id.tabImg).setBackgroundResource(resIdFive);
        } catch (Exception err) {
            err.printStackTrace(); // 說明已無這麼多 item
        }
    }

    public static void changeTextColor(TextView txtOne, TextView txtTwo,
                                       TextView txtThree, TextView txtFour, TextView txtFive) {
        txtOne.setTextColor(Color.MAGENTA); // 選中態
        txtTwo.setTextColor(Color.BLUE);
        txtThree.setTextColor(Color.BLUE);
        txtFour.setTextColor(Color.BLUE);
        txtFive.setTextColor(Color.BLUE);
    }
}

菜單項的布局檔 layout_tab_item

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <ImageView
            android:id="@+id/tabImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" />

        <TextView
            android:id="@+id/tabText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tabImg"
            android:layout_centerHorizontal="true"
            android:textColor="#ffffff"
            android:textSize="16dp" />
    </RelativeLayout>
</RelativeLayout>

實體類 Item

package com.weight.tabbottomlib.view;

public class Item {
    private int drawableNormalId;
    private int drawableSelectId;
    private String name;

    public Item(int drawableNormalId, int drawableSelectId, String name) {
        super();
        this.drawableNormalId = drawableNormalId;
        this.drawableSelectId = drawableSelectId;
        this.name = name;
    }

    public Item() {
        super();
    }
    // getter / setter 省略
}

作為依賴庫使用

要作為庫使用,右鍵你的工程 → Build Path → Configure Build Path → Android,勾選 isLibrary 並點擊 OK 即可。具體使用方法將於後續文章說明。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved