Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you listen to click events on a ListView that occur outside of the list items?

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?

like image 274
Foxichu Avatar asked Aug 30 '13 00:08

Foxichu


People also ask

How can you react to click events on an item of a ListView?

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.

Which method is used to obtain the selected option from a ListView?

To get which item was selected, there is a method of the ListView called getItemAtPosition.

How pass data from ListView to another activity in Android?

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.

What is the use of ListView explain list view with example?

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.


1 Answers

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.

like image 67
Torsten Ojaperv Avatar answered Oct 28 '22 05:10

Torsten Ojaperv