我在滚动视图中使用视图分页器,并且在具有动态内容的不同片段中加载了视图分页器。我遵循了许多来自Internet的解决方案,但没有一个对我有用。在我的CustomFragmentAdaptor setPrimaryItem 方法中包含两个条件,如果两个条件都为true,则将执行该代码块,第一个条件为true,但第二个条件为false
FragmentAdaptor
public class CustomFragmentAdoptor extends FragmentPagerAdapter {
private Context myContext;
int totalTabs;
private int mCurrentPosition = -1;
public CustomFragmentAdoptor(Context context, FragmentManager fm, int totalTabs) {
super(fm);
myContext = context;
this.totalTabs = totalTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
MeetingFragment homeFragment = new MeetingFragment();
return homeFragment;
default:
return null;
}
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if (position != mCurrentPosition && container instanceof CustomPager) {
//***IMPORTANT - This block of code is not working ---"container instanceof CustomPager"--- i think this line above if condition is creating probelm
Fragment fragment = (Fragment) object;
CustomPager pager = (CustomPager) container;
Toast.makeText(myContext, "Working", Toast.LENGTH_SHORT).show();
if (fragment != null && fragment.getView() != null) {
mCurrentPosition = position;
pager.measureCurrentView(fragment.getView());
}
}
}
@Override
public int getCount() {
return totalTabs;
}
}
CustomPager
public class CustomPager extends ViewPager {
private View mCurrentView;
Context mContext;
public CustomPager(Context context) {
super(context);
mContext = context;
}
public CustomPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mCurrentView != null) {
mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int height = Math.max(0, mCurrentView.getMeasuredHeight());
Toast.makeText(mContext, ""+height, Toast.LENGTH_SHORT).show();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}else{
Toast.makeText(mContext, "Null", Toast.LENGTH_SHORT).show();
}
}
public void measureCurrentView(View currentView) {
mCurrentView = currentView;
requestLayout();
}
}