如何使用ScrollView与一个RelativeLayout和两个LinearLayout?

时间:2016-11-09 09:48:51

标签: android scrollview android-relativelayout

我已经实施了ScrollView,其中我添加了RelativeLayout。在此RelativeLayout中,我添加了两个LinearLayout。其中每个都包含TextViewListView。每个ListView包含超过25个项目。我想显示两个列表视图的所有项目,并为两者都有一个滚动。现在,如果我添加第一个ListView只有3个项目,而在第二个25个项目中,第一个没有滚动,只有第二个获得滚动。如果我添加第一个ListView 25个项目,并且在第二个3个项目中,第一个项目会滚动,第二个项目甚至不会显示。我怎样才能做到这一点?这是我的代码:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/linearLayout1">

            <TextView
                android:text="title1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/productsToBuyTitle" />

            <com.example.alexm.shoppinglist.dlv.DragSortListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/dragSortList1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/linearLayout2"
            android:layout_below="@id/linearLayout1">

            <TextView
                android:text="title2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/allProductsTitle" />

            <com.example.alexm.shoppinglist.dlv.DragSortListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/dragSortList2" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

提前致谢!

4 个答案:

答案 0 :(得分:1)

制作固定高度的列表视图。让我们说250dp,然后在您的活动中为每个列表视图添加此代码:

mListView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mScrollView.requestDisallowInterceptTouchEvent(true);
           int action = event.getActionMasked();
            switch (action) {
                case MotionEvent.ACTION_UP:
                    mScrollView.requestDisallowInterceptTouchEvent(false);
                    break;
            }
            return false;
        }
    });

如果上面的代码不起作用,请使用:

mListView.setOnTouchListener(new View.OnTouchListener() {
        // Setting on Touch Listener for handling the touch inside ScrollView
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Disallow the touch request for parent scroll on touch of child view
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

这样可以避免scrollview阻止列表视图上的触摸事件。

答案 1 :(得分:0)

尝试此布局

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:id="@+id/linearLayout1">

        <TextView
            android:text="title1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/productsToBuyTitle" />

        <com.example.alexm.shoppinglist.dlv.DragSortListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/dragSortList1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:text="title2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/allProductsTitle" />

        <com.example.alexm.shoppinglist.dlv.DragSortListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/dragSortList2" />
     </LinearLayout>
  </LinearLayout>
</ScrollView>

答案 2 :(得分:0)

我已将屏幕分成两部分...希望它现在可以滚动

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/linearLayout1">

            <TextView
                android:text="title1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/productsToBuyTitle" />

            <com.example.alexm.shoppinglist.dlv.DragSortListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/dragSortList1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/linearLayout2"
            android:layout_below="@id/linearLayout1">

            <TextView
                android:text="title2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/allProductsTitle" />

            <com.example.alexm.shoppinglist.dlv.DragSortListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/dragSortList2" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

答案 3 :(得分:0)

我认为,不仅在滚动方面,而且在考虑性能方面,更好的方法是使用单个列表视图来扩展所有这些布局。你只需要一个卷轴来处理,你的表现也会得到改善;滚动列表视图与scrollview不是一个好主意,滚动可能会变得棘手,这样,你的列表视图只会加载可见的单元格(耶,甚至更好的性能)。

因此,您将在该布局中定义ListView并使用适配器为每个视图(TextView和列表内容,即单元格)充气。

我相信这可以解决你的问题。如果您需要代码,请告诉我,我会更新答案