Gingerbread123321
Gingerbread123321

Reputation: 249

ListView OnClickListener

Here is my problem:

I have a chatapplication and the messages are displayed in a ListView. The ListView fills a specific part of the screen. If the user clicks on the ListView a dialog for input should be displayed. My problem is that I can only recognize clicks on the ListView with an onItemClickListener, but when the app starts there are no items to click on in the ListView.

I thought about a button upon the ListView:

<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_marginTop="10dip"
>

  <ListView 
  android:layout_height="fill_parent" 
  android:layout_width="fill_parent" 
  android:id="@+id/ver_list" 
  android:stackFromBottom="true"
  android:cacheColorHint="#00000000"
  android:transcriptMode="alwaysScroll">
  </ListView>

  <Button 
   android:layout_height="fill_parent" 
   android:layout_width="fill_parent" 
   android:id="@+id/ver_listbutton" 
   android:background="@null"></Button> </FrameLayout>

Now I can recognize clicks on the space of the ListView with the Button upon it.

But with the FrameLayout I can not scroll the ListView anymore, because it is below the Button.

Does anyone have a solution for this?

Upvotes: 0

Views: 660

Answers (2)

Andrey Kolesnikov
Andrey Kolesnikov

Reputation: 56

listView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ListView list = (ListView) v;
            View child = list.getChildAt(list.getLastVisiblePosition());
            if (child != null) {
                int[] coords = new int[2];
                child.getLocationOnScreen(coords);
                if (event.getRawY() > coords[1] + child.getHeight()+20) {
                    //DO YOU STUFF
                    Toast.makeText(activity, "Outsize", Toast.LENGTH_SHORT).show();
                    return true;//you have handled the click, listview will not handle it
                }
            }

            return false; // listView handles click
        }
    });

Upvotes: -1

Qiang Jin
Qiang Jin

Reputation: 4467

You can set the button as listview's empty view via,

public void setEmptyView(View emptyView)

When listview is empty, the empty view will be displayed and handle the click event, when there are items in listview, ehe empty view will dispear, and then listview can handle click events.

Upvotes: 3

Related Questions