如何从片段在TabLayout中设置BadgeView?

时间:2018-10-22 04:19:55

标签: java android notifications fragment android-tablayout

我有MainActivity,其中包含TabLayoutViewPager菜单。然后菜单具有自定义图标和文本。我还使用了Badge Library

中的BadgeView

在菜单上使用通知徽章都做得很好,但是我不知道如何不在主要活动中而是在片段中设置通知徽章文本。

MainActivity

 viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(3);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();

 private void setupTabIcons() {

        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("Matakuliah");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.baseline_home_white_24dp, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("Tugas");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.baseline_assignment_late_white_24dp, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);
        BadgeView badge2 = new BadgeView(getApplicationContext(), tabTwo);
        badge2.setBadgeMargin(75,0);
        badge2.setTextSize(10);
        badge2.setText("1");
        badge2.show();

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabThree.setText("Pengumuman");
        tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_notifications_black_24dp, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabThree);
        BadgeView badge3 = new BadgeView(getApplicationContext(), tabThree);
        badge3.setBadgeMargin(90,0);
        badge3.setTextSize(10);
        badge3.setText("1");
        badge3.show();


    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "ONE");
        adapter.addFrag(new TwoFragment(), "TWO");
        adapter.addFrag(new ThreeFragment(), "THREE");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }



        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(mToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

我在TwoFragment中尝试了此代码,但失败了。

    View a = ((MainActivity)getActivity()).tabLayout.getChildAt(1);
    BadgeView badge = new BadgeView(getActivity(), a);
    badge.setText("12");
    badge.show();

我的目标是我可以从片段而不是从mainactivity设置徽章。真的谢谢

0 个答案:

没有答案