更改ListView上选定项目的背景颜色

我想知道如何在列表视图上更改所选项目的背景颜色。 我只想更改用户点击的特定项目,这意味着如果用户点击另一个项目,它将是突出显示的项目。 那么因为我希望它保持简单,并使用默认的Android列表视图,我用这个代码:

record_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { try{ for (int ctr=0;ctr<=record_items.length;ctr++){ if(i==ctr){ record_list.getChildAt(ctr).setBackgroundColor(Color.CYAN); }else{ record_list.getChildAt(ctr).setBackgroundColor(Color.WHITE); } } } catch (Exception e){ e.printStackTrace(); } Log.v("Selected item",record_list.getItemAtPosition(i)); } }); 

确定这个工作正常,但问题是速度很慢。 现在我想知道是否还有其他方法可以做,这样做会得到和我一样的输出。

我尝试使用record_list.getSelectedView().setBackgroundColor(Color.CYAN); 但它给了我一个空指针exception。

我也尝试了selector.xml,但它也没有做到这一点。 此外,在ListView上有一个属性叫做listSelector。 如文档“可绘制用于指示列表中当前选定的项目”所述,这是可绘制的。 我也相信,这应该做的伎俩,是的,它在我的模拟器上做的伎俩,但不是在我的星系选项卡上。 我也尝试了其他方法,但没有任何工作,因为我想它是。

您可以跟踪当前所选元素的位置:

  OnItemClickListener listViewOnItemClick = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) { mSelectedItem = position; mAdapter.notifyDataSetChanged(); } }; 

并覆盖适配器的getView方法:

  @Override public View getView(int position, View convertView, ViewGroup parent) { final View view = View.inflate(context, R.layout.item_list, null); if (position == mSelectedItem) { // set your color } return view; } 

对我来说,它做到了。

您可以使用select器。 更改颜色值并根据您的需要修改下面的内容。

bkg.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" /> <item android:state_focused="false" android:drawable="@drawable/normal" /> </selector> 

在可绘制的文件夹中被压缩

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

将背景可绘制设置为listview自定义布局,以便为每行填充

我build议使用自定义的适配器的列表视图。

  android:background="@drawable/bkg" 

如果你还没有使用自定义的适配器,你可以将listselector设置为listview,如下所示

  android:listSelector="@drawable/bkg" 

定义variables

 private ListView mListView; 

初始化variables

 mListView = (ListView)findViewById(R.id.list_view); 

ListView的OnItemClickListener

  mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adpterView, View view, int position, long id) { for (int i = 0; i < mListView.getChildCount(); i++) { if(position == i ){ mListView.getChildAt(i).setBackgroundColor(Color.BLUE); }else{ mListView.getChildAt(i).setBackgroundColor(Color.TRANSPARENT); } } } }); 

build立并运行项目 – 完成

如果您希望在点击该项目后突出显示该项目,则需要手动将其设置为在onItemClick侦听器中进行select

