一个活动中有两个listView

时间:2012-11-28 07:10:22

标签: android listview scroll

我有两个列表视图。

我遇到的问题是屏幕滚动直到较小(高度)列表视图到达底部 如何展开包含视图以便我可以滚动到更长列表视图的底部?

我重载了onScroll以同时滚动两个列表。

    public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {

        super.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        if (view.getChildAt(0) != null) {
            if (view.equals(m_lv1) ){
                m_lv2.setSelectionFromTop(view.getFirstVisiblePosition(),
                    view.getChildAt(0).getTop());
            } else if (view.equals(m_lv2) ){
                m_lv1.setSelectionFromTop(view.getFirstVisiblePosition(),
                    view.getChildAt(0).getTop());
            }
        }
    }


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"  
    android:layout_height="match_parent">

  <ListView
      android:id="@+id/list_view_left"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:scrollbars="none"  
      android:layout_weight="1" >
  </ListView>

  <ListView
      android:id="@+id/list_view_right"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1" >
  </ListView>
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

你必须设置listview的高度......比如。

<ListView
      android:id="@+id/list_view_left"
      android:layout_width="fill_parent"
      android:layout_height="120dp"
      android:scrollbars="none"  
      android:layout_weight="1" >
  </ListView>

  <ListView
      android:id="@+id/list_view_right"
      android:layout_width="fill_parent"
      android:layout_height="120dp"
      android:layout_weight="1" >
  </ListView>

答案 1 :(得分:1)

使用以下XML代码而不是代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">

  <ListView
      android:id="@+id/list_view_left"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1" >
  </ListView>

  <ListView
      android:id="@+id/list_view_right"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1" >
  </ListView>
</LinearLayout>