We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
public class HomeFragment extends Fragment {
private View viewContent; private XTabLayout tabLayout; private ViewPager viewPager; private BadgeView redDotBadgeView; private BadgeView redNumberBadgeView; private String[] strArray = new String[]{"关注", "推荐", "视频", "直播", "图片", "段子", "精华", "热门"}; private List<String> stringList = new ArrayList<>(); private List<Fragment> fragmentList = new ArrayList<>();
// private ViewPager viewPager; // private XTabLayout topTabLayout; // private BadgeView redDotBadgeView; // private BadgeView redNumberBadgeView; // // private String[] strArray = new String[]{"关注", "推荐", "视频", "直播", "图片", "段子", "精华", "热门"}; // // private List stringList = new ArrayList<>(); // private List fragmentList = new ArrayList<>(); // public static HomeFragment instance() { HomeFragment instance = new HomeFragment(); return instance; }
@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { viewContent = inflater.inflate(R.layout.fragment_home, container, false); initData(); initViews(); return viewContent; } private void initViews() { tabLayout = viewContent.findViewById(R.id.tabsFive); viewPager = viewContent.findViewById(R.id.viewPager); initData(); IndexPagerAdapter indexPagerAdapter = new IndexPagerAdapter(getFragmentManager(),stringList,fragmentList); viewPager.setAdapter(indexPagerAdapter); viewPager.setAdapter(indexPagerAdapter); tabLayout.setupWithViewPager(viewPager); tabLayout.setSelectedTabIndicatorHeight(0); } private void initData() { stringList.addAll(Arrays.asList(strArray)); fragmentList.add(new HomeFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); } class IndexPagerAdapter extends FragmentPagerAdapter { private List<String> titleList; public IndexPagerAdapter(FragmentManager fm, List<String> titleList, List<Fragment> fragmentList) { super(fm); this.titleList = titleList; } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return titleList.size(); } @Override public CharSequence getPageTitle(int position) { return titleList.get(position); } } public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); }
}
NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference
The text was updated successfully, but these errors were encountered:
另 如何在点击tab是设置字体的大小及颜色
Sorry, something went wrong.
fragmentList.add(new HomeFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment()); fragmentList.add(new EmptyFragment());
我这边按照你提供的代码试了下,并没有出现这个问题,你这个异常是因为ViewPager的adapter中getItem方法返回的Fragment为空导致,你可以再check下这块逻辑看看
现在这个tab里面暂时还没有提供设置类似这样的方法,不过我平时一般是这样弄的: `
for (i in 0 until tabLayout.tabCount) { val tab = tabLayout.getTabAt(i) ?: return val textView = tab.view as TextView if (tab.isSelected) { textView.typeface = Typeface.DEFAULT_BOLD //变色 } else { textView.typeface= Typeface.DEFAULT //变色 } }
No branches or pull requests
public class HomeFragment extends Fragment {
// private ViewPager viewPager;
// private XTabLayout topTabLayout;
// private BadgeView redDotBadgeView;
// private BadgeView redNumberBadgeView;
//
// private String[] strArray = new String[]{"关注", "推荐", "视频", "直播", "图片", "段子", "精华", "热门"};
//
// private List stringList = new ArrayList<>();
// private List fragmentList = new ArrayList<>();
//
public static HomeFragment instance() {
HomeFragment instance = new HomeFragment();
return instance;
}
}
NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference
The text was updated successfully, but these errors were encountered: