How to handle the positions in Gallery android?

别说谁变了你拦得住时间么 提交于 2020-01-03 06:03:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!