0

title explains it yet i would like to explain more;

I have a list view which all items have orange background. To say it simple: when one item is clicked it turns its background to blue. When another is clicked last clicked should be blue, previous should go to be orange with others.To make it happen, in single a row , i got 2 layouts(blue and orange) and on click , i make blue layout(llTicketViewOnClickContainer) visible.

not just background colour , it has buttons,images etc , i just wanted to simply explain it.

now the code below , its from the adapter which we use for list view

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final TicketViewDataObject item=ticketViewObjects.get(position);

    if(convertView==null){
            convertView = inflater.inflate( R.layout.companyviewlibrary_listview_row_ticketview   ,  parent,false);

    }else{
    }


    ticketView=(TicketView) convertView;
    final View clicked=ticketView.findViewById(R.id.llTicketViewOnClickContainer);
    ticketView.prepareTicketView(item);

    if(item.isClicked())
        clicked.setVisibility(View.VISIBLE);
    else
        clicked.setVisibility(View.GONE);
    ticketView.setOnClickListener(new OnClickListener() {
    for (TicketViewDataObject item : ticketViewObjects) {
                item.setClicked(false);
            }
            long timeStart =System.currentTimeMillis();
            refreshList();
            Log.e("TIME_POPULATE",""+(System.currentTimeMillis()-timeStart) );
    }

for the refreshList function

public void refreshList(){
    this.notifyDataSetChanged();

}

now my problem is when i use notifyDataSetChanged , it changes all data, clears and updates(tries to get images again which is our problem) , refills again. i just wanted to change previous clicked item's llTicketViewOnClickContainer visibility.

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
Alp
  • 1,863
  • 1
  • 20
  • 38
  • If the only thing that changes when you click on an item is the background, then you just have to use a drawable background and assign it to the ListView. – Daniel Hernández Alcojor Jun 04 '14 at 07:30
  • not just background colour , it ( layout ) has buttons,images etc , when i alter its visibility , it doesn't take effect without notiftydatasetchanged which changes all data set. – Alp Jun 04 '14 at 07:36
  • I do something similar but all I do is store the current selected index in a member variable inside my on click listener. Then in the getView, I check if the position is same as the saved index and then do special handling or else do whatever the normal. – Raghu Jun 04 '14 at 07:44

4 Answers4

2

You are facing ListView recycling. See this link for help but what you want is simple you should set your ListView background to orange by default and in your xml file use android:listSelector="@drawable/blue_color" like:

         <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/orange"
            android:listSelector="@drawable/blue" >
        </ListView>

and you are done :) hope this is what you want....

Community
  • 1
  • 1
Zubair Ahmed
  • 2,857
  • 2
  • 27
  • 47
1

If you want to change some item background, just need set background color to the item view 'itemView':

if(item_youwant_change)
{
    itemView.setBackgroundResource(whatyouwant_color);
}

but important to remember for all else items 'elseView', you have to set to your default color:

else
{
    elseView.setBackgroundResource(defalut_color);
}

Hope this help!

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33
1

You could try declaring a variable int selectedRow in your adapter and then in your getView() method inflate a different layout depending on the position of the view. Then you'll just have to update this variable whenever you click on a list item.

Freego
  • 456
  • 7
  • 18
0

Create an xml file in drawable and then:

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

Then in the getView() just write:

rowView.setBackgroundResource(R.drawable.list_selector_slider_menu);

where list_selector_slider_menu is the name of the .xml file in the drawable.

kgandroid
  • 5,507
  • 5
  • 39
  • 69