Android其他活动点击返回键到ListView,“tab”下的ListView项目无法点击

3

我有一个TabHost,

选项卡A会连接到A活动,

A活动的布局有一个编辑文本框、按钮和一个列表视图。

列表视图是从BaseAdapter自定义的,其中包括一个TextView,没有按钮或图片等。

当我进入程序时,可以单击列表视图项目,

它将连接到B子活动。

但是当我点击返回键(返回到A活动)时,

列表视图的项目无法工作,必须向上或向下拖动,

该项目才能被正确地单击。

我已经搜索了一些资料。

我尝试添加了

    android:focusable="false"
    android:focusableInTouchMode="false"
    android:clickable="false"

    or

    descendantFocusability="blocksDescendants"

在我的列表视图中,我使用了LinearLayout或者TextView。

但是存在一个问题。

如何解决这个问题?非常感谢。

我的A布局如下:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bgColor"
        tools:context=".MainActivity" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_marginTop="4dp"
                android:layout_weight="1"
                android:gravity="center" >

                <EditText
                    android:id="@+id/drugText"
                    android:layout_width="224dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:ems="10"
                    android:gravity="center_vertical|center"
                    android:hint="@string/searchHint1"
                    android:textSize="18sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="3"
                android:gravity="left" >

                <Button
                    android:id="@+id/query"
                    style="@style/ButtonText"
                    android:layout_width="70dp"
                    android:layout_height="48dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/blue_btn"
                    android:text="@string/search"
                    android:textSize="20sp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
           android:layout_below="@+id/linearLayout1" >

            <ListView
                android:id="@+id/drugList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:clickable="true" />
        </LinearLayout>

    </RelativeLayout>

以下是我的列表项布局:

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/color_listview_border"
            android:gravity="center"
            android:minHeight="60dp" >

            <TextView
                android:id="@+id/drugNameTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="20sp" />
        </LinearLayout>

    </LinearLayout>

把你的ListView的点击事件放在onResume方法里,它应该可以工作。让我知道你得到了什么结果。 - Aamirkhan
我将listview的点击事件放在了onResume()中;@Override protected void onResume(){ super.onResume(); Log.e("log", "on resumed"); drugLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {.......}});}` 但是它显示以下错误。(错误信息很长,我在这里打印了照片) [链接](http://134.208.3.130/~dickfala/android/err.png) 非常感谢。 我使用ActivityGroup来控制返回。 我的activityGroup代码在这里。[链接](http://134.208.3.130/~dickfala/android/ActivityGroup1.java) - dickfala
我在这里放置了我的主代码框架链接。 非常感谢。 - dickfala
错误是由于某些空值引起的,它会给你一个空指针异常,因此在onResume方法中缺少一些数据,这些数据负责显示列表视图或任何其他元素的内容。请仔细检查一下,我认为你离完美运行代码很近了,解决空指针异常即可。 - Aamirkhan
我已经尝试编写@Override protected void onResume(){ super.onResume(); Log.e("log", "on resumed"); });。当我从B子活动返回到A子活动时,我没有看到日志“on resumed”显示。 @~@ - dickfala
我发现这个问题的原因。 我更改了其他布局,它没有这个问题。 导致这个问题的是EditText焦点问题。 但是我现在还没有解决。 - dickfala
2个回答

0
你可以在跳转到B子活动时保存listview的状态。返回A子活动后,你需要恢复listview的状态。

当我回来时,A子活动上的列表有数据,但无法点击项目。必须向上或向下拖动,项目才能被点击。 - dickfala
当您点击返回键时,子活动不会自动获得焦点。请尝试在活动或列表视图上设置焦点。 - Lee Lin
请问如何自动获取焦点? - dickfala
你可以覆盖activity的onResume函数。 - Lee Lin
首先感谢您的回复。 我在A子活动中编写了以下代码: @Override protected void onResume(){ super.onResume(); Log.e("log", "on resumed");} 但是,我没有从B子活动中看到日志。 如果我添加下面两行代码: drugLV.setItemsCanFocus(true); drugLV.requestFocus(); 它会显示错误。 FATAL EXCEPTION: main java.lang.RuntimeException: Unable to resume activity java.lang.RuntimeException: Unable to resume activity {xxx.xxx.ActivityGroup1}: java.lang.RuntimeException: Unable to resume activity - dickfala

0
在 onResume 函数中,只需调用 adapter.notifyDataSetChanged();。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接