ListView控件元素无法点击 [英] ListView element not clickable

查看:126
本文介绍了ListView控件元素无法点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有SimpleCursorAdapter一个ListView。布局是使用的LinearLayout,但阅读有关内存消耗为复杂的(嵌套)布局手册的时候,我切换到RelativeLayout的,而不是和我已经成功地设置布局完全按照我想要的。

I have a ListView with SimpleCursorAdapter. The layout was using a LinearLayout, but when reading manuals about memory consumption for complex (nested) layouts, I switched to RelativeLayout instead and I've managed to setup the layout exactly as I want.

现在,有一个怪癖。在模拟器中,我不能单击列表中的项目。看来,如果元素是一大没有按钮。不过,如果我使用仿真器箭头并选择列表视图元素(高亮显示),然后单击按钮,它工作正常。

Now, there's one quirk. In the emulator, I cannot click the list items. It seems as if the elements are one big "no button". However, if I use the emulator arrows and select the listview element (highlights it) and click the button, it works fine.

我为什么不能点击的列表视图项目,因为我切换到RelativeLayout的?

Why can't I "click" the listview items since I switched to RelativeLayout?

这里的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" >

  <TextView
    android:id="@+id/locationName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Junk text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
    android:id="@+id/numStores"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/locationName"
    android:text="Junk text #1: 117"
    android:textSize="10dp" />

  <TextView
    android:id="@+id/numItems"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/numStores"
    android:text="Junk text #2: 42"
    android:textSize="10dp" />
</RelativeLayout>

我甚至尝试机器人:可点击=真正的的RelativeLayout的,没有用

I even tried android:clickable="true" for RelativeLayout, to no avail.

修改 $ C $下的onClick如下:

EDIT Code for onClick is as follows:

listItems.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    int countyID = cursor.getInt(cursor.getColumnIndex(LocationDB.C_ID));
    int numItems = cursor.getInt(cursor.getColumnIndex(LocationDB.C_NUM_ITEMS));
    int numStores = cursor.getInt(cursor.getColumnIndex(LocationDB.C_NUM_STORES));
    String countyName = cursor.getString(cursor.getColumnIndex(LocationDB.C_NAME));
    String foo = String.format(TAG + "Clicked ID #%d (%s), stores = %d, items = %d", countyID, countyName, numStores, numItems);
    Log.i(LOG_TAG, foo);

    // Show the item in a new activity
    Intent apan = new Intent(avBase, Browser.class);
    apan.putExtra("countyID", countyID);
    startActivity(apan);
  }
});

编辑2

在code现在是测试我的手机,我也得到了同样的错误;使用RelativeLayout的时候不能点击的项目。

The code is now tested on my phone, and I get the same error; can't click the items when using RelativeLayout.

推荐答案

我碰到这样的一些问题,也这似乎是与组合的的ListView S上点击听众和那些我在适配器分配每个单项

I faced some issues with this, too, which seemed to be related to the combination of the ListView's on click listeners and those I assigned each individual item in my Adapter.

在我的 Adapter.getView()我必须以确保项目进行配置,如:

In my Adapter.getView() I had to make sure the items were configured like:

item.setLongClickable( false );
item.setClickable( false );
item.setOnClickListener( null ); 

而我的的ListView 必须有它的 OnItemClickListener 和/或它的 OnItemLongClickListener 设置。

while my ListView had to have its OnItemClickListener and/or its OnItemLongClickListener set.

打开对个别项目的点击的的在列表视图是行不通的。也许这是什么导致你的问题了。

Turning on clicks on the individual items and on the listview would not work. Maybe that's what's causing your issue, too.

这篇关于ListView控件元素无法点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