I have a ListView
that takes up the entire screen, but sometimes it doesn't have enough items in it to completely cover the ListView
widget. I'd like to be able to respond to clicks on that empty space below the last item that doesn't actually contain an item (meaning setOnItemClickListener
isn't appropriate).
I've tried setOnClickListener
(which they disable for the ListView
itself) on a parent view, but clicks don't seem to be reaching it. I also tried setOnTouchListener
on the ListView
, but that caused some item clicks to not register for some reason. Any ideas on how to accomplish this?
Place an empty linearlayout below the listview by setting an appropriate height to the listview. Place an onClick() method to that linear layout. That must do it.
To get which item was selected, there is a method of the ListView called getItemAtPosition.
Implement ListView 's OnItemClickListener, once you handle this event, try to get the location of the row that was clicked. Once you get it, access that particular row position in the source array (or whatever else you're having). This way, you'll have the data that you want to pass to another activity.
Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.
Ran into similiar problem where I needed to close expanded list elements when empty space is clicked. Here is how I solved it.
public class CustomListView extends ListView {
private OnNoItemClickListener mOnNoItemClickListener;
public interface OnNoItemClickListener {
void onNoItemClicked();
}
public CustomListView(Context context) {
super(context);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//check whether the touch hit any elements INCLUDING ListView footer
if(pointToPosition((int) (ev.getX() * ev.getXPrecision()),
(int) (ev.getY() * ev.getYPrecision())) == -1 && ev.getAction() == MotionEvent.ACTION_DOWN) {
if(mOnNoItemClickListener != null) {
mOnNoItemClickListener.onNoItemClicked();
}
}
return super.dispatchTouchEvent(ev);
}
public void setOnNoItemClickListener(OnNoItemClickListener listener) {
mOnNoItemClickListener = listener;
}
}
Then use this CustomListView in your XML file instead of regular ListView and implement OnNoItemClickListener inside your activity / fragment and call mListView.setOnNoItemClickListener(this); in onCreate to enable callback to onNoItemClicked() function.
Note that if your ListView has headers or footers then these are considered as list elements and the onNoItemClicked() won't be called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With