How can i set a tag for viewpager fragments?

出自:https://stackoverflow.com/questions/34861257/how-can-i-set-a-tag-for-viewpager-fragments

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

}

 

Viewed 19k times

17

3

I,m using slidingTabLayout with 4 tabs and a viewpager and 4 fragment class for every tab.

i need to reload one of my fragments (first tab) in my project when user click some button, and for this i need to have a tag for that fragment

my question is : how can i set a tag for one of viewpagers fragment???

My MainActivity tab and viewpager codes:

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 ....

Thank you a lot

android android-fragments android-viewpager

shareimprove this question

asked Jan 18 '16 at 18:07

 

K1-Aria

37733 gold badges88 silver badges2222 bronze badges

if you want to use fragments already in viewpager, you can use adapter's getItem(position) method. and for tags, i think you should pass the tags in Constructor of Adapter and then set to each fragments. –Sandip Soni Jan 18 '16 at 18:12

thank you my friend , but can you explain me more about passing the tags to adapter? thank you –K1-AriaJan 18 '16 at 19:28

add a comment

 

3 Answers

activeoldestvotes

 

26

There are couple of solution for this.

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. So you can simply get the current visible Fragment in your ViewPager using these lines of code

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). You should keep track of all the active fragments in the FragmentStatePagerAdapter. For demo sample you can refer to second approach here

3). You can make use of EventBus to post events to your Fragment from anywhere. All you have to do is to register your Fragment for that specific event. For official documentation refer this

shareimprove this answer

edited Dec 23 '17 at 9:51

answered Jan 18 '16 at 18:53

 

Mukesh Rana

3,42322 gold badges2020 silver badges3737 bronze badges

but i just used ViewPagerAdapter . what should i do? –K1-Aria Jan 18 '16 at 20:36

your ViewPagerAdapter may be extending FragmentPagerAdapter or FragmentStatePagerAdapter. isn't it?–Mukesh Rana Jan 19 '16 at 5:36

no i,m not used it never!!!! is there any other wat to set tags to fragment? idont know why its so difficult!!! –K1-Aria Jan 19 '16 at 14:49

you can go with the third solution then, its a universal solution and will work for sure without any issues. –Mukesh Rana Jan 19 '16 at 17:50

aha! Thanks . now with EventBus i can Tag fragments?? or i can refresh Them?? –K1-Aria Jan 19 '16 at 19:17

show 13 more comments

 

8

Here is the method which is used by FragmentPagerAdapter to create the Fragment's tag name:

private static String makeFragmentName(int viewId, long id) {

    return "android:switcher:" + viewId + ":" + id;

}

viewId is the id of the ViewPager.

id is the position of the fragment inside the ViewPager.

Example:

String fragmentTag = makeFragmentName(myViewPager.getId(), 2);

Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragmentTag);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值