我正在使用:
activity
布局{/ 1}}。fragment
带有视图寻呼机,所有内容都在FragmentStatePagerAdapter
内。问题是:
为此,我使用了
ScrollView
和
viewpager.getLayoutParams().height = 20000;
viewpager.requestLayout();`
另外,我尝试过查看寻呼机并将寻呼机片段高度属性视为ViewGroup.LayoutParams params = viewPagerBottom.getLayoutParams();
params.height = 20000;
viewPagerBottom.setLayoutParams(params);
wrap_content
设置scrollview android:layout_height="match_parent"
,则看不到视图寻呼机内容。是父片段layout.xml。
android:fillViewport="true"
答案 0 :(得分:5)
您可以自定义ViewPager
,以便在answer页面滑动时将ViewPager
调整为当前页面大小。
您可以使用以下代码:
public class WrapContentViewPager extends ViewPager {
private int mCurrentPagePosition = 0;
public WrapContentViewPager(Context context) {
super(context);
}
public WrapContentViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;
if (wrapHeight) {
View child = getChildAt(mCurrentPagePosition);
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
}
} catch (Exception e) {
e.printStackTrace();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void reMeasureCurrentPage(int position) {
mCurrentPagePosition = position;
requestLayout();
}
}
在xml:
中声明它 <your.package.name.WrapContentViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</your.package.name.WrapContentViewPager>
在页面滑动后调用reMeasureCurrentpage
功能。
final WrapContentViewPager wrapContentViewPager = (WrapContentViewPager) findViewById(R.id.view_pager);
wrapContentViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
wrapContentViewPager.reMeasureCurrentPage(wrapContentViewPager.getCurrentItem());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
注意:ListView
中可能有数百行。将wrap_content
用于ListView
并不是一个好主意。您需要为ListView
指定固定高度,否则您需要使用match_parent
。