2017-06-21 72 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)來填充視圖尋呼機。