Dynamically change ViewPagerIndicator titles

空扰寡人 提交于 2019-12-06 06:31:35

问题


My current project uses some ListFragments to show rows of data. The rows get updated dynamically every some seconds. The amount of rows varies with every update and in every ListFragment.

I would like to show the amount of rows to the user, and think that the perfect place for that would be next to the Fragment's title in the ViewPagerIndicator. I provided a sample image for better comprehension:

Sadly I am pretty clueless how to achieve this. I tried the following:

public class PagerAdapter extends FragmentPagerAdapter {
    private int numOne = 0;
    private int numTwo = 0;

    // ...

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return "List 1 (" + numOne + ")";
        case 1:
            return "List 2 (" + numTwo + ")";
        default:
            return "";
    }

    public void setNumOne(int num) {
        this.numOne = num;
    }

    public void setNumTwo(int num) {
        this.numTwo = num;
    }
}

When I now call the setNumXXX() method, nothing happens, until I move between fragments, what seems to trigger the getPageTitle() to fire.

My question is: How can I force an update of the title(s), everytime when the num value changes?


回答1:


If you call notifyDataSetChanged() on the indicator instance it will redraw itself and grab the new titles.



来源:https://stackoverflow.com/questions/12186754/dynamically-change-viewpagerindicator-titles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!