You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中点击ListView项后ImageView图标无法恢复默认求助

Fix: Revert ImageView Icon After ListView Item Click

The issue you're facing happens because once you modify the ImageView's resource programmatically in onItemClick, the ListView's recycled view retains that changed state even after returning to the Activity. To achieve the desired behavior (only show the pressed icon during the click, then revert immediately), the cleanest solution is to use Android's built-in State List Drawable (selector), which handles state changes automatically.

This approach lets Android manage the icon state (default/pressed) without manual code changes. Here's how to set it up:

Step 1: Create Selector Drawables

For each icon, create an XML file in res/drawable that defines the icon for different states.

Example for the "acil" icon (acil_icon_selector.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Show pressed icon when the item is clicked -->
    <item android:drawable="@drawable/mainpage_acil_icon" android:state_pressed="true"/>
    <!-- Default state (non-pressed) -->
    <item android:drawable="@drawable/mainpage_acil_icon_p"/>
</selector>

Repeat this for the other two icons:

  • bildirim_icon_selector.xml (uses mainpage_bildirim_icon for pressed, mainpage_bildirim_icon_p for default)
  • isguvenligi_icon_selector.xml (uses mainpage_isguvenligi_icon for pressed, mainpage_isguvenligi_icon_p for default)

Step 2: Update Your Activity Code

Modify the imageId array in MainPageActivity.java to use these selector drawables instead of the individual icons:

Integer[] imageId = {
    R.drawable.acil_icon_selector,
    R.drawable.bildirim_icon_selector,
    R.drawable.isguvenligi_icon_selector
};

Step 3: Remove Manual Image Changes

Delete the lines where you set imageView.setImageResource(...) inside the onItemClick method. The selector will now automatically switch the icon when the item is pressed and revert it when the press ends.

Your simplified onItemClick will look like this:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.v("Pozisyon", String.valueOf(position));
        Intent intentPos;
        switch (position) {
            case 0 :
                intentPos=new Intent(MainPageActivity.this,EmergencyActivity.class);
                startActivity(intentPos);
                break;
            case 1 :
                intentPos=new Intent(MainPageActivity.this,NotificationPage.class);
                startActivity(intentPos);
                break;
            case 2 :
                intentPos=new Intent(MainPageActivity.this,JobSecurityActivity.class);
                startActivity(intentPos);
                break;
            default :
                System.out.println("Hatali secim! 1, 2 ya da 3'e basiniz.");
                break;
        }
    }
});

Solution 2: Manual State Reset (Fallback)

If you can't use selectors for some reason, you can manually reset the icon after the click using an OnTouchListener to detect press and release events. Note this is less reliable with ListView recycling:

list.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int position = list.pointToPosition((int) event.getX(), (int) event.getY());
        if (position == ListView.INVALID_POSITION) return false;

        View listItemView = list.getChildAt(position - list.getFirstVisiblePosition());
        ImageView imageView = listItemView.findViewById(R.id.img);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Set pressed icon when user touches the item
                switch (position) {
                    case 0: imageView.setImageResource(R.drawable.mainpage_acil_icon); break;
                    case 1: imageView.setImageResource(R.drawable.mainpage_bildirim_icon); break;
                    case 2: imageView.setImageResource(R.drawable.mainpage_isguvenligi_icon); break;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // Reset to default icon and start activity
                switch (position) {
                    case 0:
                        imageView.setImageResource(R.drawable.mainpage_acil_icon_p);
                        startActivity(new Intent(MainPageActivity.this, EmergencyActivity.class));
                        break;
                    case 1:
                        imageView.setImageResource(R.drawable.mainpage_bildirim_icon_p);
                        startActivity(new Intent(MainPageActivity.this, NotificationPage.class));
                        break;
                    case 2:
                        imageView.setImageResource(R.drawable.mainpage_isguvenligi_icon_p);
                        startActivity(new Intent(MainPageActivity.this, JobSecurityActivity.class));
                        break;
                }
                break;
        }
        return false;
    }
});

Why Solution 1 is Better

  • Framework-managed: Android handles state changes (pressed, released, canceled) automatically.
  • Recycling-safe: Works correctly with ListView's view recycling, avoiding unexpected icon states.
  • Cleaner code: No need for manual state tracking or reset logic.

内容的提问来源于stack exchange,提问作者Ahmet Yılmaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:39:58