在 TabActivity 内的 ListView 上使用 OnItemClickListener

发布于 2024-11-27 11:47:22 字数 6502 浏览 1 评论 0原文

我的 ListView 的 OnItemCliskListener 遇到了一个愚蠢的问题。当 XML 非常简单时它工作得很好,但在我提高其复杂性后,OnItemCliskListener 停止工作

换句话说,我有一个只有 TextView 的 ListView。但是,如果我将 ListView 变成一个更复杂的列表,其中包含其他 TextView、图标、复选框,则 ClickListener 将停止工作。

如果我运行完全相同的代码,仅更改 XML,则 OnItemCliskListener 将停止工作。如果您能帮助我,我将不胜感激。

工作列表视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:orientation="vertical"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/title"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_toLeftOf="@+id/appCheck"
        android:layout_toRightOf="@+id/appIcon"
        android:textAppearance="?android:attr/textAppearanceLarge">
    </TextView>
</RelativeLayout>

不工作列表视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:orientation="vertical"
    android:layout_height="fill_parent">
    <ImageView android:src="@drawable/icon" android:id="@+id/appIcon"
        android:layout_width="30px" android:layout_height="40px"></ImageView>
    <CheckBox android:id="@+id/appCheck"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentRight="true"></CheckBox>
    <TextView android:id="@+id/title"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_toLeftOf="@+id/appCheck"
        android:layout_toRightOf="@+id/appIcon"
        android:textAppearance="?android:attr/textAppearanceLarge">
    </TextView>
    <TextView android:id="@+id/subtitle"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_toLeftOf="@+id/appCheck"
        android:layout_toRightOf="@+id/appIcon" android:layout_below="@+id/title"
        android:textAppearance="?android:attr/textAppearanceSmall">
    </TextView>
</RelativeLayout>

我的 JAVA 活动:

public class MyTabsActivity extends TabActivity implements OnTabChangeListener {
private static final String LIST3_TAB_TAG = "List3";

private RelativeLayout layoutTab3;      
private MyArrayAdapter adapterListViewTab3;
private TabHost tabHost;
...
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            tabHost = getTabHost();
            tabHost.setOnTabChangedListener(this);
...
...
// ----------------- BUILD TAB3
            // That is my Tab of Running App
            layoutTab3 = (RelativeLayout) findViewById(R.id.running_app_relative_layout);
            // Fill my ListView of Running App
            ListView runningAppList = (ListView) findViewById(R.id.running_app_listview);
            List<String> list3Strings = new ArrayList<String>();
            list3Strings.add("Item 1");
            list3Strings.add("Item 2");
            list3Strings.add("Item 3");
            list3Strings.add("Item 

            adapterListViewTab3 = new MyArrayAdapter(this, android.R.layout.simple_list_item_1, list3Strings);
            runningAppList.setAdapter(adapterListViewTab3);
            // ????????????????????????????????????????????????????????????????????????????????????????????
            // >>>>>>>>>>>> THIS OnItemClickAdapter DOESN'T WORK !!!!! WHY ????????
            runningAppList.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Toast.makeText(MyTabsActivity.this, "EUREKA", Toast.LENGTH_SHORT).show();
                }
            });

...
// add Tab3
            TabSpec tab3 = tabHost.newTabSpec(LIST3_TAB_TAG).setIndicator(LIST3_TAB_TAG).setContent(new TabContentFactory() {
                public View createTabContent(String arg0) {
                    return layoutTab3;
                }
            });
            tabHost.addTab(tab3);
            layoutTab3.setVisibility(View.INVISIBLE);
...
}
}

我的 R.layout.main:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">

            <ListView android:id="@+id/list1" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1">
            </ListView>

            <ListView android:id="@+id/list2" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1">
            </ListView>

            <RelativeLayout android:id="@+id/running_app_relative_layout"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout android:orientation="horizontal"
                    android:id="@+id/coco" android:layout_alignParentTop="true"
                    android:layout_width="fill_parent" android:layout_height="fill_parent">
                    <ListView android:id="@+id/running_app_listview"
                        android:scrollbars="vertical" android:layout_width="fill_parent"
                        android:layout_height="wrap_content" android:layout_gravity="fill_vertical"
                        android:layout_alignParentTop="true"></ListView>
                </LinearLayout>
            </RelativeLayout>

        </FrameLayout>
    </LinearLayout>
</TabHost>

I am having a silly problem with a OnItemCliskListener for my ListView. It works well when the XML is very simple but after I improve its complexity, the OnItemCliskListener stop working

In other words, I have a ListView with just a TextView. But if I turn my ListView into a more complex list with other TextView, an Icon a CheckBox, the ClickListener stop working.

