ListFragment OnListItemClick not being called
I encountered the same problem after converting my app from a ListActivity to a ListFragment.
The ListFragment was the only fragment of the activity (no tabs, etc), but the ListFragment was not seeing any of the clicks.
The problem was that my Activity was still defined as a ListActivity. Changed it to a activity fixed it, and now the ListFragment sees the clicks.
public class MyActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
}
should have been:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment class="com.davidmarkscott.myapp.MyFragment"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
If you have an item in your layout that can steal input from other components like a CheckBox, that component needs to be defined as not focusable.
Here is an elegant solution which allows onListItemClick
to fire the way you think it should, and also allows any child views to receive events (or not, depending on how you want to set it.) For the question posed, this would allow for keeping the checkbox subview. Please take care to consider whether this is an intuitive experience for users.
On the root ViewGroup of all list items, set the descendantFocusability
attribute in XML:
android:descendantFocusability="blocksDescendants"
Android SDK documentation of descendantFocusability. Has been part of the SDK since Android 1.0. Other settings for android:descendantFocusability
include "beforeDescendants"
and "afterDescendants"
.
Example cluster_layout.xml which sets descendantFocusability
(List items applied to cluster_list.xml via an ArrayAdapter):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cluster_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#e0e0e0"
android:clickable="false"
android:descendantFocusability="blocksDescendants" >
<FrameLayout
android:id="@+id/cluster_sentiment_handle"
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="@color/handle_red" />
<!-- other elements -->
</LinearLayout>
Example cluster_list.xml, which has no bearing on this solution other than to show that it's intended for use in a ListFragment by having a ListView element with the id @id/android:list:
<?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="match_parent"
android:orientation="vertical"
android:padding="8dp"
android:background="#f6f6f6" >
<include layout="@layout/cluster_header" />
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center_horizontal"
android:gravity="center_horizontal"
android:divider="@android:color/transparent"
android:dividerHeight="8dp"
android:fastScrollEnabled="true" />
</LinearLayout>