我有许多页面,其内容大于屏幕,即用户必须向下滚动才能看到更多内容。 这些页面位于 ViewPager 中。 对于我的一生,我不知道如何使垂直滚动工作。
需要明确的是,我不想垂直翻页到另一个页面,只是在单个页面内向下滚动。
我希望有人能为我指出正确的方向。 搜索不会产生任何结果,但如果这不是一件常见的事情,我会感到惊讶?
我尝试过:
将- 滚动视图环绕在包含视图寻呼器和其他视图的线性布局周围。 布局
- 和布局参数的不同组合。
谢谢
根据建议,我从AndroidHive中获取了示例,因此我的片段xml如下所示,它有足够的内容不适合屏幕。 因此,我希望能够像列表视图一样向下滚动以查看其余内容。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text3"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text4"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text5"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text6"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text7"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text8"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="@+id/text10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text9"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
我没有从示例代码中更改任何其他内容。
使用 Scrollview 可以查看页面中的片段。
viewpager 中的片段应提供所需的水平分页,片段内的滚动视图提供垂直分页。
片段一
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OneFragment extends Fragment{
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
片段一的布局
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/one"
android:textSize="40dp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
</RelativeLayout>
</ScrollView>
Androidhive有一个简单的教程http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
你试过使用片段吗?在滚动视图中使用页面布局创建一个单独的片段 XML 文件...
<!-- fragment_layout.xml -->
<ScrollView
android:layout_width='match_parent'
android:layout_height='match_parent'
...>
<WhateverYourPageLayoutIs
android:layout_width='match_parent'
android:layout_height='wrap_content'
.../>
</ScrollView>
然后创建一个使用此布局的 Fragment 子类,并使用 FragmentPagerAdapter(或 FragmentStatePagerAdapter)来填充视图页。