android.support.design.widget.TabLayout与ViewPager无法正常工作 如果我设置tabLayout.setupWithViewPager(viewPager);然后tablayout视图没有显示请找到附加的屏幕截图。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="false"
android:orientation="vertical"
tools:context=".home.HomeActivity"
tools:showIn="@layout/activity_home">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
片段代码:
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); tabLayout.addTab(tabLayout.newTab().setText(getActivity().getResources().getText(R.string.feeds))); tabLayout.addTab(tabLayout.newTab().setText(getActivity().getResources().getText(R.string.following))); tabLayout.addTab(tabLayout.newTab().setText(getActivity().getResources().getText(R.string.you)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager);
final PageAdapter adapter = new PageAdapter(getChildFragmentManager(), 3);
viewPager.setOffscreenPageLimit(3);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
删除“tabLayout.setupWithViewPager(viewPager);”时视图工作正常。但是当我点击相应的标签时,我需要浏览相应的片段。
答案 0 :(得分:6)
在我的情况下,我通过替换来解决这个问题,
tabLayout.setupWithViewPager(viewPager);
通过,
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});
我希望这会帮助你。
答案 1 :(得分:2)
短版解决方案(Naveen Kumar M):
viewPager.addOnPageChangeListener(
new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
答案 2 :(得分:2)
如果您在片段内使用tabLayout,请尝试此答案
可能的错误
您需要将getChildFragmentManager()传递给FragmentPagerAdapter
不是getSupportFragmentManager()或getFragmentManager()
//wrong one
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
//correct one
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
ViewPagerAdapter是FragmentPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
}
答案 3 :(得分:0)
app:layout_behavior="@string/appbar_scrolling_view_behavior"
同时保持父级作为协调员布局,然后只有滚动才能正常工作
请参阅此处:http://coderzpassion.com/working-appbarlayout-like-whatsapp/
答案 4 :(得分:0)
检查getCount()。 不要把它归还给我!返回到blankFragment。
package my.package;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class tabPagers extends FragmentStatePagerAdapter {
String[] titles = new String[]{"Home", "Campus", "Course"};
public tabPagers(FragmentManager fm) {
super(fm);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public Fragment getItem(int position) {
BlankFragment blankFragment = new BlankFragment();
Blank2Fragment blank2Fragment = new Blank2Fragment();
Blank3Fragment blank3Fragment = new Blank3Fragment();
switch (position) {
case 0:
return blankFragment;
case 1:
return blank2Fragment;
case 3:
return blank3Fragment;
}
return blankFragment; //This one is important
}
@Override
public int getCount() {
return titles.length; //This one is important too
}
}
答案 5 :(得分:0)
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</androidx.viewpager.widget.ViewPager>