2017-08-19 4 views
2

これまでFragmentViewPagerからScrollViewに表示されていました。問題は、正確な高さをViewPagerにする必要があるためです。 WRAP_CONTENTまたはMATCH_PARENTに頼ることはできません。私はhereのコードで解決しました。今、私の問題は、Fragmentの高さがフルスクリーンではない場合、私は画面の空の部分でViewPagerをスライドできません。私はFragmentの目に見える部分でのみスライドできます。以下はコードです。ScrollView内のViewPagerの高さをMATCH_PARENTにするにはどうすればいいですか?

public class CustomPager extends ViewPager { 

    private View mCurrentView; 

    public CustomPager(Context context) { 
     super(context); 
    } 

    public CustomPager(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     if (mCurrentView == null) { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
      return; 
     } 
     int height = 0; 
     mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
     int h = mCurrentView.getMeasuredHeight(); 
     if (h > height) height = h; 

     //TODO: if (height < empty space height of a screen) height = empty space height of a screen 

     heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 

    public void measureCurrentView(View currentView) { 
     mCurrentView = currentView; 
     requestLayout(); 
    } 

    public int measureFragment(View view) { 
     if (view == null) 
      return 0; 

     view.measure(0, 0); 
     return view.getMeasuredHeight(); 
    } 
} 

layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroll_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

     <TextView 
      android:id="@+id/text1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/text2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/text3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <com.example.CustomPager 
      android:id="@+id/custom_pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 
</ScrollView> 
+0

シェアあなたのXMLレイアウト –

+0

あなたは 'ViewPager'の内容をスクロールするには、' ViewPager'外 'ScrollView'を使用している場合は、 'ScrollView'を個々のフラグメントのレイアウトに移動してください。それ以外の理由で 'ViewPager'の外に' ScrollView'を持っているだけの場合は、[fillViewport](https://developer.android.com/reference/android/widget/ScrollView.html#attr_android:fillViewport)属性を'ScrollView'ではtrueです。 –

+0

@NileshRathodが完了しました。 – Sam

答えて

0

それは私の場合には 男性のあなたのcom.example.CustomPager

<com.example.CustomPager 
     android:id="@+id/custom_pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

とあなたの断片では親のレイアウト

として NestedScrollViewを作る働き、これを試してください
<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/scrollview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    tools:context="com.example.Fragment"> 

と作るあなたのScrollViewandroid:fillViewport="true"

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="match_parent" 
    android:fillViewport="true" 
    android:layout_height="match_parent"> 
+0

それは私のために働いていない。 'Fragment'が表示されますが、空の部分をスライドするとスライドできません。 – Sam

+0

@Samはアップデートされたansを確認しました –

+0

まだ動作していません – Sam

1

は、このソリューションをお試しください。

アンドロイド:fillViewport = "true" を

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scroll_view" 
android:layout_width="match_parent" 
android:fillViewport="true" 
android:layout_height="match_parent"> 

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

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/text3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <chatapp.com.testdemo.CustomPager 
     android:id="@+id/custom_pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

+0

真剣に、前に 'android:fillViewport =" true "'を入れようとしましたが、動作しません。それは何も変わりません。 – Sam

関連する問題