0

I'm using ViewPager and TabLayout with 5 fragment. My code showed if I click a button which in first or third fragment, data in second fragment. But ViewPager already makes previous and next page, so second fragment doesn't update directly. Second fragment has ListView and shows data in regular order. When I go to fourth or fifth fragment and back to second fragment, the data updated. Because ViewPager newly draws a second fragment.

I found many informations about this problem. Many people say to solve it, I should use 'setUserVisibleHint'. But I don't know how to use that function in my case.

To solve this problem, I must use 'setUserVisibleHint'? Then how to I apply it?

or Are there any other method?

Here is my second fragment code.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.accesslog_layout, container, false);

    adapter = new ListViewAdapter(getActivity().getApplicationContext());

    listView = (ListView) view.findViewById(R.id.Accesslog_listView);
    emptyElement = (TextView) view.findViewById(R.id.empty_element);

    listView.setAdapter(adapter);
    listView.setOnItemLongClickListener(itemLongClickListener);
    listView.setEmptyView(emptyElement);

    check_editName = (TextView) view.findViewById(R.id.Room_State);
    listView.setOverScrollMode(view.OVER_SCROLL_ALWAYS);

    accessTable = AccessTable.instance();//(getActivity().getApplicationContext());

    logD(TAG, "TestTable Dbug===Fragment====AccessLog============ ");

    loadAllFromDB();

    return view;
}

================================== Update ==================================

Where should I put on the code "getItemPosition()" and "notifyDataSetChanged()"?

Here is my Adapter code.

public class TabPagerAdapter extends FragmentStatePagerAdapter {

private int tabCount;

public TabPagerAdapter(FragmentManager fm, int tabCount){

    super(fm);
    this.tabCount = tabCount;

}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
} // Here is right? and is this right code?

@Override
public Fragment getItem(int position){

    switch (position){
        case 0:
            HomeFragment fragment1 = new HomeFragment();
            return fragment1;

        case 1:
            AccessLogFragment fragment2 = new AccessLogFragment();
            return fragment2;

        case 2:
            RegistrationFragment fragment3 = new RegistrationFragment();
            return fragment3;

        case 3:
            SettingFragment fragment4 = new SettingFragment();
            return fragment4;

        case 4:
            HelpFragment fragment5 = new HelpFragment();
            return fragment5;

        default:
            return null;
    }

}

@Override
public int getCount(){
    return tabCount;
}

}

오영택
  • 7
  • 1
  • 8
  • check the 1st and 2nd answer here https://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view – ericn Mar 07 '18 at 02:32
  • @ericn I updated my code and question. Can you tell me how do I fix that? – 오영택 Mar 07 '18 at 08:51

3 Answers3

0

Override the method in your second fragment. Follow this template:

@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if(isResumed() && isVisibleToUser) {
            //Update your data
        } else {
            //Do nothing
        }
    }
Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
  • I think using "getItemPosition" is what I want. But I don't know how to use that code. Can you help me? – 오영택 Mar 07 '18 at 08:54
0

You can call notifyDataSetChanged() on your PagerAdapter to refresh fragments with new data.

ahomphophone
  • 319
  • 1
  • 8
  • Data will update well. But It doesn't operate directly. I should go to fourth or fifth fragment. I think it is a problem of ViewPager. – 오영택 Mar 07 '18 at 02:35
0

After you click the button,you can write a flag into SharedPreferences or database.When you turn back to second fragment,check if the flag exits and refresh data.

Qian Sijianhao
  • 564
  • 7
  • 19