これまでFragment
がViewPager
から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>
シェアあなたのXMLレイアウト –
あなたは 'ViewPager'の内容をスクロールするには、' ViewPager'外 'ScrollView'を使用している場合は、 'ScrollView'を個々のフラグメントのレイアウトに移動してください。それ以外の理由で 'ViewPager'の外に' ScrollView'を持っているだけの場合は、[fillViewport](https://developer.android.com/reference/android/widget/ScrollView.html#attr_android:fillViewport)属性を'ScrollView'ではtrueです。 –
@NileshRathodが完了しました。 – Sam