如何解决android动态滚动视图中viewPager的高度
我在滚动视图中使用视图分页器,并且在具有动态内容的不同片段中加载了视图分页器。我遵循了许多来自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();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。