使 activity 的片段像一页长页一样滚动
Make activity with fragments scroll as if one long page
我有一个 activity,它由垂直线性布局中的几个片段(全部包含列表视图)组成。现在,每个列表本身都是可滚动的,但我想要的是整个 activity 滚动,就好像所有内容都只是一个长页面。
我尝试在要添加片段的容器周围添加一个滚动视图,检查每一个似乎相关的 属性,但无济于事。我怎样才能使这项工作?
activity 布局的 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>
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);
}
}
我有一个 activity,它由垂直线性布局中的几个片段(全部包含列表视图)组成。现在,每个列表本身都是可滚动的,但我想要的是整个 activity 滚动,就好像所有内容都只是一个长页面。
我尝试在要添加片段的容器周围添加一个滚动视图,检查每一个似乎相关的 属性,但无济于事。我怎样才能使这项工作?
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>
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);
}
}