2017-06-21 120 views
0

我有许多页面,其内容大于屏幕,即用户必须向下滚动以查看更多内容。这些页面在ViewPager中。对于我的生活,我无法弄清楚如何进行垂直滚动工作。垂直滚动和在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> 

我还没有从示例代码中改变任何东西。

+0

您的滚动查看标记未关闭。 –

+0

对不起,它被错误地复制,它实际上正确关闭 – WolfBane

+0

相对布局安排视图组彼此相对。 您可以将文本排列在其他文本视图的左侧 在您的示例中,一个简单的解决方案将使用线性布局。因此,每个文本视图排列在彼此的底部 [阅读更多](http://androidexample。com/Relative_Layout_Basics _-_ Android_Example/index.php?view = article_discription&aid = 73) –

回答

2

使用ScrollView insde视图中的片段。

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/

+0

谢谢。我设法通过使用NestedScrollView和CoordinaterView来得到这个工作。 – WolfBane

0

您是否尝试过使用碎片?与滚动视图内页面布局创建一个单独的片段的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> 

然后创建一个使用此布局的片段子类,并使用FragmentPagerAdapter(或FragmentStatePagerAdapter)来填充视图寻呼机。