Update ViewPager dynamically?

后端 未结 20 3061
时光说笑
时光说笑 2020-11-22 02:00

I can\'t update the content in ViewPager.

What is the correct usage of methods instantiateItem() and getItem() in FragmentPagerAdapter class?

I was using onl

相关标签:
20条回答
  • 2020-11-22 02:32

    I know am late for the Party. I've fixed the problem by calling TabLayout#setupWithViewPager(myViewPager); just after FragmentPagerAdapter#notifyDataSetChanged();

    0 讨论(0)
  • 2020-11-22 02:33

    I use EventBus library to update Fragment content in ViewPager. The logic is simple, just like document of EventBus how to do. It is no need to control FragmentPagerAdapter instance. The code is here:

    1: Define events

    Define which message which is needed to update.

    public class UpdateCountEvent {
        public final int count;
    
        public UpdateCountEvent(int count) {
            this.count = count;
        }
    }
    

    2.Prepare subscribers

    Write below code in the Fragment which is needed update.

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }
    
    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);  
        super.onStop();
    }
    
    public void onEvent(UpdateCountEvent event) {//get update message
        Toast.makeText(getActivity(), event.count, Toast.LENGTH_SHORT).show();
    }
    

    3.Post events

    Write below code in other Activity or other Fragment which needs to update parameter

    //input update message
    EventBus.getDefault().post(new UpdateCountEvent(count));
    
    0 讨论(0)
  • 2020-11-22 02:35

    I had a similar problem but don't want to trust on the existing solutions (hard coded tag names etc.) and I couldn't make M-WaJeEh's solution work for me. Here is my solution:

    I keep references to the fragments created in getItem in an array. This works fine as long as the activity is not destroyed due to configurationChange or lack of memory or whatever (--> when coming back to the activity, fragments return to their last state without 'getItem' being called again and thus without updating the array).

    To avoid this problem I implemented instantiateItem(ViewGroup, int) and update my array there, like this:

            @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Object o = super.instantiateItem(container, position);
            if(o instanceof FragmentX){
                myFragments[0] = (FragmentX)o;
            }else if(o instanceof FragmentY){
                myFragments[1] = (FragmentY)o;
            }else if(o instanceof FragmentZ){
                myFragments[2] = (FragmentZ)o;
            }
            return o;
        }
    

    So, on the one hand I'm happy that I found a solution that works for me and wanted to share it with you, but I also wanted to ask whether somebody else tried something similar and whether there is any reason why I shouldn't do it like that? So far it works very good for me...

    0 讨论(0)
  • 2020-11-22 02:36

    For some reason none of the answers worked for me so I had to override the restoreState method without calling super in my fragmentStatePagerAdapter. Code:

    private class MyAdapter extends FragmentStatePagerAdapter {
    
        // [Rest of implementation]
    
        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {}
    
    }
    
    0 讨论(0)
  • 2020-11-22 02:36

    I have lived same problem and I have searched too much times. Any answer given in stackoverflow or via google was not solution for my problem. My problem was easy. I have a list, I show this list with viewpager. When I add a new element to head of the list and I refresh the viewpager nothings changed. My final solution was very easy anybody can use. When a new element added to list and want to refresh the list. Firstly set viewpager adapter to null then recreate the adapter and set i to it to viewpager.

    myVPager.setAdapter(null);
    myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(),newList);
    myVPager.setAdapter(myFragmentAdapter);
    

    Be sure your adapter must extend FragmentStatePagerAdapter

    0 讨论(0)
  • 2020-11-22 02:37

    for those who still face the same problem which i faced before when i have a ViewPager with 7 fragments. the default for these fragments to load the English content from API service but the problem here that i want to change the language from settings activity and after finish settings activity i want ViewPager in main activity to refresh the fragments to match the language selection from the user and load the Arabic content if user chooses Arabic here what i did to work from the first time

    1- You must use FragmentStatePagerAdapter as mentioned above.

    2- on mainActivity i override the onResume and did the following

    if (!(mPagerAdapter == null)) {
        mPagerAdapter.notifyDataSetChanged();
    }
    

    3-i overrided the getItemPosition() in mPagerAdapter and make it return POSITION_NONE.

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
    

    works like charm

    0 讨论(0)
提交回复
热议问题