【问题标题】:ListView is not clickable, all widgets in rows are TextViewListView 不可点击,行中的所有小部件都是 TextView
【发布时间】:2011-10-02 17:17:23
【问题描述】:

我有一个简单的行模型和一个片段中的 ListView; ListView 得到正确填充,具有所有正确的尺寸、颜色等。问题是,单击一行不会触发任何事件。

我的片段中的代码是:

MyAdapter mAdapter = new MyAdapter(getActivity().getApplicationContext(), R.layout.list_row, strings);
mList.setAdapter(mAdapter);

mList.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.d("LIST", "Selected item # " + position);
    }
}); 

片段布局(一部分)是:

<ListView
    android:id="@+id/m_list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_margin="20dp"
    android:clickable="true"
    android:longClickable="true">
</ListView>

行布局为:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">

    <TextView
        android:id="@+id/txt_surname"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:text="@string/surname"
        style="@style/ListStyleXLarge"/>

    <!-- 4 more TextView just as the first one -->
</LinearLayout>

风格很简单:

    <style name="ListStyleXLarge">
    <item name="android:textSize">
        @dimen/text_size_large
    </item>
    <item name="android:textStyle">
        bold
    </item>
    <item name="android:textColor">
        @color/list_item_text_color
    </item>
</style>

什么可以解决这个问题?我已经尝试设置

android:clickable="false"
android:focuseable="false"
android:focuseableInTouchMode="false"

到所有的 TextViews,但到目前为止还没有。

【问题讨论】:

标签: android list listview textview row


【解决方案1】:

在您的 MyAdapter 类中:

在getView方法中使用:

convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Log.d("LIST", "Selected item # " + position);

            }
        });

【讨论】:

    【解决方案2】:

    在您的 list_item xml 中,将 android:descendantFocusability="blocksDescendants" 放入您拥有的 LinearLayout 或 RelativeLayout。

    【讨论】:

      猜你喜欢
      • 2015-11-24
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      相关资源
      最近更新 更多