Android listview item background change

前端 未结 3 578
借酒劲吻你
借酒劲吻你 2021-01-18 07:27

i have a android listview. i want to change listview item background when i click one listview item.

and then previous selected item must go back to default backgrou

相关标签:
3条回答
  • 2021-01-18 08:05

    You should use the built in methods of selecting items in a listview. Manually changing the background is prone to error as you have found.

    Add this attribute to the root view in your listview item xml

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

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

    Ensure your listview has a ChoiceMode set that allows selection (such as "SingleChoice")

    0 讨论(0)
  • 2021-01-18 08:06

    You can change the ListView item colour on clicking it like below. Follow these steps.

    (Remember, This is for Custom List View)

    1. 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 own resources.

    2. While implementing Custom ListVIew, you'll have additional layout for Custom List Item design. Below is such 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 Above code I have put the set the XML file from Step 1 as "background" attribute. This will work as you want to.

    Additionally if you want to change the text colour on ListItem selection as well, use below XML code and set that XML file as "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 text color to while on selection and revert to original when unclicked.

    0 讨论(0)
  • 2021-01-18 08:29

    When I have this in a similar example I have a global field named:

    selectedListItem;
    

    This would be updated in your onitemClickListener and the previous item would then have it's background returned to the default.

    So to update your 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;
    
        }
    }
    

    So simply initalise selectedListItem as a field in your adapter with the onClickListener as an inner class and have your default background drawable instead of list_default_background .

    Alternatively you can track the position numbers instead of the actual view.

    EDIT:

    To use this method for your list you will also have to keep track of an ID or object instance for your specific list item. In my own solution, in my ListAdapter's getView method I make sure only the list item that matches the ID/instance of the correct item is updated. With your code the way it is you will also find that when you scroll down the view at the same position in this list of visible items is also updated. This is because list view's refer to the list in sets of items, where each set corresponds to the items visible on the screen at any one time.

    To update a singular, specific item you would be better suited to using a selector background or indicator as mentioned in the other answers.

    HTH

    0 讨论(0)
提交回复
热议问题