6

我目前在我的应用程序中使用 PagerTitleStrip(文档:http: //developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html

一切正常,我设置了这样的标题:

public class PageAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PageAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

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

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

    String[] titles = { "1", "2", "3", "4","5" };

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

我现在担心的是,一旦第一个片段完成加载,我必须更改片段的标题。

我已经尝试获取 FragmentPagerAdapter 并尝试对其进行操作,但从未成功更改标题(文本)

确实有一个 getPageTitle 方法: mPagerAdapter.getPageTitle(position) 但没有 setPageTitle ...

有什么想法或建议吗?

4

2 回答 2

12

如果我正确理解问题 - 您稍后会获取数据,然后想要更新标题数组并触发标题刷新。

If that's the case just trigger PagerAdapter.notifyDataSetChanged(); after that method is invoked Fragments will call getPageTitle method and update themselves.

于 2013-03-10T10:33:18.047 回答
-1

Kind of ugly solution but working : iterate to the visual tree and set the text yourself.

Also, you have to be aware that there is a first transparent/empty item on the left.

Here is the code (Xamarin here) to update the first title.

var firstHidedOneFound = false;
for (int i = 0; i < _tabstrip.ChildCount; ++i)
{
    var child = _tabstrip.GetChildAt(i) as TextView;

    if (child == null) { continue; }
    if (!firstHidedOneFound)
    {
        firstHidedOneFound = true;

        continue;
    }
    child.Text = ViewModel.MyShowPanelTitle;
    break;
}
于 2017-08-24T08:16:52.923 回答