Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

自訂組合 View 的陷阱(I):Press 效果與點擊事件為何不能兼得

編輯:Android 開發實例

本文探討組合 View(Layout)使用不當時出現的怪異現象,下一篇再分析成因與解法。

先看一個典型的組合 View

開發 Android App 時經常需要「一個圖示 + 兩行文字」這種組合控制項,用寫法如下:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:id="@+id/ll1"
    android:background="@android:drawable/btn_default"
    android:focusable="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:src="@drawable/feedicon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:text="Line one"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <TextView android:text="Line two"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

要掛點擊事件時,程式碼是:

LinearLayout ll1 = (LinearLayout) findViewById(R.id.ll1);
ll1.setOnClickListener(listener);

示例一:直接寫在版面配置中

在一個頁面裡放多個這種組合 View,並為每個都設上 android:clickable="true" 與 android:focusable="true":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:id="@+id/ll"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:id="@+id/ll1"
                android:clickable="true"
                android:focusable="true"
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@android:drawable/btn_default" >
                <ImageView
                    android:src="@drawable/feedicon"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"/>
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                    <TextView android:text="Line one"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"/>
                    <TextView android:text="Line two"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"/>
                </LinearLayout>
            </LinearLayout>

            <!-- ll2 結構與 ll1 相同 -->
        </LinearLayout>
    </ScrollView>
</LinearLayout>

透過指定 LinearLayout 的 clickable(若在程式碼中設定了 OnClickListener,系統會自動幫你打開這個屬性)與 focusable,每個組合 View 都能用軌跡球或方向鍵取得焦點,點擊時也會顯示焦點狀態。這種用法是正常的。

示例二:包成自訂組合 View

但在某些情況下,組合 View 顯示的內容(圖示、上下兩行文字)是依其他資料模型動態決定的。為了簡化程式碼,我們很自然會想寫一個自訂 View 把這段結構封裝起來。

版面配置 icon_text_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:background="@android:drawable/btn_default"
    android:focusable="true"
    android:clickable="false"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:src="@drawable/feedicon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:text="IconTextView Line one"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
        <TextView android:text="IconTextView Line two"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

自訂的 IconTextView 類別:

package org.goodev.viewtest;

public class IconTextView extends LinearLayout {

    ImageView mImageView;
    TextView  mTextOne;
    TextView  mTextTwo;

    public IconTextView(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.icon_text_view, this, true);
        // 這裡接著找出 mImageView、mTextOne、mTextTwo
    }

    public void setData(Drawable icon, String line1, String line2) {
        mImageView.setImageDrawable(icon);
        mTextOne.setText(line1);
        mTextTwo.setText(line2);
    }
}

動態把它加入 Layout:

LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
IconTextView itv = new IconTextView(this);
itv.setOnClickListener(listener);
ll.addView(itv, 0);

怪異現象出現了

此時你會發現:IconTextView 在按下時沒有顯示對應的狀態(背景圖不會換成 Press 樣式)。

若把 icon_text_view.xml 中的 clickable 改為 true,按下時的背景就正常了——但接著又冒出新的問題:IconTextView 的點擊事件沒了,使用者點擊這個組合 View 完全不會觸發 OnClickListener。

於是形成一個尷尬的局面:直接寫在版面配置中的組合 View 可以同時正常顯示 Press 狀態並響應點擊,一旦改用 IconTextView 封裝,兩者卻變成不可兼得。為什麼會這樣?下一篇我們從 View 階層結構的角度拆解這個問題。

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