本文介绍了让Listview`s报头 - / FooterView可选的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 通过

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

list_footer_view.xml看起来是这样的:

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>

的问题是,在非触摸模式只是整个页脚被选中。正如你看到的我已经试图操纵通过 descendantFocusability 可聚焦属性的选择行为。没有成功。在TouchMode的点击第一个的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.

我也试图与第三 addFooterView 参数设置为false。

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

如何选择页脚视图的独生子女元素?

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

推荐答案

您也应该在调用的ListView setItemsCanFocus(真)。所以实际上需要做两件事情:

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


  1. 的android:descendantFocusability =afterDescendants页眉和页脚ViewGroups

  2. setItemsCanFocus(真)整个的ListView(您可能要设置机器人:可聚焦=假和android:focusableInTouchMode =假,为的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:

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:

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;
  }

这篇关于让Listview`s报头 - / FooterView可选的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 03:21