使用片段滚动活动就好像一个长页面

时间:2016-02-16 03:02:44

标签: android listview android-fragments android-scrollview

我有一个活动,它包含一些垂直线性布局的片段(都包含列表视图)。现在,每个列表本身都是可滚动的,但我想要的是整个活动滚动,好像一切都只是一个长页面。

我尝试在容器周围添加一个滚动视图,我将这些片段添加到,检查似乎相关的每个属性,但无济于事。我怎样才能做到这一点?

enter image description here

活动布局的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout (...)
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:fillViewport="true"
        android:touchscreenBlocksFocus="true"
        android:importantForAccessibility="yes">

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

    </ScrollView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

public class ListViewForScrollView extends ListView {
    public ListViewForScrollView(Context context) {
        super(context);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs,
    int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    /**
     * rewrite this method to let listview be suitable for scrollview.
     * after that, you can use ListViewForScrollView in ScrollView
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
    MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}