我在这里搜索过但没有找到我的解决方案。所以我发布了我的问题,当我们点击选项卡时它的工作正常但是当我们滚动viewPager时它并没有完美地工作。动态高度有一些问题。一些内容没有显示。
我的Xml是
<my_packagename.ViewPager_Height
android:id="@+id/pager"
android:layout_width="match_parent"
android:nestedScrollingEnabled="false"
android:layout_height="wrap_content" />
现在我的java文件就像
public class ViewPager_Height extends ViewPager {
private View mCurrentView;
public ViewPager_Height(Context context) {
super(context);
}
public ViewPager_Height(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();
}
}
和主要类是
final ViewPager_Height viewPager = (ViewPager_Height) findViewById(R.id.pager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(about_us));
tabLayout.addTab(tabLayout.newTab().setText(listing));
tabLayout.addTab(tabLayout.newTab().setText(contact));
DynamicheightTabsAdapter adapter;
adapter = new DynamicheightTabsAdapter(getSupportFragmentManager(), "TASK");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
最后我的Adapter类就像
public class DynamicheightTabsAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
private int mCurrentPosition = -1;
public DynamicheightTabsAdapter(FragmentManager fm, String takidetifier) {
super(fm);
this.fragments = new ArrayList<>();
switch (takidetifier){
case "Task":
fragments.add(new MyFirstFragment());
fragments.add(new MySecondFragment());
fragments.add(new MyThirdFragment());
break;
}
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if (position != mCurrentPosition) {
Fragment fragment = (Fragment) object;
ViewPager_Height pager = (ViewPager_Height) container;
if (fragment != null && fragment.getView() != null) {
mCurrentPosition = position;
pager.measureCurrentView(fragment.getView());
}
}
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}
答案 0 :(得分:0)
在OnTabSelected
中调用adapter.notifyDataSetChanged()
所以新代码将是,
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
adapter.notifyDataSetChanged()
}