2013-02-01 82 views
1

在我的android应用程序中,我有列表视图和每个列表项的详细视图。对于平板电脑,我显示了项目列表视图和选定项目的详细视图,如下所示。如何在android中突出显示选定的项目?

enter image description here

所以我的问题是我怎么能高亮显示所选项目用户点击列表项之后。

我使用一个BaseAdapter我view.How可以做到这一点任何想法来加载列表?

编辑:

是作为chintan khetiya提到我用下面的XML文件的列表项的背景,但也不会高兴选定的项目。我错过了什么?

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
android:state_selected="false" 
    android:state_pressed="false" 
    android:drawable="@color/abs__background_holo_light" /> 
<item android:state_pressed="true" 
    android:drawable="@color/action_bar_blue" /> 
<item android:state_selected="true" 
android:state_pressed="false" 
    android:drawable="@color/action_bar_blue" /> 
</selector> 

回答

2

您的疑问:

我的问题是我怎么能高亮显示所选项目用户点击列表项之后。

我想你问选择。意思是如果列表行处于焦点状态,那么它应该与所有其他行看起来不同。按下或触摸行时也是如此。

对于你必须在文件夹可绘制文件Selector.xml,只是把那个选择文件在列表中排

该文件应该有不同的标签一样Focus-Click-Press和更改绘制对象按状态。

更新:

只需更换你的图标,并在保存文件夹可绘制。

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

    <!-- Pressed --> 
    <item android:drawable="@drawable/p_paly_press" android:state_pressed="true"/> 

    <!-- selected --> 
    <item android:drawable="@drawable/p_play" android:state_selected="true"/> 

    <!-- focused --> 
    <item android:drawable="@drawable/p_paly_press" android:state_focused="true"/> 

    <!-- default --> 
    <item android:drawable="@drawable/p_play"/> 

</selector> 
+0

是的chintan khetiya我编辑了这个问题,你能告诉我该代码的错误部分。 –

+0

我有更新答案。接受,如果它对你有帮助 –

+0

它为我工作相同的代码,我有粘贴在这里。其他问题 –

1

您可以在styles.xml定义

<style name="Theme.Base" parent="..."> 
     <item name="activatableItemBackground">@drawable/activatable_item_background</item> 
    </style> 

    <style name="ListItemContainerBase"> 
     <item name="android:background">?activatableItemBackground</item> 
    </style> 

在res /绘制定义activatable_item_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/item_pressed" android:state_pressed="true" /> 
    <item android:drawable="@drawable/item_focused" android:state_focused="true" /> 
    <item android:drawable="@drawable/item_focused" android:state_selected="true" /> 
    <item android:drawable="@drawable/item_activated" android:state_activated="true" /> 
    <item android:drawable="@drawable/item_checked" android:state_checked="true" /> 
    <item android:drawable="@android:color/transparent" /> 
</selector> 

item_pressed,item_focused .....在水库图像/绘制-XXX

定义您的视图中的每个条目的布局是这样的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="@style/ListItemContainerBase"> 
2

类似下面的下面的代码中的代码可用于高亮显示所选项目的肯定,如果其他方面不这样做,你需要:

class Adapter extends ArrayAdapter<String> { 

    private int selectedPos = -1; 
    Drawable selectedBackground; 

    public MainSelectAdapter(Context context, int textViewResourceId, 
      List<String> objects) { 
     super(context, textViewResourceId, objects); 
     selectedBackground = 
       context.getResources().getDrawable(R.color.selecteditembackground); 
    } 
    public void setSelectedPosition(int pos){ 
     selectedPos = pos; 
     notifyDataSetChanged(); 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = super.getView(position, convertView, parent); 
     if (selectedPos == position) { 
      v.setBackgroundDrawable(selectedBackground); 
     } else { 
      v.setBackgroundDrawable(null); 
     } 
     return v; 
    } 
} 

而且在主要活动

Adapter adapter = new Adapter(this, android.R.layout.simple_list_item_1, 
    myItemsToShow); 

    list = (ListView) findViewById(R.id.flows); 
    list.setItemsCanFocus(true); 
    list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    public void onItemClick(AdapterView<?> adapter, View view, 
       int pos, long id) { 
       adapter.setSelectedPosition(pos); 
    } 
    }); 

通过这种方法,您可以对选定的项目高亮显示进行自己的控制,您可以通过侦听器捕获选择事件,并为自己设置所需的Drawable。

+0

这种方法适用于'ArrayAdapter',但如果某人使用'CursorAdapter',该怎么办? –

相关问题