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>
Teilen Sie Ihre XML-Layout –
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'. –
@NileshRathod erledigt. – Sam