简体   繁体   English

FragmentPagerAdapter 动态更新标题

[英]FragmentPagerAdapter update title dynamically

I have a ViewPager with FragmentPagerAdapter, which has 3 Fragments.我有一个带有 FragmentPagerAdapter 的 ViewPager,它有 3 个片段。 Now, I have a number of items as part of the title of the fragment:现在,我有许多项目作为片段标题一部分

在此处输入图片说明

So user can add or remove those items, and title should update accordingly.所以用户可以添加或删除这些项目,标题应该相应地更新。 However, this title seems static.然而,这个标题似乎是静态的。

How can one recall this method to update FragmentPagerAdapter's titles?怎么能记得这个方法来更新 FragmentPagerAdapter 的标题呢?

@Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return items + " (" + notFoundItems + ")";
            case 1: return found;
            case 2: return review;
        }
        return "";
    }

I found a couple of similar questions on SO, but I need to update title , not only the fragment itself.我在 SO 上发现了几个类似的问题,但我需要更新title ,而不仅仅是片段本身。

Finally, I solved it by simply re-setting the pager.最后,我通过简单地重新设置寻呼机来解决它。

private OrdersTabsAdapter tabsAdapter;
@InjectView(R.id.tabs_orders) SlidingTabLayout ordersTabs;
@InjectView(R.id.pager) ViewPager pager;

public void update() {
  tabsAdapter.updateFragments(productId, status);
  ordersTabs.setViewPager(pager); //this helped with the titles
}

As one of the comments beneath it suggests, the accepted answer looks a bit overkill to me.正如它下面的评论之一所暗示的那样,接受的答案对我来说看起来有点矫枉过正。

Based on Frank's answer , this is what I have done:根据弗兰克的回答,这就是我所做的:

public void myUpdatePageTitle(int pagePosition, int numItems) {
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    TabLayout.Tab tab = tabLayout.getTabAt(pagePosition);
    if (tab != null) {
        tab.setText(myCalcTabTitle(pagePosition, numItems));
    }
}

private String myCalcTabTitle(int pagePosition, int numItems) {
    //TODO - For this page, return whatever title string you like, including the numItems value. (The best way to do this is using a string resource that accepts a decimal parameter.)
}

Okay.好的。 So you can have a method in FragmentPagerAdapter that will update the count for notFoundItems , foundItems , itemsForReview .因此,您可以在FragmentPagerAdapter中使用一个方法来更新notFoundItemsfoundItemsitemsForReview的计数。 After you update them, call the notifyDataSetChanged() .更新它们后,调用notifyDataSetChanged()

 @Override
 public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return items + " (" + notFoundItems + ")";
            case 1: return found + " (" + foundItems + ")";
            case 2: return review + " (" + itemsForReview + ")";
        }
        return "";
 }

Also, add this to the your implementation of FragmentPagerAdapter class.此外,将此添加到FragmentPagerAdapter类的实现中。 So whenever you have a new data count and want to update the title, call this method.因此,每当您有新的数据计数并想要更新标题时,请调用此方法。

 public void updateTitleData(int notFoundItems, int foundItems, int itemsForReview) {
      this.notFoundItems = notFoundItems;
      this.foundItems = foundItems;
      this.itemsForReview = itemsForReview;
      notifyDataSetChanged();
 }

you have 3 fragments so you will use 3 tabs(first tab hav index 0,second 1,third 2).您有 3 个片段,因此您将使用 3 个标签(第一个标签具有索引 0,第二个 1,第三个 2)。 get your tab using index and set her text as title.使用索引获取您的标签并将她的文本设置为标题。 May this help you bro可以帮助你兄弟

Example:例子:

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.getTabAt(index).setText("");

I don't think reset ViewPager and notifyDataSetChange is a good idea.我不认为重置 ViewPager 和 notifyDataSetChange 是个好主意。 Because it will cause the view in ViewPager to recreate.因为它会导致 ViewPager 中的视图重新创建。 It's unnecessary.这是不必要的。

And i found another way to solve the problem.我找到了另一种解决问题的方法。

    List<OrderListResult.TradeStatusVOsEntity> tradeStatusVOs = updateTitleEvent.getTradeStatusVOs();
    for (int i = 0; i < tradeStatusVOs.size(); i++) {
        OrderListResult.TradeStatusVOsEntity tradeStatusVOsEntity = tradeStatusVOs.get(i);
        int num = tradeStatusVOsEntity.getNum();
        String text = mAdapter.getPageTitle(i).toString();
        if (num > 0) {
            text = text + "(" + num + ")";
        }
        mButterTabs.getTabAt(i).setText(text);
    }

    mButterTabs.setTabTextColors(getResources().getColor(android.R.color.darker_gray),
            getResources().getColor(R.color.yellow));

First, you need to use getTabAt method of TabLayout to change the model of tab.首先,你需要使用getTabAt的方法TabLayout改变标签的模型。 Then call setTabTextColors to update view.然后调用setTabTextColors来更新视图。 Because from the source code of TabLayout, setTabTextColors will update all tabs one by one.因为从TabLayout的源码看, setTabTextColorssetTabTextColors更新所有的tab。

There is no point in your re setting your pager each time.每次都重新设置寻呼机是没有意义的。
You've already got getPageTitle() overridden and you are just missing to track when your pages are changed and when is the time to update the title.您已经覆盖了 getPageTitle() 并且您只是缺少跟踪页面何时更改以及何时更新标题。
So you can easily do this:所以你可以很容易地做到这一点:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
        // your tite is: mAdapter.getPageTitle(position));
        // TODO: update title
    }
});

Also you will have to set initial title in onCreate()您还必须在 onCreate() 中设置初始标题

TabLayout.OnTabSelectedListener will be triggered when re-setup with the ViewPager , so I recommend update the title by self. TabLayout.OnTabSelectedListener重新设置时会触发ViewPager ,因此我建议自行更新标题。

viewPager.adapter?.apply {
    for (p in 0..count) {
        tabLayout.getTabAt(p)?.text = getPageTitle(p) ?: break
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM