【问题标题】:android - listview - blocksDescendants and avoid highlighting sub elementsandroid - listview - blocksDescendants,避免高亮子元素
【发布时间】:2013-06-23 13:58:08
【问题描述】:

为了能够选择项目(行)和行上的子项目,我使用android:descendantFocusability="blocksDescendants"。我正在使用onItemSelectevent....

我也为我的行使用自定义背景。

现在的问题是,当我触摸一行时,复选框也会被选中(蓝色默认背景,当您选择复选框时),我该如何避免这种情况?当我触摸该行时,我只希望该行本身将其背景调整为项目状态。

我在适配器的列表视图中使用以下行布局作为一行:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/list_gradient"
android:descendantFocusability="blocksDescendants" >

<CheckBox
    android:id="@+id/cbSelected"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true" />

<TextView
    android:id="@+id/tvDayName"
    style="@style/text_list_info_big"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/cbSelected"
    android:text="Tag" />

<RelativeLayout
    android:id="@+id/drag_handle"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true" >

    <ImageView
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/grabber" />
</RelativeLayout>

<LinearLayout
    android:id="@+id/lvData"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/s"
    android:layout_toRightOf="@+id/tvDayName"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvCount"
        style="@style/text_list_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="" />

    <TextView
        android:id="@+id/tvInfo"
        style="@style/text_list_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="" />

    <TextView
        android:id="@+id/tvInfo2"
        style="@style/text_list_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="" />
</LinearLayout>


</RelativeLayout>

PS:我知道我可以处理行的onClick 事件和我的适配器中的子项,而不是使用onItemClick,但我使用的是 DragAndSortList 视图并且这样做不适用于拖放到那里...

【问题讨论】:

    标签: android listview row highlight


    【解决方案1】:

    我找到了解决方案。覆盖行的 SetPressed 无效。

    现在我正在使用以下 RelativeLayout,这解决了我的问题:

    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.RelativeLayout;
    
    public class UnpressableRL extends RelativeLayout
    {
        public UnpressableRL(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }
    
        @Override
        protected void  dispatchSetPressed(boolean pressed) {
            // avoid handing on the event to the child views
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 1970-01-01
      • 2014-10-24
      • 2013-08-07
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多