0

我想突出显示单击自定义列表视图的项目。我尝试了不同的选项,但没有一个有效。有人可以告诉我有什么问题吗?

我的自定义列表视图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

4

2 回答 2

1

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

在你的 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>

可绘制文件夹中的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> 

可绘制文件夹中的 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 中设置根布局的背景。

结果快照

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

在此处输入图像描述

于 2013-04-21T14:33:55.043 回答
0

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

您可以在名为 ApiDemos (ListView with multichoice)的Android 示例中找到示例。

更新: 不要忘记您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从 statelist 获取您的状态(将其设置为背景),那么您需要覆盖您的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);
    }
}
于 2013-04-21T15:24:58.403 回答