Android中点击ListView项后ImageView图标无法恢复默认求助
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.
Solution 1: Use State List Drawables (Recommended)
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(usesmainpage_bildirim_iconfor pressed,mainpage_bildirim_icon_pfor default)isguvenligi_icon_selector.xml(usesmainpage_isguvenligi_iconfor pressed,mainpage_isguvenligi_icon_pfor 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

