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

手機影音實戰(二):自定義標題欄與 ViewPager 懶加載優化

編輯:關於 Android 開發

手機影音實戰(二):自定義標題欄與頁面懶加載

主頁面的第一步是搭骨架。這一節我們實現自定義標題欄,並解決 ViewPager 各子頁面重複初始化數據的性能問題。

一、標題欄布局

由四個部分組成:Logo、搜索入口、遊戲入口(帶紅點)、播放歷史。

<?xml version="1.0" encoding="utf-8"?>
<com.atguigu.mobileplayer2.view.TitleBar
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:background="#ff3097fd"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:src="@drawable/ic_topbanner_logo" />

    <TextView
        android:id="@+id/tv_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_weight="1"
        android:background="@drawable/tv_search_bg_selector"
        android:clickable="true"
        android:drawableLeft="@drawable/tv_search_drawable_selector"
        android:drawablePadding="3dp"
        android:text="全網搜索"
        android:textColor="@drawable/tv_search_textcolor_selector"
        android:textSize="14sp" />

    <RelativeLayout
        android:id="@+id/rl_game"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp">

        <TextView
            android:id="@+id/tv_game"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_topbanner_game" />

        <ImageView
            android:layout_width="6dp"
            android:layout_height="6dp"
            android:layout_alignRight="@id/tv_game"
            android:background="@drawable/dot" />

    </RelativeLayout>

    <ImageView
        android:id="@+id/iv_record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_topbanner_record" />

</com.atguigu.mobileplayer2.view.TitleBar>

二、selector 與 shape 的配合

標題欄的視覺效果全靠這幾個資源文件撐起來,值得完整梳理一遍。

圖片按壓切換:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="false"
          android:drawable="@drawable/ic_sousuokuang" />
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_sousuokuang_press" />
</selector>

文字顏色切換:注意這裡用的是 android:color 而非 drawable。

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:color="#99ffffff" />
    <item android:state_pressed="true"  android:color="#ffffffff" />
</selector>

搜索框背景切換:

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_pressed="false"
          android:drawable="@drawable/search_bg_normal" />
    <item android:state_pressed="true"
          android:drawable="@drawable/search_bg_press" />
</selector>

圓角矩形背景:通過 shape 標簽實現,正常狀態用不透明色,按壓狀態降低透明度。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="12.0dp" />
    <padding
        android:bottom="5dp"
        android:left="8dp"
        android:right="8dp"
        android:top="5dp" />
    <solid android:color="#ff2679ca" />
</shape>

按壓版本僅把顏色改成 #cc2679ca,透明度變化帶來了反饋感。

橢圓形紅點:

<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#ffff0000" />
</shape>

三、封裝自定義標題欄

把點擊邏輯收攏到自定義 View 內部,Activity 就再也不需要寫一堆 findViewById。

public class TitleBar extends LinearLayout implements View.OnClickListener {

    private View tv_search;
    private View rl_game;
    private View iv_record;
    private Context context;

    /** 代碼中實例化時使用 */
    public TitleBar(Context context) {
        this(context, null);
    }

    /**
     * 布局文件中使用時,系統通過這個構造方法實例化。
     * 注意:寫在 XML 裡的自定義 View 必須提供此構造方法
     */
    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /** 需要指定樣式時使用 */
    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    /** 布局加載完成後回調,此時子 View 已經準備好 */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        tv_search = getChildAt(1);
        rl_game = getChildAt(2);
        iv_record = getChildAt(3);

        tv_search.setOnClickListener(this);
        rl_game.setOnClickListener(this);
        iv_record.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_search:
                Intent intent = new Intent(context, SearchActivity.class);
                context.startActivity(intent);
                break;
            case R.id.rl_game:
                Toast.makeText(context, "遊戲", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_record:
                Toast.makeText(context, "播放歷史", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

兩個細節值得留意。第一,onFinishInflate 是獲取子 View 的正確時機,在構造方法裡調用 getChildAt 會拿到空值。第二,用 getChildAt 按下標取子 View 雖然省事,但布局一旦調整順序就會出錯,改為 findViewById 會更健壯。

四、屏蔽子頁面重複初始化數據

ViewPager 默認會預加載相鄰頁面,用戶每次滑回去時如果都重新請求數據,既浪費流量又造成閃爍。解法是在 BasePager 中加一個標志位。

public abstract class BasePager {

    /** 判斷是否已經初始化過數據 */
    public boolean isInit;

    // 其他抽象方法省略
}

主頁面獲取具體頁面實例時做判斷:

/** 得到具體某個子頁面的實例視圖 */
private BasePager getBasePager() {
    BasePager basePager = basePagers.get(position);

    if (basePager != null && !basePager.isInit) {
        basePager.initData();
        basePager.isInit = true;
    }

    return basePager;
}

這個思路也可以擴展到懶加載場景:把判斷條件從 isInit 換成「頁面是否對用戶可見」,就能實現真正的按需加載——只有用戶滑到這一頁才發起請求,而不是預先全部拉一遍。

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