Skip to content
New issue

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

hello,xtablayout fragment 出错 #4

Open
molele2 opened this issue May 28, 2019 · 3 comments
Open

hello,xtablayout fragment 出错 #4

molele2 opened this issue May 28, 2019 · 3 comments

Comments

@molele2
Copy link

molele2 commented May 28, 2019

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

@molele2
Copy link
Author

molele2 commented May 28, 2019

另 如何在点击tab是设置字体的大小及颜色

@JasonGaoH
Copy link
Owner

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下这块逻辑看看

@JasonGaoH
Copy link
Owner

另 如何在点击tab是设置字体的大小及颜色

现在这个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
                //变色
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants