Skip to content Skip to sidebar Skip to footer

Listview Odd/even Rows Refresh Automatically When Scrolling Down Or Selecting Anotheritem

I use even and odd rows to set backgrond to my listview rows. In my efficientAdapter I set the row background as follows: public View getView(int position, View convertView, ViewG

Solution 1:

All row view of a ListView created by the getView() method of BaseAdpter class. When ever we scroll the ListView all, new viable row create by getView() using recycle. So getView() called again and again when new row is viable on scroll.

There are two solution of your question:-\

  • You can save the status of ListView

// Save ListView state Parcelable state = listView.onSaveInstanceState();

// Set new items
listView.setAdapter(adapter);

// Restore previous state (including selected item index and scroll position)
listView.onRestoreInstanceState(state)
  • And other solution is create RowView at runtime and add it on a Parent Layout by using addView() method.

LayoutInflater inflater = LayoutInflater.from(context);

// You should use the LinerLayout instead  of the listview, and parent Layout should be inside of the ScrollView
parentView = (LinerLayout)this.findViewById(R.id.parentView);

for(inti=0; i<=numberOfRow;i++){
   LinearLayoutrowView= (LinerLayout)inflater.inflate(R.layout.rowView);
   ImageViewrowImageView= (ImageView)rowView.findViewById(R.id.rowImage);
   rowImageView.setOnClickListener(newView.onClickListListener(){
     @OverridepublicvoidonClick(){
        rowImageView.setImageBitmap(onClickBitmapImage);      
 }

});
parentView.addView(rowView); 
}

Please check this answer Maintain/Save/Restore scroll position when returning to a ListView

More Reference http://developer.android.com/reference/android/widget/Adapter.html#getView(int,android.view.View, android.view.ViewGroup)

Solution 2:

The item changes back to the default background because the view gets recycled. This is the same problems of checkboxes losing their checked state

Check out this answer too see how to handle it:

CheckBox gets unchecked on scroll in a custom listview

As for your second problem, I believe it's already answered here:

highlighting the selected item in the listview in android

Hope it helps

Post a Comment for "Listview Odd/even Rows Refresh Automatically When Scrolling Down Or Selecting Anotheritem"