【发布时间】:2019-07-05 10:03:54
【问题描述】:
在这里,当我到达第一个片段时,所有视图都已加载并滚动,但是当我来自第三个片段时,即来自其他一些活动时,我需要进入第三个片段,并且当导航到第一个片段时,视图未加载,这使得不可滚动. Firstfragment xml 包含 recyclerview 和 Group 元素。
MyActivity 类:
MyAdapter adapter = new MyAdapter(this, getSupportFragmentManager(), fragments);
viewPager.setOffscreenPageLimit(3);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
//MyAdapter
public class MyAdapter extends FragmentPagerAdapter {
private Context mContext;
private List<Fragment> fragmentList ;
public MyAdapter(Context context, FragmentManager fm,List<Fragment> fragmentList) {
super(fm);
mContext = context;
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return mContext.getString(R.string.firstfragmentname);
case 1:
return mContext.getString(R.string.seconsfrag);
case 2:
return mContext.getString(R.string.thirdfrag);
default:
return null;
}
}
}
//XML has custom viewpager
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/headerHeight"
android:layout_marginBottom="@dimen/footerHeight"
android:fadingEdgeLength="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dp_30_60"
android:layout_marginTop="10dp"
android:layout_marginEnd="@dimen/dp_30_60"
android:orientation="vertical">
<TextView
android:id="@+id/sp_header"
style="@style/pageTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="@string/my_bill" />
<com.view.Tab
android:id="@+id/tab_custom"
android:layout_width="match_parent"
android:layout_height="@dimen/tab_height" />
<com.custom.CustomViewPager
android:id="@+id/sp_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/dp_10_53"
android:layout_marginTop="4dp"
android:layout_marginEnd="@dimen/dp_10_53"
android:layout_marginBottom="4dp" />
</LinearLayout>
</ScrollView>
//CustomViepager
public class CustomViepager extends ViewPager {
public CustomViepager(Context context) {
super(context);
initPageChangeListener();
}
public CustomViepager(Context context, AttributeSet attrs) {
super(context, attrs);
initPageChangeListener();
}
private void initPageChangeListener() {
addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
requestLayout();
}
});
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
View child = getChildAt(getCurrentItem());
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public void setCurrentItem(int item) {
super.setCurrentItem(item, false);
}
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
super.setCurrentItem(item, false);
}
}
【问题讨论】:
-
使用 FragmentStatePagerAdapter 代替 FragmentStateAdapter
-
您需要自定义视图寻呼机
-
@LakhwinderSingh 如果我不使用带有 onMeasure 覆盖的 CustomViewpager,则无法看到布局,它完全是空白屏幕。
-
如果您不使用自定义视图寻呼机,您认为会发生什么,因为我不明白 onMesuare 的使用,请您指定
标签: android android-fragments android-viewpager fragmentpageradapter