自定义TabLayout:notifydatasetchanged将选项卡的自定义视图恢复为默认值

时间:2017-04-27 10:59:42

标签: android android-fragments android-tablayout

以下是我目前得到的结果。

Result

预期结果应该是其中三个表现为第三个标签。

这是我设置标签布局的方式。

可添加片段以查看寻呼机/标签布局的可重用方法。

public void addFragmentToViewPager(Fragment fragment, String title, int imageRes) {

    FragmentViewPagerAdapter fragmentViewPagerAdapter =
            (FragmentViewPagerAdapter) mViewPager.getAdapter();

    int position = fragmentViewPagerAdapter.addFragment(fragment, title);

    fragmentViewPagerAdapter.notifyDataSetChanged();

    View tabItemView = LayoutInflater.from(getContext()).inflate(R.layout.tab_item_child, null);

    RoundedImageView roundImageView = (RoundedImageView) tabItemView.findViewById(R.id.childitem_riv);

    roundImageView.setImageResource(imageRes);

    TextView textView = (TextView) tabItemView.findViewById(R.id.childitem_tv_title);

    textView.setText(title);

    TabLayout.Tab tab = mTabLayout.getTabAt(position);

    if (tab != null) {
        Log.d(TAG,tab.getText()+"");
        tab.setCustomView(tabItemView);
    }
}

我如何添加片段:

    getView().addFragmentToViewPager(new ReusableMomentFragment(), "One", R.mipmap.ic_launcher);

    getView().addFragmentToViewPager(new ReusableMomentFragment(), "Two", R.mipmap.ic_launcher);

    getView().addFragmentToViewPager(new ReusableMomentFragment(), "Three", R.mipmap.ic_launcher);

我的FragmentViewPagerAdapter:

public class FragmentViewPagerAdapter extends FragmentPagerAdapter {

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

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

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

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

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

    return getCount() -1 ;
}

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

addFragmentToViewPager中的Log.d结果:

04-27 06:49:39.874 14334-14334/com.zxc.zxczxc D/MomentFragment: One
04-27 06:49:39.877 14334-14334/com.zxc.zxczxc D/MomentFragment: Two
04-27 06:49:39.878 14334-14334/com.zxc.zxczxc D/MomentFragment: Three

更新:

之后我发现了
  

fragmentViewPagerAdapter.notifyDataSetChanged();

调用,选项卡布局将更改为返回默认视图。有没有办法阻止notifyDataSetChanged()不影响标签布局&#39;自定义视图?因为我的标签布局标签在运行中设置(应用程序将从服务器获取标签内容并进行设置)所以我的想法是首先设置viewpager:

FragmentViewPagerAdapter fragmentViewPagerAdapter =
            new FragmentViewPagerAdapter(getActivity().getSupportFragmentManager());

    mViewPager.setAdapter(fragmentViewPagerAdapter);

    mTabLayout.setupWithViewPager(mViewPager);

然后使用:

添加片段
    public void addFragmentToViewPager(Fragment fragment, String title, int imageRes) {

    FragmentViewPagerAdapter fragmentViewPagerAdapter =
            (FragmentViewPagerAdapter) mViewPager.getAdapter();

    int position = fragmentViewPagerAdapter.addFragment(fragment, title);

    View tabItemView = LayoutInflater.from(getContext()).inflate(R.layout.tab_item_child, null);

    RoundedImageView roundImageView = (RoundedImageView) tabItemView.findViewById(R.id.childitem_riv);

    roundImageView.setImageResource(imageRes);

    TextView textView = (TextView) tabItemView.findViewById(R.id.childitem_tv_title);

    textView.setText(title);

    TabLayout.Tab tab = mTabLayout.getTabAt(position);

    if (tab != null) {
        Log.d(TAG,tab.getText()+"");
        tab.setCustomView(tabItemView);
    }
}

提前致谢

2 个答案:

答案 0 :(得分:3)

解决方案非常简单。

我们应该了解PagerAdapter.notifyDataSetChange()的工作原理。通过调用它,将重新加载选项卡。我发布的方法是在外部更改布局。这意味着自定义布局将恢复为默认布局。

因此解决方案是首先将片段添加到fragmentViewPager,然后调用notifyDataSetChanged,然后自定义您的布局。

您应该只在调用notifyDataSetChanged后自定义您的布局。

答案 1 :(得分:0)

尝试使用Material Design TabLayout

in gradle:

 compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'

代码:

 public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.ic_tab_favourite,
            R.drawable.ic_tab_call,
            R.drawable.ic_tab_contacts
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        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("ONE");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("TWO");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_call, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabThree.setText("THREE");
        tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_contacts, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabThree);
    }

    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);
        }
    }
}

布局:

activity_main.xml中:

<android.support.design.widget.TabLayout
       android:id="@+id/tabs"
       android:layout_width="match_parent"
       android:layout_height="@dimen/custom_tab_layout_height"
       app:tabMode="fixed"
       app:tabGravity="fill"/>

输出:

enter image description here

这是详细信息Implementation