I've an activity and inside it a list fragment. In case I receive no elements for the list i'd like to add an onclick listener on the whole layout to retry the request. Activity layout relative_browse:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_browse"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
Then inside the onCreateView of the fragment i inflate fragment_list_categoriers.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_list_categories"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/gray"
android:dividerHeight="1dp"
android:listSelector="@android:color/transparent" >
</ListView>
</RelativeLayout>
I tried to add the onclick listener on the empty list inside onActivityCreated, but it doesn't work:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = container.findViewById(R.id.fragment_list_categories);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.w("DEBUG", "Click!");
}
});
}
In particular I've no "Click" log, except for a tiny border of the screen on the left (it's like if the fragment_list_categories cover all the screen except some pixels). What's the right way to do this?
EDIT: here my onCreateView and onClick method
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.inflater = inflater;
View view = inflater.inflate(R.layout.fragment_list_categories,
container, false);
this.container = (ViewGroup) view;
view.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
Log.d("DEBUG", "Click!");
}
The first thing that pops out at me is that the ListView
is taking up all the space.
Since you have its height set to match_parent
, it will always occupy the entire parent's space. If the list is empty, this is probably not the desired behavior.
ListViews are sometimes weird about propagating clicks through to their parent views. When you click it, it's probably just dead-ending there instead of going up the chain. That would explain why you can get the clicks on the "border"; it's just the parent view's padding.
You may want to consider changing the ListView
height to wrap_content
. That way if it's empty, it won't take up space and you can get your clicks.
You may also want to try to detect if it's actually empty. If you have a couple entries that don't take up the full height of the parent, what is the expected behavior if the user clicks in the "empty" area? If that should trigger the retry as well, that's fine. If not, you need to enable/disable clicking based on that.