使 Listview 的 Header-/FooterView 的一部分可选择

发布于 2024-12-04 22:07:25 字数 1128 浏览 1 评论 0原文

我有一个 ListView ,其中通过

list.addFooterView(getLayoutInflater().inflate(R.layout.list_footer_view, null, true), null,true);

list_footer_view.xml 设置了自定义页脚,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:descendantFocusability="afterDescendants" android:focusable="false">
<LinearLayout android:onClick="onProfileClick"
    android:focusable="true" android:clickable="true">
    <TextView android:text="@string/footer_text"
        style="@style/Text" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_weight="1" />
</LinearLayout>
... other views which should not be focusable

 </LinearLayout>

问题是,在非触摸模式下,仅选择整个页脚。正如您所看到的,我已经尝试通过 descendantFocusabilityfocusable 属性来操纵选择行为。没有成功。在触摸模式下,单击第一个 LinearLayout 有效。

我还尝试将第三个 addFooterView 参数设置为 false。

如何仅选择页脚视图的子元素?

I have a ListView with a custom footer set via

list.addFooterView(getLayoutInflater().inflate(R.layout.list_footer_view, null, true), null,true);

list_footer_view.xml looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:descendantFocusability="afterDescendants" android:focusable="false">
<LinearLayout android:onClick="onProfileClick"
    android:focusable="true" android:clickable="true">
    <TextView android:text="@string/footer_text"
        style="@style/Text" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_weight="1" />
</LinearLayout>
... other views which should not be focusable

 </LinearLayout>

The problem is, in non-touch-mode only the whole footer gets selected. As you see I have already tried to manipulate the selection behavior via the descendantFocusability and focusable attributes. Without success. In touchmode clicking the first LinearLayout works.

I have also tried with setting the third addFooterView parameter to false.

How can you select only child-elements of the footer-view?

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

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

发布评论

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

评论(1

翻身的咸鱼 2024-12-11 22:07:25

您还应该在 ListView 上调用 setItemsCanFocus(true)。所以实际上需要做两件事:

  1. android:descendantFocusability="afterDescendants" 对于页眉和页脚 ViewGroups
  2. setItemsCanFocus(true) 对于整个 ListView (您可能需要设置 android:focusable="false" 和 android:focusableInTouchMode=" false” 对于行 ViewGroup

您可能还需要为需要可聚焦的元素设置 focusable="true"。这是一个示例:

poi_details_buttons.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:descendantFocusability="afterDescendants"
  android:orientation="vertical" >

  <View style="@style/Poi.DetailsSeparator" />

  <Button
    android:id="@+id/show_all_comments"
    style="@style/Poi.DetailsItem"
    android:drawableRight="@drawable/icon_forward"
    android:hint="@string/poi_details_show_all_comments"
    android:text="@string/poi_details_show_all_comments" />

  <View style="@style/Poi.DetailsSeparator" />

  <Button
    android:id="@+id/report_bug"
    style="@style/Poi.DetailsItem"
    android:drawableRight="@drawable/icon_forward"
    android:hint="@string/poi_details_report_bug"
    android:text="@string/poi_details_report_bug" />

  <View style="@style/Poi.DetailsSeparator" />

</LinearLayout>

PoiFragment.java:

      protected View createView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    list = (ListView) (ViewGroup) inflater.inflate(R.layout.poi_details, container, false);

    // allow buttons in header/footer to receive focus (comment_row.xml has focusable=false
    // for it's element, so this will only affect header and footer)
    // note: this is not enough, you should also set android:descendantFocusability="afterDescendants"
    list.setItemsCanFocus(true);

...

    View buttonsView = inflater.inflate(R.layout.poi_details_buttons, null);
    buttonsView.findViewById(R.id.show_all_comments).setOnClickListener(this);
    buttonsView.findViewById(R.id.report_bug).setOnClickListener(this);

    list.addFooterView(buttonsView, null, false);

    return list;
  }

You should also call setItemsCanFocus(true) on ListView. So there are actually two things needed to be done:

  1. android:descendantFocusability="afterDescendants" for header and footer ViewGroups
  2. setItemsCanFocus(true) for the whole ListView (you may want to set android:focusable="false" and android:focusableInTouchMode="false" for the row ViewGroup

You may also need to set focusable="true" for your elements you need to be focusable. Here's an example:

poi_details_buttons.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:descendantFocusability="afterDescendants"
  android:orientation="vertical" >

  <View style="@style/Poi.DetailsSeparator" />

  <Button
    android:id="@+id/show_all_comments"
    style="@style/Poi.DetailsItem"
    android:drawableRight="@drawable/icon_forward"
    android:hint="@string/poi_details_show_all_comments"
    android:text="@string/poi_details_show_all_comments" />

  <View style="@style/Poi.DetailsSeparator" />

  <Button
    android:id="@+id/report_bug"
    style="@style/Poi.DetailsItem"
    android:drawableRight="@drawable/icon_forward"
    android:hint="@string/poi_details_report_bug"
    android:text="@string/poi_details_report_bug" />

  <View style="@style/Poi.DetailsSeparator" />

</LinearLayout>

PoiFragment.java:

      protected View createView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    list = (ListView) (ViewGroup) inflater.inflate(R.layout.poi_details, container, false);

    // allow buttons in header/footer to receive focus (comment_row.xml has focusable=false
    // for it's element, so this will only affect header and footer)
    // note: this is not enough, you should also set android:descendantFocusability="afterDescendants"
    list.setItemsCanFocus(true);

...

    View buttonsView = inflater.inflate(R.layout.poi_details_buttons, null);
    buttonsView.findViewById(R.id.show_all_comments).setOnClickListener(this);
    buttonsView.findViewById(R.id.report_bug).setOnClickListener(this);

    list.addFooterView(buttonsView, null, false);

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