【问题标题】:android: extend the listitem click rangeandroid:扩展listitem点击范围
【发布时间】:2014-09-20 01:44:49
【问题描述】:

我在我的 android 应用程序中实现了 ExpanddableListView,但我对子项的点击范围不满意。 OnCLickListener 仅在我完全单击作为我的列表项的字符串时才会触发。

如何将列表项的点击范围扩展到整行?

这是我的 layout-xml “selectreasonlist_item.xml”:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" android:weightSum="1">

<TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17dip"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:clickable="false" android:focusable="false"/>

</LinearLayout>

在我的活动中,我有:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selectreason);
    final Context context = this;

    //Get the ListView
    expListView = (ExpandableListView) findViewById(R.id.xlvSelectReason);

    //Preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader,listDataChild);

    //Setting list adapter
    expListView.setAdapter(listAdapter);

    //Setting OnClickListener to child item
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

       //My Code

            return false;
        }
    });
}

感谢您的帮助 干杯尼克

【问题讨论】:

  • 发现必须配置android:padding才能扩大点击范围。我将 android:paddingRight="130dp" 添加到我的 list_item.xml 布局中。解决了。 ;)

标签: android click range extend listitem


【解决方案1】:

您可以使用setOnGroupClickListenersetOnChildClickListener 将侦听器附加到列表,以将其事件处理给侦听器OnGroupClickListenerOnChildClickListener

【讨论】:

  • 我看到您在 TextView 上将 false 设置为可点击,但是您是否按照您在问题中提到的那样删除了 OnCLickListener?
  • 不,我在 Activity 中设置了 OnChildClickListener。 Override 方法“onChildClick()”返回 false...函数 setChildIndicatorBounds(int, int) 对我有帮助吗?
  • setOnGroupClickListener 和 setOnChildClickListener 的默认行为是覆盖整个行,您可能在其他地方选择点击事件,或者您的列表没有覆盖您想要的整个宽度。
  • 请注意:fill_parent 在 Android 2.2 中已弃用,如果您的目标 api 不低于此值,则应改用 match_parent。
  • 感谢您的信息。我已将活动中的代码添加到线程中 - 如您所见,我有一个自定义适配器类:这会导致问题吗?
【解决方案2】:

我发现我必须配置 android:padding 才能扩大点击范围。 我加了

android:paddingRight="130dp"

到我的 list_item.xml 布局。解决了。 ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多