如何将列表视图放在滚动视图中

时间:2012-11-23 09:26:35

标签: android

我有RelativeLayout及以下,我有一个ListView。我必须将它们放在ScrollView内。任何帮助都将深表感谢。

4 个答案:

答案 0 :(得分:2)

你不应该把一个ListView放在ScrollView中,因为ListView类实现了自己的滚动,它只是不接收手势,因为它们都由父ScrollView处理

答案 1 :(得分:0)

ScrollView只能为单个Layout设置。因此,将您想要滚动的widgets放在单个layout中,如下所示:

<ScrollView
    android:id="@+id/scrView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     >

    <RelativeLayout
        android:id="@+id/rltvLyt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/lstView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="20sp" />
    </RelativeLayout>
</ScrollView>

答案 2 :(得分:0)

不要这样做。你到底想要达到什么目的?这里(以及其他地方)已经提到过这不是一个好主意,因为滚动机制会因为它们都想滚动而混淆。然而,忽略你有一个庞大的列表视图然后你错过了使用listview的主要性能提升,并且可能会使用表格布局并添加行。“

答案 3 :(得分:0)

您不应该将ListView放在ScrollView中,因为ListView类实现了自己的滚动,它只是不接收手势,因为它们都由父ScrollView处理。我强烈建议你以某种方式简化你的布局。例如,您可以将要滚动的视图添加到ListView作为页眉或页脚。

private void setListViewScrollable(final ListView list) {
        list.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                listViewTouchAction = event.getAction();
                if (listViewTouchAction == MotionEvent.ACTION_MOVE)
                {
                    list.scrollBy(0, 1);
                }
                return false;
            }
        });
        list.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view,
                    int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if (listViewTouchAction == MotionEvent.ACTION_MOVE)
                {
                    list.scrollBy(0, -1);
                }
            }
        });
    }

listViewTouchAction是一个全局整数值。如果你可以替换

 list.scrollBy(0, 1);