【问题标题】:Android CustomListView item higlhight on clickAndroid CustomListView item higlhight on click
【发布时间】:2013-04-14 11:22:11
【问题描述】:

我想突出显示在自定义列表视图上单击的项目。我尝试了不同的选项,但没有一个有效。谁能告诉我怎么了?

我的自定义列表视图com.foo.ViewEditListView extends ListView implements android.widget.AdapterView.OnItemLongClickListener

list_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"
     >

    <TextView
        android:id="@+id/textView_list_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        >
    </TextView>

     <EditText
         android:id="@+id/searchQueryText"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="left"
         android:drawableRight="@drawable/search"
         android:hint="Search Expenses by Amount"        
         android:textColorHint="#5e8e19"
         android:inputType="numberSigned|numberDecimal"
         style="@android:style/TextAppearance.Small"         
          >

        </EditText>

     <com.foo.ViewEditListView
        android:id="@+id/viewExpenseListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="@/drawable/list_selector_bg" //doesn't work
         />

</LinearLayout>

list_row.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@/drawable/list_selector_bg" //doesn't work
    >

 <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"

     >

  <TextView
        android:id="@+id/v_row_field1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="14sp"
        android:textStyle="bold"
        android:textColor="#C11B17"
        android:layout_alignParentLeft="true"
        />


    <TextView
        android:id="@+id/v_row_field2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textStyle="bold"
        android:layout_marginRight="2dp" />

  </RelativeLayout>  

</LinearLayout>



list_selector_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_pressed="true"
    android:drawable="@android:drawable/list_selector_background" />      


</selector>

我也尝试在 Activity 中设置选择器,但它们都不起作用。现在,我一无所知。有人可以帮我吗?

如果我将 android:background="@/drawable/list_selector_bg" 放入 RelativeLayout,它只会突出显示该部分。但我希望突出显示整个布局。

谢谢 CP

【问题讨论】:

    标签: android listview click highlight


    【解决方案1】:

    1) 如果您想突出显示选定的项目,您可以使用您的 CustomAdapter 来处理它。但这不是很好的方法:)

    2) 我认为,最好的方法是为您的视图使用常见的 Android 实现 Checkable 接口(例如):

    public class CheckableFrameLayout extends FrameLayout implements Checkable {
        private boolean mChecked;
    
        public CheckableFrameLayout(Context context) {
            super(context);
        }
    
        public CheckableFrameLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public void setChecked(boolean checked) {
            mChecked = checked;
            setBackgroundDrawable(checked ? new ColorDrawable(0xff0000a0) : null);
        }
    
        public boolean isChecked() {
            return mChecked;
        }
    
        public void toggle() {
            setChecked(!mChecked);
        }
    }
    

    3) 使用CheckedTextView

    您可以在Android Samples 中找到名为 ApiDemos 的示例(多选的 ListView)。

    更新: 别忘了你的ListView 必须有ChoiceMode != ListView.CHOICE_MODE_NONE

    CheckableFrameLayout 放入你的包中并像这样重写你的list_row.xml

    <com.foo.CheckableFrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/list_selector_bg"
        >
    
     <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         >
    
      <TextView
            android:id="@+id/v_row_field1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:textSize="14sp"
            android:textStyle="bold"
            android:textColor="#C11B17"
            android:layout_alignParentLeft="true"
            />
    
    
        <TextView
            android:id="@+id/v_row_field2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:textStyle="bold"
            android:layout_marginRight="2dp" />
    
      </RelativeLayout>  
    </com.foo.CheckableFrameLayout>
    

    如果您想从状态列表中获取 CheckableFrameLayout 的状态(将其设置为后台),那么您需要覆盖您的 list_selector_bg.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_checked="true" android:drawable="@android:drawable/list_selector_background" />
    </selector>
    

    并重写 CheckableFrameLayout 以使用它。我没有匆忙正确地完成它(我不知道为什么它只在第二次点击后才正确更改突出显示)。但是您可以使用此代码并对其进行改进(也许使用CheckedTextView 的代码可以帮助您):

    public class CheckableFrameLayout extends FrameLayout implements Checkable {
        private static final int[] CHECKED_STATE_SET = {
            android.R.attr.state_checked
        };
    
        private boolean mChecked;
    
        public CheckableFrameLayout(Context context) {
            super(context);
        }
    
        public CheckableFrameLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected int[] onCreateDrawableState(int extraSpace) {
            final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
            if (isChecked()) {
                mergeDrawableStates(drawableState, CHECKED_STATE_SET);
            }
            return drawableState;
        }
    
        @Override
        public void setChecked(boolean checked) {
            mChecked = checked;
            // setBackgroundDrawable(checked ? new ColorDrawable(0xff0000a0) : null);
        }
    
        @Override
        public boolean isChecked() {
            return mChecked;
        }
    
        @Override
        public void toggle() {
            setChecked(!mChecked);
        }
    }
    

    【讨论】:

    【解决方案2】:

    将自定义可绘制设置为布局的背景。根据您的需要修改以下内容。按下时您将拥有不同的背景,释放时将返回默认状态。

    在您的 list_row.xml 中

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:background="@drawable/listbkg" // set backcground
    android:orientation="vertical"
     >
    ....
    </LinearLayout>
    

    可绘制文件夹中的listbkg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:drawable="@drawable/pressed" /> //pressed state
    <item  android:state_focused="false" 
        android:drawable="@drawable/normal" /> // normal state
    </selector>
    

    drawable 文件夹中的pressed.xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#FF1A47"/>     //background color 
    <stroke android:width="3dp"          // border color
            android:color="#0FECFF"/>
    <padding android:left="5dp"
             android:top="5dp"
             android:right="5dp"
             android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp"  //for rounded corners    
             android:bottomLeftRadius="7dp" 
             android:topLeftRadius="7dp"
             android:topRightRadius="7dp"/> 
    </shape> 
    

    drawable 文件夹中的 normal.xml

     <?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#FFFFFF"/>    //  change olor
    <stroke android:width="3dp"   // for border color
            android:color="#0FECFF" />
    
    <padding android:left="5dp"
             android:top="5dp"
             android:right="5dp"
             android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp"// for rounded corners
             android:bottomLeftRadius="7dp" 
             android:topLeftRadius="7dp"
             android:topRightRadius="7dp"/> 
    </shape>
    

    编辑:

    您还可以添加渐变,就像在快照中看到的一样。

    在自定义适配器的getView()中

         convertView =mInflater.inflate(R.layout.list_row, parent,false);
         // inflating custom layout
    

    然后在 list_row.xml 中设置根布局的背景。

    生成的快照

    按下时会突出显示布局,释放时会恢复正常状态。

    【讨论】:

    • 嗨,Raghunandan,我尝试了您的解决方案。但它不起作用。还是一样的行为。您认为 VieEditListView 是否引起了一些问题?
    • @Chintan 如果你做得对,这应该可以工作。我将自定义可绘制对象作为背景的列表视图行使用了相同的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    相关资源
    最近更新 更多