2015-07-10 53 views
2

我想創建兩個ViewPager內部滾動查看佈局。第一個將高度設置爲所需的大小,第二個應與其餘佈局匹配。問題是第二個ViewPager不可見。我究竟做錯了什麼?這是我的佈局:兩個ViewPagers裏面滾動查看 - 第二不可見

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

      <android.support.v4.view.ViewPager 
       android:id="@+id/gallery" 
       android:layout_width="match_parent" 
       android:layout_height="200dp" /> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/content" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:orientation="vertical" /> 
     </LinearLayout> 
    </ScrollView> 

</FrameLayout> 

我使用第二ViewPager顯示片段(使用FragmentPagerAdapter),這是我的FragmentPagerAdapter代碼:

public class MainPagerAdapter extends FragmentPagerAdapter { 
    public MainPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
     case 0: 
      return new FirstFragment(); 
     case 1: 
      return new SecondFragment(); 
     default: 
      return new FirstFragment(); 
     } 
    } 

    @Override 
    public int getCount() { 
     return 2; 
    } 
} 

代碼FirstFragment的:

public class FirstFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.first_step, container, false); 
    } 
} 

first_step.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="400dp" 
     android:background="@android:color/holo_blue_light" /> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="400dp" 
     android:background="@android:color/holo_green_light" /> 
</LinearLayout> 

回答

0

更改

<android.support.v4.view.ViewPager 
       android:id="@+id/content" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:orientation="vertical" /> 

<android.support.v4.view.ViewPager 
       android:id="@+id/content" 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:orientation="vertical" /> 

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
+0

一切都沒變,只有第一個是可見的。 – user3626048

+0

我編輯了我的文章。問題是你把一個帶有「match_parent」高度的LinearLayout放到一個ScrollView中。 – Gordak

+0

感謝您的回覆。當我將第二個ViewPager的高度設置爲某個尺寸時,它就可以看到,但我想將它匹配到佈局的其餘部分。 – user3626048

0

如果您使用的佈局重量。你必須0dp相應地設置的寬度或高度佈局您使用

這給出相等的空間來同時尋呼機 得到重量比即2:1或2:1等

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <android.support.v4.view.ViewPager 
      android:id="@+id/gallery" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/content" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="vertical" /> 
    </LinearLayout> 
+0

當我這樣做時,沒有任何viewPagers可見。 – user3626048

+0

必須設置圖像或您的數據到它..?..你可以告訴我你的java代碼..嘗試設置背景顏色,以防你正在檢查.. – abhishek

+0

我更新了我的帖子與代碼,檢查出來。 – user3626048

0

必須定義

android:layout_height="200dp" 

刪除

android:layout_weight="1" 

後兩者viewpager可見不設置android:layout_height =「wrap_content」

+0

沒有任何改變。 – user3626048

+0

爲什麼你在這個佈局中使用ScrollView,請解釋.. –

+0

Second ViewPager顯示Fragments(使用FragmentPagerAdapter)佈局很長,所以用戶必須能夠滾動它。 – user3626048

0

在ScollView中設置fillViewport = true。

我認爲ScollView是沒用的,如果第二個ViewPager沒有固定在屏幕上的高度。

<ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true"> 
+0

謝謝,現在看起來不錯,但我不能滾動,你知道有什麼問題嗎? – user3626048

+0

嘗試使第二個ViewPager的高度更高,如1000dp。 – hoi

+0

當我設置高度,那麼它是可滾動的,但我ViewPager中的片段可能有不同的高度,所以我不能這樣做。 – user3626048

0
case ViewPager is a puzzle view,you must set the height for it,then it can 
appear in you UI Screen,gallery dont have this problem; 
why use ScrollView in this layout, i dont understand. 
but,if you use linearlayout 

所以你可以做這樣的背景只是爲了測試,你應該得到的命運在你的項目相匹配的屏幕,不只是200 * 2:

layout: 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 


    <android.support.v4.view.ViewPager 
       android:id="@+id/content1" 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:background="@android:color/darker_gray" 
       android:orientation="vertical" /> 


    <android.support.v4.view.ViewPager 
       android:id="@+id/content2" 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:background="#ff0000" 
       android:orientation="vertical" /> 
</LinearLayout> 

activity: 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.viewpage01); 
     int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); 
     ViewPager content = (ViewPager) findViewById(R.id.content2); 
     content.getLayoutParams().height = screenHeight - 200 * 2; 
     content.requestLayout(); 
    }