如何在Android中禁用视图背后的视图?

时间:2014-11-07 16:52:12

标签: android android-layout listview view android-ui

我有一个像这样的ListView的LinearLayout:

Training.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="vertical"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
        <include layout="@layout/head_buttons"></include>
        <ListView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/programmes" android:layout_gravity="bottom"/>
</LinearLayout>

这个LinearLayout包含在这样的FrameLayout中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="vertical"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
        <include layout="@layout/training_page"></include>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="60"
            android:gravity="center"
            android:alpha="0"
            android:id="@+id/countdown" android:layout_gravity="center|center_vertical"
            android:background="@drawable/circle_icon"
            android:textColor="#ff000000" android:textSize="60dp"
            android:layout_marginTop="30dp"
            android:keepScreenOn="true"
            />
</FrameLayout>

TextView&#34;倒计时&#34;如果我需要它将永远出现。现在我希望我可以禁用ListView中的所有项目,以便用户无法单击它们。我在代码中尝试的是:

  //disable other views behind,
                for (int i = 0; i < programListView.getChildCount(); i++) {
                    View child = programListView.getChildAt(i);
                    child.setEnabled(false);
                }

但它没有正常工作,我仍然可以点击文本视图后面的项目。我该怎么办?

2 个答案:

答案 0 :(得分:0)

我假设您在类似的布局中定义了列表项?

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if(convertView == null) {
    // Inflate a new View every time a new row requires one.
    convertView = LayoutInflater.from(context).inflate(R.layout.shelfrow, parent, false);
}
   ...
}

你可以尝试:

for (int i = 0; i < programListView.getChildCount(); i++) {
                View child = programListView.getChildAt(i);
                EnableDisable(true,child);
 }


private static void EnableDisable(boolean enable, ViewGroup layout) {
    //layout.setEnabled(false);
    for (int i = 0; i < layout.getChildCount(); i++) {
        View child = layout.getChildAt(i);
        if (child instanceof ViewGroup) {
            EnableDisable(enable, (ViewGroup) child);
        } else {
            child.setEnabled(enable);
        }
    }
}

答案 1 :(得分:0)

1)在自定义ArrayAdapter中创建一个参数exp boolean mIsListEnable = true;

2)覆盖 isEnabled 功能:

@Override public boolean isEnabled(int position) { return mIsListEnable; }

3)为此参数创建一个setter:

public void setEnabled(boolean value){
   mIsListEnable = value;  
}