Change the background of an Android list item

I have an android listview. I want to change the background of a list item when I click a single list item.

and then the previous selected item should return to the default background. this means that you need to select only one item.

I have been looking for him for a long time. I can change the background of the selected item using onItemClick ()

but I cannot change the previously selected item. for example, if I select the second item, it has been changed. and then I select the third item. Oh my God! he has changed too! what can i do for this. How can I get the previous position?

here is my android code.

private class ListViewItemClickListener implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView title = (TextView) view.findViewById(R.id.title); title.setBackgroundResource(R.drawable.list_shape); } } 
+6
source share
3 answers

When I have this in a similar example, I have a global field with the name:

 selectedListItem; 

This will be updated in your onitemClickListener, and the previous item will then return it to its default value.

So, to update the code:

 private class ListViewItemClickListener implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //First update the previously selected item if one has been set if(selectedListItem!=null){ TextView previousTitle = (TextView) selectedListItem.findViewById(R.id.title); previousTitle.setBackgroundResource(R.drawable.list_default_background); } //Then update the new one TextView title = (TextView) view.findViewById(R.id.title); title.setBackgroundResource(R.drawable.list_shape); selectedListItem = view; } } 

It's so easy to initalise selectedListItem as a field in your adapter with onClickListener as an inner class, and instead of list_default_background you can use the default background image.

Alternatively, you can track item numbers instead of the actual presentation.

EDIT:

To use this method for your list, you will also need to track an instance of an identifier or object for your specific list item. In my own solution, in my getView ListAdapter method, I make sure that only the list item that matches the identifier / instance of the correct item is updated. With your code, like you, you will also find that when you scroll down, the view at the same position in this list of visible elements is also updated. This is due to the fact that viewing a list refers to a list in sets of elements, where each set corresponds to elements visible on the screen at any given time.

To update a particular, specific element, it is better for you to use the background of a selector or indicator, as indicated in other answers.

NTN

+1
source

You must use the built-in methods for selecting items in the list. Manually changing the background is subject to errors, as you have already found.

Add this attribute to the root view in the listview xml element

 android:background="?android:attr/activatedBackgroundIndicator" 

then call setItemChecked(x, true) on the ListView, where x is the position of the item you want to select.

Make sure your list has a ChoiceMode that allows you to select (for example, "SingleChoice")

+4
source

You can change the color of the ListView by clicking on it as shown below. Follow these steps.

(Remember that this is for a custom list)

  • Create an XML file in Drawable Folder as below:

     <item android:drawable="@color/orange" android:state_focused="true"/> <item android:drawable="@color/orange" android:state_pressed="true"/> <item android:drawable="@drawable/listview"></item> 

    Choose your resources.

  • When implementing Custom ListView, you will have an additional layout for custom list design. The following is an example.

     <ImageView android:id="@+id/imageView1" android:layout_width="60dp" android:layout_height="60dp" /> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_toRightOf="@+id/imageView1" android:background="@drawable/listselect_picture" android:gravity="center" android:text="TextView" android:textColor="@drawable/select_txtcolor" android:textSize="16sp" /> 

In the above code, I put the XML file from step 1 as the "background" attribute. This will work the way you want.

In addition, if you want to change the color of the text in the ListItem, use the XML code below and set this XML file as the β€œTextColor” attribute.

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

The code above will change the color of the text during selection and will return to the original when disconnected.

+1
source

Source: https://habr.com/ru/post/957987/


All Articles