2013-04-21 97 views
0

我想突出显示点击自定义列表视图的项目。我尝试了不同的选择,但不是他们正在工作。有人能告诉我什么是错的吗?Android CustomListView高亮点击项目

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

我也试图在setSelector活动,但他们都不工作。现在,我无能为力。有人可以帮我吗?

如果我把android:background =“@/drawable/list_selector_bg”放到RelativeLayout中,它只会突出显示那部分。但我希望整个布局突出显示。

感谢 CP

回答

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在绘制fodler

<?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设置为根布局的背景。

得到的快照

当按下它突出的布局和发布将回到正常状态。

enter image description here

+0

嗨Raghunandan,我想你的解决方案。但它不起作用。还是一样的行为。你认为VieEditListView导致一些问题吗? – Chintan 2013-04-22 00:52:12

+0

@Chintan如果你做得对,这应该起作用。我用自定义drawable作为背景的列表视图行也一样。 – Raghunandan 2013-04-22 04:21:27

+0

谢谢拉古南丹。它正在工作! – Chintan 2013-04-22 23:59:01

0

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

2)我认为,最好的办法是用普通的Android执行勾选的接口,为您的观点(为例):

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); 
    } 
} 

OR

3)使用CheckedTextView

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

UPDATE: 不要忘记,你的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); 
    } 
} 
+0

我不希望复选框被显示。我如何将CheckableFrameLayout附加到我自定义的listview类(即ViewEditListView)? – Chintan 2013-04-22 01:00:49

+0

我也有customAdapter,我如何实现突出选定的项目? – Chintan 2013-04-22 01:18:42

+0

@Chintan我正在更新我的答案。使用适配器它不是很好的主意,但很多人使用它:( http://stackoverflow.com/questions/13699818/highlight-listview-item http://stackoverflow.com/questions/13137465/highlight- listview-items-android – nfirex 2013-04-22 07:21:26