Android ListView选中的项目保持突出显示 :

 myList.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { view.setSelected(true); // <== Will cause the highlight to remain //... do more stuff }}); 

这假设你在你的select器中有一个state_selected项目:

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

首先,您可以在drawable/list_item_selector.xml创build如下所示的select器xml文件

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_activated="true"> <shape android:shape="rectangle"> <solid android:color="#333333" /> <padding android:left="5dp" android:right="5dp" /> </shape></item> <item><shape android:shape="rectangle"> <solid android:color="#222222" /> </shape></item> </selector> 

然后在你的listview中指定背景为

 android:background="@drawable/list_item_selector" 

方法1:

在你的xml布局activity / fragment中更新ListView:

 <ListView ... android:choiceMode="singleChoice" android:listSelector="@android:color/darker_gray" /> 

就是这样,你完成了!

如果你想要一个编程方式来处理这个然后使用方法2 …

方法2:

如果您使用的是ListFragment,则可以使用视图来覆盖onListItemClick(),以设置颜色。 保存选中的当前视图以重置上次select的颜色。

请注意,这仅适用于一个屏幕上的列表视图,因为视图被回收。

 public class MyListFragment extends ListFragment { View previousSelectedItem; ... @Override public void onListItemClick(ListView parent, View v, int position, long id) { super.onListItemClick(parent, v, position, id); if (previousSelectedItem!=null) { previousSelectedItem.setBackgroundColor(Color.WHITE); } previousSelectedItem=v; v.setBackgroundColor(Color.BLUE); } } 

使用下面的XML作为listitem背景它将解决所有的问题。 尽pipe您向下滚动,所选内容将突出显示。

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

谢谢,Nagendra

我也做类似的事情:突出显示选定的列表项目的背景(将其更改为红色),并将项目中的文本颜色设置为白色。

我可以想出一个“ 简单但并不高效 ”的方法:在自定义适配器中保持选定的项目位置,并在ListView的OnItemClickListener实现中对其进行更改:

 // The OnItemClickListener implementation @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mListViewAdapter.setSelectedItem(position); } // The custom Adapter private int mSelectedPosition = -1; public void setSelectedItem (int itemPosition) { mSelectedPosition = itemPosition; notifyDataSetChanged(); } 

然后在getView()方法中更新所选项目的背景和文本颜色。

 // The custom Adapter @Override public View getView(int position, View convertView, ViewGroup parent) { ... if (position == mSelectedPosition) { // customize the selected item's background and sub views convertView.setBackgroundColor(YOUR_HIGHLIGHT_COLOR); textView.setTextColor(TEXT_COLOR); } else { ... } } 

search一段时间后,我发现许多人提到要设置android:listSelector="YOUR_SELECTOR" 。 尝试一段时间后,我发现最简单的方法来突出显示选定的ListView项目的背景可以完成只有两行设置为ListView的布局资源:

 android:choiceMode="singleChoice" android:listSelector="YOUR_COLOR" 

还有其他的方式来使其工作,如自定义activatedBackgroundIndicator主题。 但我认为这将是一个更通用的解决scheme,因为它会影响整个主题。

最简单的方法,我发现:

在你的活动XML添加这些行:

 <ListView ... android:choiceMode="singleChoice" android:listSelector="#666666" /> 

或以编程方式设置这些属性:

 listView.setSelector(Drawable selector) listView.setSelector(int resourceId) 

我特别的例子:

  <ListView android:choiceMode="singleChoice" android:listSelector="#666666" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listView"/> 

感谢AJG: https ://stackoverflow.com/a/25131125/1687010

对于那些想知道为了保持行的select,甚至在向下滚动的时候都需要做的事情。 这是state_activated其余的是由内部function照顾,你不必担心切换,并可以select多个项目。 我不需要使用notifyDataSetChanged()或setSelected(true)方法。

将此行添加到您的select器文件中,对于我来说是可绘制的\ row_background.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:color/holo_blue_light"/> <item android:state_enabled="true" android:state_pressed="true" android:drawable="@android:color/holo_blue_light" /> <item android:state_enabled="true" android:state_focused="true" android:drawable="@android:color/holo_blue_bright" /> <item android:state_enabled="true" android:state_selected="true" android:drawable="@android:color/holo_blue_light" /> <item android:state_activated="true" android:drawable="@android:color/holo_blue_light" /> <item android:drawable="@android:color/transparent"/> </selector> 

然后在layout \ custom_row.xml中

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dip" android:background="@drawable/row_background" android:orientation="vertical"> <TextView android:id="@+id/line1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

有关更多信息,我使用ListView适配器,使用myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 和myList.setMultiChoiceModeListener(new MultiChoiceModeListener()…

从这个例子: http : //www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/

此外,你(应该)使用这个结构为你的列表适配器耦合:List myList = new ArrayList();

而不是:ArrayList myList = new ArrayList();

说明: 在Java中inputList与typesArrayList

我知道这是一个老问题,但我提供了一个简单的解决scheme(没有循环!):

 //On your adapter create a variable: private View lastSelectedItem; //Define the folowing method: private void toggleBackgroundItem(View view) { if (lastSelectedItem != null) { lastSelectedItem.setBackgroundColor(Color.TRANSPARENT); } view.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark)); lastSelectedItem = view; } //finally invoque the method onItemClick lvSac.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick (AdapterView < ? > adapterView, View view,int i, long l){ toggleBackgroundItem(view); } } 

假设您想要每次单击一个项目。 那么这个代码运作良好。 我们以listview名称作为stlist

 stList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override // here i overide the onitemclick method in onitemclick listener public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //color change //selected item colored for(int i=0; i<stList.getAdapter().getCount();i++) { stList.getChildAt(i).setBackgroundColor(Color.TRANSPARENT); } parent.getChildAt(position).setBackgroundColor(Color.GRAY); }); 

这是一个简单的方法,可以处理select,即使列表也很长:

 public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Holder holder=new Holder(); View rowView; rowView = inflater.inflate(R.layout.list_item, null); //Handle your items. //StringHolder.mSelectedItem is a public static variable. if(getItemId(position)==StringHolder.mSelectedItem){ rowView.setBackgroundColor(Color.LTGRAY); }else{ rowView.setBackgroundColor(Color.TRANSPARENT); } return rowView; } 

然后在你的onclicklistener:

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { StringHolder.mSelectedItem = catagoryAdapter.getItemId(i-1); catagoryAdapter.notifyDataSetChanged(); ..... 
  View updateview;// above oncreate method listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override `enter code here`public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (updateview != null) updateview.setBackgroundColor(Color.TRANSPARENT); updateview = view; view.setBackgroundColor(Color.CYAN); } }); 

在一个ListView集合中:

 android:choiceMode="singleChoice" 

为背景创build一个select器(drawable / selector_gray.xml):

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/gray" android:state_checked="true" /> <item android:drawable="@color/white" /> </selector> 

添加一个项目的列表:

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:background="@drawable/selector_gray" android:textColor="@color/colorPrimary" tools:text="Your text" /> 

在ViewHolder中,你可以膨胀这个项目。