gpt4 book ai didi

android - 如何让 ScrollView 中 ViewPager 的高度为 MATCH_PARENT?

转载 作者:搜寻专家 更新时间:2023-11-01 09:32:44 26 4
gpt4 key购买 nike

以前,在 ScrollView 中显示来自 ViewPagerFragment 时遇到问题。问题是因为我们需要为 ViewPager 设置准确的高度。我们不能依赖 WRAP_CONTENTMATCH_PARENT。我用 here 中的代码解决了它.现在,我的问题是,如果 Fragment 的高度不是全屏,我无法在屏幕的空白部分滑动 ViewPager。我只能滑动我的 Fragment 的可见部分。以下是代码。

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>

最佳答案

试试这个解决方案。

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>

关于android - 如何让 ScrollView 中 ViewPager 的高度为 MATCH_PARENT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45767096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com