首先,我搜索了很多,但解决方案没有用,所以我来这里寻求帮助。使用的设计库是23.1.1
// MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
// main.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">
<fragment
class = "com.tablayoutfragmenttest.FragmentTest"
android:id="@+id/hahah"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
//片段
public class FragmentTest extends Fragment {
private TabLayout mTablayout;
private ViewPager mVp;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment, container, false);
mTablayout = (TabLayout)v.findViewById(R.id.bizfrag_tabs_id);
mTablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
mVp = (ViewPager)v.findViewById(R.id.bizfrag_vp_id);
mVp.setAdapter(new MyAdapter(getChildFragmentManager()));
mVp.setOffscreenPageLimit(0);
//mTablayout.setupWithViewPager(mVp);
//mTablayout.post(new Runnable() {
// @Override
// public void run() {
// if (ViewCompat.isLaidOut(mTablayout)) {
// mTablayout.setupWithViewPager(mVp);
// } else {
// mTablayout.addOnLayoutChangeListener(
// new View.OnLayoutChangeListener() {
// @Override
// public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
// mTablayout.setupWithViewPager(mVp);
//
// mTablayout.removeOnLayoutChangeListener(this);
// }
// });
// }
// }
// });
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mTablayout.post(new Runnable() {
@Override
public void run() {
mTablayout.setupWithViewPager(mVp);
}
});
}
}
// fragment.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/bizfrag_tabs_id"
android:layout_width="match_parent"
android:layout_height="40dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/bizfrag_vp_id"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
// ViewPager的适配器
public class MyAdapter extends FragmentStatePagerAdapter {
public static String[] totalPossibleTabs = new String[]{"this is tab0", "this is tab1", "this is tab2", "this is tab3", "this is tab4", "this is tab5", "this is tab6", "this is tab7", "this is tab8", "this is tab9"};
public MyAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int position) {
return new Fragment();
}
@Override
public int getCount() {
return totalPossibleTabs.length;
}
@Override
public CharSequence getPageTitle(int position) {
return totalPossibleTabs[position];
}
}
当我运行应用程序并滑动页面时,问题如下: the tab's indicator is in wrong position
答案 0 :(得分:0)
试试这个: -
if (ViewCompat.isLaidOut(tabLayout)) {
mTablayout.setupWithViewPager(viewPager);
} else {
tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(...) {
mTablayout.setupWithViewPager(viewPager);
mTablayout.removeOnLayoutChangeListener(this);
}
});
}
而不是: -
mTablayout.post(new Runnable() {
@Override
public void run() {
mTablayout.setupWithViewPager(mVp);
}
});
答案 1 :(得分:0)
谢谢大家,我终于明白了这一点, 我们就是无法使用
new Fragment();
在这种情况下为简单起见, 把它改成你自己的片段。