2017-08-19 5 views
2

Zuvor hatte ich ein Problem mit Fragment von ViewPager in einem ScrollView. Das Problem war, weil wir die genaue Höhe für ViewPager setzen müssen. Wir können uns nicht auf WRAP_CONTENT oder MATCH_PARENT verlassen. Ich löste es mit Code von here. Nun, mein Problem ist, wenn die Höhe von nicht Vollbild ist, kann ich nicht die ViewPager mit dem leeren Teil meines Bildschirms schieben. Ich kann nur mit dem sichtbaren Teil meiner Fragment gleiten. Das Folgende ist der Code.Wie wird die Höhe von ViewPager innerhalb von ScrollView zu MATCH_PARENT gemacht?

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

Teilen Sie Ihre XML-Layout –

+0

Wenn Sie einen 'ScrollView' außerhalb des' ViewPager' den Inhalt der 'ViewPager' blättern, tun Sie das nicht, verschieben Sie die 'ScrollView' in das Layout der einzelnen Fragmente. Wenn Sie aus irgendwelchen anderen Gründen nur eine 'ScrollView' außerhalb des' ViewPager' haben, setzen Sie das Attribut [fillViewport] (https://developer.android.com/reference/android/widget/ScrollView.html#attr_android:fillViewport) auf True auf der 'ScrollView'. –

+0

@NileshRathod erledigt. – Sam

Antwort

0

dies versuchen, es funktioniert in meinem Fall männlich Ihre 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"/> 

und in den Fragmenten NestedScrollView als übergeordnetes Layout machen

<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"> 

und machen Sie Ihre 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

es funktioniert nicht für mich. Das "Fragment" wird angezeigt, aber wenn ich auf dem leeren Teil rutsche, kann ich es nicht verschieben. – Sam

+0

@Sam check updated ans –

+0

Immer noch nicht funktioniert – Sam

1

Diese Lösung Versuchen.

android: 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

Ernsthaft habe ich versucht, 'android: fillViewport =" true "' vor, aber es funktioniert nicht. Es ändert nichts. – Sam

Verwandte Themen