简体   繁体   中英

android change image of imageview in listview

I have created a sliding menu. In that i put image-view, on clicking image-view it shows me content,all works fine.

Now what i want to do is, on selecting image-view, which is active i want to change its image and remaining things will still be unchanged... (i have created for that adapter )

private void onMenuItemClick(AdapterView<?> parent, View view,
            int position, long id) {

//bla bla bla
//some other code
    if (selectedItem.compareTo("ABC") == 0) {
                View subview = view.findViewById(R.id.list_image);
                ((ImageView) subview).setImageResource(R.drawable.img1);
                fragment = new abcactivity();

            } else if (selectedItem.compareTo("XYZ") == 0) {
                View subview = view.findViewById(R.id.list_image);
                ((ImageView) subview).setImageResource(R.drawable.img2);
                fragment = new xyzactivity();
            } else if (selectedItem.compareTo("Another") == 0) {
                View subview = view.findViewById(R.id.list_image);
                ((ImageView) subview).setImageResource(R.drawable.img3);
                fragment = new anotheractivity();
            }

here i put images in

 images = new ArrayList<Integer>();
            images.add(R.drawable.img1);
            images.add(R.drawable.img2);
images.add(R.drawable.img3);

I want change only activated image and that's also i have done but it should change to previous if i am selecting second position image.!

I think what you need is a selector file. You need add a image_change.xml in your /res/drawable folder.

img_selector.xml

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

And in your ImageView, you must set the drawable file:

<ImageView
    ...
    android:background="@drawable/img_selector"
    ... />

Or, in your code:

images.setBackground(R.drawable.img_selector);

I hope I've helped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM