使用android支持TabLayout单击选项卡时显示Dialog片段?

时间:2016-04-09 08:43:13

标签: android android-fragments android-tablayout

我想点击支持DialogFragment中的第五个标签时显示TabLayout弹出窗口,我不想在点击第5个项目时替换片段或标签位置。为了实现这一点,我做了以下工作。

TabViewPagerAdapter adapter = new TabViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(ProfileFragment.create(mProfileId), getString(R.string.profile));
adapter.addFragment(NewsFragment.create(getString(R.string.news_feed)), getString(R.string.news_feed));
adapter.addFragment(AlertFragment.create(mProfileId), getString(R.string.alert));
adapter.addFragment(EventsFragment.create(mProfileId), getString(R.string.events));
//Logic implemented to show an empty fragment
adapter.addFragment(new Fragment(), getString(R.string.more));

适配器

public class TabViewPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

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

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

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

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


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

}

显示更多对话框

((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                MoreDialogFragment newFragment = MoreDialogFragment.newInstance();
                newFragment.show(ft, "ReserveChooser");
            }
        });

在我将支持库更新到最新版之前,它工作正常。更新后,Dialog显示,但同时标签项替换/显示空片段,即第5个标签被选中,我不想要。任何评论家伙?

2 个答案:

答案 0 :(得分:0)

我已经做到了,但不知道是否有正确的方法来等待经验丰富的确认

 ((ViewGroup) mTabLayout.getChildAt(0)).getChildAt(4).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if (action == MotionEvent.ACTION_UP) {
                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    MoreDialogFragment newFragment = MoreDialogFragment.newInstance();
                    newFragment.show(ft, "ReserveChooser");
                    return true;
                }
                return false;
            }
        });

答案 1 :(得分:-1)

你可以试试这个:

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab.getPosition() == 4){
                    //show dialog
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });