场景一:在Fragment中使用TabLayout+ViewPager,ViewPager加载不同Fragment,tablayout的Tab显示而Fragment不显示
使用tablayout需导入design库implementation 'com.android.support:design:26.1.0'
- xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
- activity
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout mTablayout;
private List<Fragment> fragmentList;
private String[] title = {"第一", "第二", "第三"};
private MyPagerAdapter2 mAdapter;
private <T extends View> T bindView(int id) {
return (T) findViewById(id);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = bindView(R.id.viewpager);
mTablayout = bindView(R.id.tablayout);
initData();
}
private void initData() {
fragmentList = new ArrayList<>();
fragmentList.add(new OneFragment());
fragmentList.add(new TwoFragment());
fragmentList.add(new ThreeFragment());
mAdapter = new MyPagerAdapter2(getSupportFragmentManager());
mTablayout.setTabMode(TabLayout.MODE_FIXED);//均分屏幕
mTablayout.setTabTextColors(Color.BLACK, Color.RED);//第一个参数为未选中字体颜色,第二个为选中tab字体颜色
mTablayout.setSelectedTabIndicatorColor(Color.RED);//选中tab下划线颜色
mTablayout.setSelectedTabIndicatorHeight(5);//选中tab下划线高度
viewPager.setAdapter(mAdapter);
mTablayout.setupWithViewPager(viewPager);
mTablayout.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) {
}
});
}
private class MyPagerAdapter2 extends FragmentPagerAdapter {
public MyPagerAdapter2(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList != null ? fragmentList.get(position) : null;
}
@Override
public int getCount() {
return fragmentList != null ? fragmentList.size() : 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
// return super.isViewFromObject(view, object);
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
}
代码看上去很正常没毛病,实际结果:tablayout显示而fragment全部不显示
🐎->界面不显示这还了得,赶紧找原因
分析:
tablayout显示,而且title是关联viewpager时添加显示的,说明adapter中数据源应该是没问题的,应该是控制fragment显示的方法出现问题。最终定位到方法isViewFromObject
,重写此方法导致fragment不显示了查看源码发现,它是PagerAdapter里面的一个抽象方法
/**
* Determines whether a page View is associated with a specific key object
* as returned by {@link #instantiateItem(ViewGroup, int)}. This method is
* required for a PagerAdapter to function properly.
*
* @param view Page View to check for association with <code>object</code>
* @param object Object to check for association with <code>view</code>
* @return true if <code>view</code> is associated with the key object <code>object</code>
*/
public abstract boolean isViewFromObject(View view, Object object);
而FragmentPagerAdapter继承自PageAdapter,同时已经实现了该方法
@Override
public boolean isViewFromObject(View view, Object object) {
return ((Fragment)object).getView() == view;
}
在写适配器时,错误重写了方法isViewFromObject
导致fragment不显示
- 解决办法
1、在适配器中删除该方法(推荐,系统已经实现该方法无需冲洗)
2、重写后直接使用父类方法`
@Override
public boolean isViewFromObject(View view, Object object) { return super.isViewFromObject(view, object);}`
3、重写
` @Override
public boolean isViewFromObject(View view, Object object) { return view ==((Fragment) object).getView();}`
//2、3方法不建议,这样做无任何意义
- 说明
在写适配器时,一般来讲直接extends FragmentPagerAdapter
,必须重载方法只有以下三个
/**
* Return the Fragment associated with a specified position.
*/
public abstract Fragment getItem(int position);
/**
* Return the number of views available.
*/
public abstract int getCount();
/**
*constuctor
*/
public FragmentPagerAdapter(FragmentManager fm) {
mFragmentManager = fm;
}
场景二:在Fragment中使用Tablayout+viewpager加载fragment,fragment出现空白情况
- 1、同场景一
- 2、在创建适配器对象时传错参数,在
activity
中使用时创建对象需传入getSupportFragmentManager()
作为参数,在fragment
中使用时需要传入getChildFragmentManager()
作为参数