0

这是我的问题:

我有一个聊天应用程序,消息显示在 ListView 中。ListView 填充屏幕的特定部分。如果用户单击 ListView,则应显示一个输入对话框。我的问题是我只能通过 onItemClickListener 识别对 ListView 的点击,但是当应用程序启动时,ListView 中没有可点击的项目。

我想到了 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>

现在我可以识别对 ListView 空间的点击,上面有 Button。

但是使用 FrameLayout 我无法再滚动 ListView,因为它位于 Button 下方。

有人对此有解决方案吗?

4

2 回答 2

3

您可以通过将按钮设置为列表视图的空视图,

public void setEmptyView(View emptyView)

当listview为空时,会显示空view并处理click事件,当listview中有item时,空view会消失,然后listview可以处理click事件。

于 2012-12-31T00:30:29.077 回答
-1

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
        }
    });
于 2015-04-24T15:54:55.287 回答