我有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>
问题是,在非触摸模式下,只有整个页脚被选中。如您所见,我已尝试通过descendantFocusability
和focusable
属性操纵选择行为。没有成功。在touchmode中,点击第一个LinearLayout
即可。
我还尝试将第三个addFooterView
参数设置为false。
如何只选择页脚视图的子元素?
答案 0 :(得分:3)
您还应该在ListView上调用setItemsCanFocus(true)。所以实际上有两件事需要做:
您可能还需要为您需要关注的元素设置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;
}