88722

Make part of Listview`s Header-/FooterView selectable

Question:

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?

Answer1:

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

<ol><li>android:descendantFocusability="afterDescendants" for header and footer ViewGroups</li> <li>setItemsCanFocus(true) for the whole ListView (you may want to set android:focusable="false" and android:focusableInTouchMode="false" for the row ViewGroup</li> </ol>

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

Recommend

  • Blackberry UI showing images at bottom
  • How to make imageView clickable from OnItemClickListener?
  • Sharing Violation on image View and Edit WPF
  • Is there a way to programmatically set src image for ToggleButton?
  • Image Map-like Blackberry Control - CLDC Application
  • The size of the buttons depending on the screen resolution
  • How to notify parent's view model when child's view model property is changed?
  • Spinner with border
  • Android, Custom Notification, Giving me always white Text
  • Sticky Footer Below ListView in Android Layout
  • How to automatically pop-up keyboard?
  • EditText ignores the ImeActionLabel
  • Toolbar shifts up, when soft keyboard appears
  • Gray line inside cardview :)?
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • How do I alternate colors in Flat List (React Native)
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Android fill_parent issue
  • AES padding and writing the ciphertext to a disk file
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • jquery mobile loadPage not working
  • Web-crawler for facebook in python
  • AT Commands to Send SMS not working in Windows 8.1
  • Unanticipated behavior
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • trying to dynamically update Highchart column chart but series undefined
  • apache spark aggregate function using min value
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal