【问题标题】:Problems with click listener for header in a ListViewListView 中标题的单击侦听器问题
【发布时间】:2014-04-08 11:52:10
【问题描述】:

所以,我有一个如下定义的标题视图:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">

    <TextView android:id="@+id/purchase_menu_header_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="20sp"
        android:text="@string/purchase_options_label"/>

</LinearLayout>

膨胀如下:

View header = ((Activity) context).getLayoutInflater().inflate(R.layout.purchase_menu_header, options, false); options.addHeaderView(header);

我对选项 ListView 中的项目有一个点击监听器:

options.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Offer offer = (Offer) options.getItemAtPosition(position);
                initiatePurchase(offer.getStoreProduct());
            }
        });

以及标题视图本身的点击监听器:

header.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                baseLayout.removeView(background);
                baseLayout.removeView(overlay);
            }
        });

我的问题是标题只记录对标题文本部分的点击。期望的行为是让整个标题可点击。我已经尝试过使用 setOnItemClickListener 和 getItemAtPosition(0) 等解决方案,但这会返回 null。还尝试使用 MATCH_PARENT 扩展标题中的文本视图,但没有运气。

【问题讨论】:

  • 您是否尝试过删除 LinearLayout,即您的布局文件是否只包含 TextView?然后,您可以调整 TextView 的重力和/或填充以获取所需的文本。
  • 我也会在标题中添加一张图片,所以我认为这个解决方案不会很好。

标签: android android-layout android-listview


【解决方案1】:

尝试使用 android:clickable 属性使您的 LinearLayout 可点击。

【讨论】:

  • 尝试通过 setClickable (true) 以编程方式使标题视图可点击。
  • 奇怪的是,程序化的方式实际上奏效了。作为记录,另一种解决方案是在标题视图中创建一个空白视图(我使用了一个 ImageView),其中 match_parent 宽度/高度。谢谢网络山姆!
  • 不客气。不幸的是,我从经验中了解到,最外层布局视图的许多属性往往被忽略。
【解决方案2】:

我的问题是标题只记录对文本的点击 标题的一部分。

因为您的标题只是 TextViewwrap_content 作为宽度和高度。因此,您将clicklistener 应用到TextView 并限制可点击区域。

解决方案:

  1. 使用match_parent 作为TextView 的宽度。或者
  2. 使用其中包含TextViewLinearLayout 作为标题并使其可点击。

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多