【问题标题】:Scrolling vertically and paging horizontally in a ViewPager在 ViewPager 中垂直滚动和水平分页
【发布时间】:2017-06-21 14:10:52
【问题描述】:

我有许多页面,其内容大于屏幕,即用户必须向下滚动才能看到更多内容。这些页面位于 ViewPager 中。对于我的生活,我无法弄清楚如何使垂直滚动工作。

为了清楚起见,我不想垂直翻页到另一个页面,只需在单个页面内向下滚动即可。

我希望有人能指出我正确的方向。搜索不会产生任何结果,但如果这不是常见的事情,我会感到惊讶吗?

我尝试过的:

  1. 围绕包含视图分页器和其他视图的线性布局包裹滚动视图。
  2. 布局和布局参数的不同组合。

谢谢

根据建议,我从 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 标签未关闭。
  • 抱歉复制不正确,其实是正确关闭的
  • 相对布局相对于彼此排列视图组。您可以将文本排列在另一个文本视图的左侧 在您的示例中,一个简单的解决方案是使用线性布局。因此,每个文本视图都排列在彼此的底部 [阅读更多] (androidexample.com/Relative_Layout_Basics_-_Android_Example/…)

标签: android android-layout android-viewpager


【解决方案1】:

在 viewpager 的片段中使用 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/

【讨论】:

  • 谢谢。我设法通过使用 NestedScrollView 和 CoordinaterView 来完成这项工作。
【解决方案2】:

您是否尝试过使用片段?使用滚动视图中的页面布局创建一个单独的片段 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)来填充视图分页器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多