gpt4 book ai didi

android - BottomSheet 与 ViewPager 和 fragment ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:52 29 4
gpt4 key购买 nike

我想要一个与旧版街景 View 类似的 View 。

refer this

我已尝试将 BottomSheetBehavior 与 ViewPager 结合使用,但其 fragment 的动态高度无法正常工作。

最佳答案

使用 CustomViewPager 我们可以设置 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;
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();
}}

并在适配器中添加也添加这些行

private int mCurrentPosition = -1;

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if (position != mCurrentPosition) {
Fragment fragment = (Fragment) object;
CustomPager pager = (CustomPager) container;
if (fragment != null && fragment.getView() != null) {
mCurrentPosition = position;
pager.measureCurrentView(fragment.getView());
}
}
}

关于android - BottomSheet 与 ViewPager 和 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37582683/

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