问题
How to handle the positions in Gallery.I need to change the background of image at perticular position.Iam able to change the background image of selected image successfully within the onItemClick method.But i need to change the old selected background image also as non-selected one.
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
//previouse selected image
//here sometimes it is changing background of another image(which is not a old selected image)
if(mSelectedItemIndex!=-1 && mSelectedItemIndex!=position){
// Object v =mGallery.getItemAtPosition(mSelectedItemIndex);
// View v =imageAdapter.getDropDownView(mSelectedItemIndex,null,null);
// View v =mGallery.getChildAt(mSelectedItemIndex);
View v = (View)view.getChildAt(mSelectedItemIndex);
ImageView imgView = (ImageView) v.findViewById(R.id.adsimage);
// ImageView imgView = (ImageView)v;
// ImageView imgView = (ImageView)arg0.findViewById(R.id.adsimage);
imgView.setBackgroundResource(R.drawable.gallery_unselected_default);
//current selected image
//always this code is working fine.
ImageView imgView = (ImageView)view.findViewById(R.id.adsimage);
imgView.setBackgroundResource(R.drawable.gallery_selected_focused);
}
I used getItemBackground theme in base adapter but no use.Please give me how to persist image positions if gallery is scrolling form left to right and right to left.If i select one by one it is working fine.But when i select alternate image.It is not working.
回答1:
Create a member variable eg View lastSelected
in your activity to store the view when it has been selected/clicked. When a new item is selected/clicked use lastSelected
to change the background and then update lastSelected
with the newly selected view.
Hope that helps.
来源:https://stackoverflow.com/questions/4958262/how-to-handle-the-positions-in-gallery-android