If I run exactly the same code changing just the XML, the OnItemCliskListener stops working. I would be rather grateful if you could help me

WORKING LIST VIEW:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:orientation="vertical"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/title"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_toLeftOf="@+id/appCheck"
        android:layout_toRightOf="@+id/appIcon"
        android:textAppearance="?android:attr/textAppearanceLarge">
    </TextView>
</RelativeLayout>

NOT WORKING LISTVIEW:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:orientation="vertical"
    android:layout_height="fill_parent">
    <ImageView android:src="@drawable/icon" android:id="@+id/appIcon"
        android:layout_width="30px" android:layout_height="40px"></ImageView>
    <CheckBox android:id="@+id/appCheck"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentRight="true"></CheckBox>
    <TextView android:id="@+id/title"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_toLeftOf="@+id/appCheck"
        android:layout_toRightOf="@+id/appIcon"
        android:textAppearance="?android:attr/textAppearanceLarge">
    </TextView>
    <TextView android:id="@+id/subtitle"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_toLeftOf="@+id/appCheck"
        android:layout_toRightOf="@+id/appIcon" android:layout_below="@+id/title"
        android:textAppearance="?android:attr/textAppearanceSmall">
    </TextView>
</RelativeLayout>

MY JAVA ACTIVITY:

public class MyTabsActivity extends TabActivity implements OnTabChangeListener {
private static final String LIST3_TAB_TAG = "List3";

private RelativeLayout layoutTab3;      
private MyArrayAdapter adapterListViewTab3;
private TabHost tabHost;
...
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            tabHost = getTabHost();
            tabHost.setOnTabChangedListener(this);
...
...
// ----------------- BUILD TAB3
            // That is my Tab of Running App
            layoutTab3 = (RelativeLayout) findViewById(R.id.running_app_relative_layout);
            // Fill my ListView of Running App
            ListView runningAppList = (ListView) findViewById(R.id.running_app_listview);
            List<String> list3Strings = new ArrayList<String>();
            list3Strings.add("Item 1");
            list3Strings.add("Item 2");
            list3Strings.add("Item 3");
            list3Strings.add("Item 

            adapterListViewTab3 = new MyArrayAdapter(this, android.R.layout.simple_list_item_1, list3Strings);
            runningAppList.setAdapter(adapterListViewTab3);
            // ????????????????????????????????????????????????????????????????????????????????????????????
            // >>>>>>>>>>>> THIS OnItemClickAdapter DOESN'T WORK !!!!! WHY ????????
            runningAppList.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Toast.makeText(MyTabsActivity.this, "EUREKA", Toast.LENGTH_SHORT).show();
                }
            });

...
// add Tab3
            TabSpec tab3 = tabHost.newTabSpec(LIST3_TAB_TAG).setIndicator(LIST3_TAB_TAG).setContent(new TabContentFactory() {
                public View createTabContent(String arg0) {
                    return layoutTab3;
                }
            });
            tabHost.addTab(tab3);
            layoutTab3.setVisibility(View.INVISIBLE);
...
}
}

MY R.layout.main:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent" android:layout_height="fill_parent">

            <ListView android:id="@+id/list1" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1">
            </ListView>

            <ListView android:id="@+id/list2" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1">
            </ListView>

            <RelativeLayout android:id="@+id/running_app_relative_layout"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout android:orientation="horizontal"
                    android:id="@+id/coco" android:layout_alignParentTop="true"
                    android:layout_width="fill_parent" android:layout_height="fill_parent">
                    <ListView android:id="@+id/running_app_listview"
                        android:scrollbars="vertical" android:layout_width="fill_parent"
                        android:layout_height="wrap_content" android:layout_gravity="fill_vertical"
                        android:layout_alignParentTop="true"></ListView>
                </LinearLayout>
            </RelativeLayout>

        </FrameLayout>
    </LinearLayout>
</TabHost>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2024-12-04 11:47:22

我发现当自定义 ListView 包含可聚焦元素时, onListItemClick 将不起作用(我认为这是预期的行为)。所以我刚刚从自定义视图中删除了焦点,它就成功了。换句话说,我将以下命令添加到我的视图中:

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

完整的解释可以在这里找到: 如何使用列表中的按钮触发 Listactivity 中的 onListItemClick?

I've figured out that when a custom ListView contains focusable elements, onListItemClick won't work (I think it's the expected behaviour). So I've just removed the focus from the custom view and it did the trick. In other words, I added the following commands to my view:

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

The full explanation is avaiable here: How to fire onListItemClick in Listactivity with buttons in list?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文