如何为 viewpager 片段设置标签?

新手上路,请多包涵

我正在使用带有 4 个选项卡的 slidingTabLayout 和一个 viewpager 以及每个选项卡的 4 个片段类。

当用户单击某个按钮时,我需要在我的项目中重新加载我的一个片段(第一个选项卡),为此我需要为该片段添加一个标签

我的问题是:如何为其中一个 viewpager 片段设置标签???

我的 MainActivity 选项卡和 viewpager 代码:

 public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"نوع","قیمت","برند","همه"};
int Numboftabs = 4;

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "درج آگهی رایگان", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.colorWhite);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);
    pager.setCurrentItem(3);

} and ....

十分感谢

原文由 K1-Aria 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 290
2 个回答

有几个解决方案。

1). When the FragmentPagerAdapter adds a Fragment to the FragmentManager , it uses a special tag based on the particular position that the Fragment will be placed .所以你可以简单地得到当前可见的 Fragment 在你的 ViewPager 使用这些代码行

 Fragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + ViewPager.getCurrentItem());
 // based on the current position you can then cast the page to the correct Fragment class and call some method inside that fragment to reload the data:
 if (0 == ViewPager.getCurrentItem() && null != fragment) {
      ((TabFragment1)fragment).reloadFragmentData();
 }

2).您应该跟踪 FragmentStatePagerAdapter 中的所有活动片段。对于演示示例,您可以 在此处 参考第二种方法

3).您可以使用 EventBus Fragment 从任何地方将事件发布到您的 —。您所要做的就是为该特定事件注册您的 Fragment 。官方文档参考 这个

原文由 Mukesh Rana 发布,翻译遵循 CC BY-SA 3.0 许可协议

这是 FragmentPagerAdapter 用来创建 Fragment 的标签名称的方法:

 private static String makeFragmentName(int viewId, long id) {
    return "android:switcher:" + viewId + ":" + id;
}

viewIdViewPager 的 ID。

id 是片段在 ViewPager --- 中的位置。

例子:

 String fragmentTag = makeFragmentName(myViewPager.getId(), 2);
Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);

原文由 vovahost 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题