Android ListView items not responding when come back from another activity?

北城余情 提交于 2019-12-21 23:29:21

问题


I create a ListView from an ArrayAdapter. Each row of ListView have an ImageView and a TextView. Now I handle clicked event by using setOnItemClickListener

lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            CategoryInfo cat = (CategoryInfo) lv
                    .getItemAtPosition(position);
            showGameByCategory(Long.valueOf(cat.getId()), cat.getName());
        }
    });

When I click on a row, it will start another activity. On that activity, I have a button to go back to the ListView. When click that button, I'll call Activity.finish();.

Here's the problem: first time I open ListView and click on any items, it works fine and open new activity. But when I click button back and go back to ListView, I can't click to any items. My app have many tabs, if I switch to another tab and switch back to the ListView, I can click ListView item again. But anytime I click button back, the items are unclickable.

I test on 2 different OS and it works normally on Android 2.3 but this error occurred on Android 4.0.

Does anyone know how to fix this?

EDIT: this is my layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/listitem_selector_odd"
    android:padding="6dip" >

    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/avatar"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginRight="6dip" 
            android:layout_alignParentLeft="true"/>

        <TextView
            android:id="@+id/toptext"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_toRightOf="@+id/avatar"
            android:layout_marginRight="6dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textColor="#000000"
            android:textSize="18dp"
            android:textStyle="bold" >
        </TextView>

        <ImageView
            android:id="@+id/arrow"
            android:layout_width="10dip"
            android:layout_height="15dip"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="6dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:src="@drawable/arrow_icon" />
    </RelativeLayout>

</LinearLayout>

回答1:


I had exactly the same problem as you. Even stranger, it happened on some tablet 4.0 or 4.1, while it didn't happen on a phone 4.1. Using "descendentFocusabilty=blocksDescendants" didn't solve it.

Following the idea of user1603602, I ask for the invalidation of Views of the list (hence a redraw of elements of the list) each time the View becomes visible:

@Override
protected void onWindowVisibilityChanged(int visibility) {
    super.onWindowVisibilityChanged(visibility);
    if (visibility == View.VISIBLE) {
        list.invalidateViews();
    }
}

As it reloads each items of the list, it can be annoying if you load images with animations for example, but it solves the problem, and it keep the scrolled position.




回答2:


I had the same problem and just figured out a way to solve it. Remove the listview, then add it back. This will force the view to refresh to the original working state.

for example if your ListView lv is on a LinearLayout lout

 lout.removeView(lv);
 lout.addView(lv);

make sure you call this from the UI thread.




回答3:


I think this is the same issue as described here

I used a custom adapter containing an array of Views. Within getView(position, ...), I return the View at the specified position. Apparently, ListView didn't like this and sometimes (platform specific) recycles views that shouldn't be recycled.



来源:https://stackoverflow.com/questions/11184987/android-listview-items-not-responding-when-come-back-from-another-activity

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