1

I have three tabs which holds three different fragments with listview for each fragment and it looks like this:

enter image description here

When I'm pressing white button list item is added to tab2 and if I press purple button list item is added to tab3. Tab3 is refreshed because viewPager has default offscreen limit 1 so when I'm navigating to tab3 it is created and on that time it has its new listitem, but tab2 is already created when app starts so I need to refresh it by some method. So I created updateView() method in tab2 fragment and I'm calling that method on OnPageChangeListener() it updates view, but it gets called every time I navigate to tab2 and it causing lag.All I want to do is to delete that list item and add it to tab2 and it gets removed and it is added to tab2 but when I swipe to tab2 I cannot see it, just after closing app it appears or when tab2 fragment calls onResume, onCreate...

Async method which loads list items in background:

public class AchievedDreamFragment extends Fragment {

ListView achievedListView;
Activity activity;
ArrayList<AchievedCloud> achievedClouds;


AchievedListAdapter achievedListAdapter;
AchievedActionHandler achievedActionHandler;

private GetAvedTask atask;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
achievedActionHandler = new AchievedActionHandler(activity);

}


public class GetAvedTask extends AsyncTask<Void, Void, <AchievedCloud>> {

private final WeakReference<Activity> activityWeakRef;


public GetAvedTask(Activity context) {
    this.activityWeakRef = new WeakReference<Activity>(context);
}

@Override
protected ArrayList<AchievedCloud> doInBackground(Void... arg0) {
    Log.e(TAG,"do in background");
    //achievedActionHandler = new AchievedActionHandler(activity);
    ArrayList<AchievedCloud> dreamList = achievedActionHandler.getaDreams();
    return dreamList;
}

@Override
protected void onPostExecute(ArrayList<AchievedCloud> adrmList) {
    Log.e(TAG,"on Post execute");
    if (activityWeakRef.get() != null
            && !activityWeakRef.get().isFinishing()) {
        achievedClouds = adrmList;
        if (adrmList != null) {
            if (adrmList.size() != 0) {
                achievedListAdapter = new AchievedListAdapter(activity,
                        adrmList);
                achievedListView.setAdapter(achievedListAdapter);
                achievedListAdapter.notifyDataSetChanged();
            }
        }

    }
}
}

UpdateView() method in same fragment where asyncTask is:

 public void updateView() {

    atask = new GetAvedTask(activity);
    atask.execute((Void) null);

 }

viewpager OnPageChangeListener():

@Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        if(position == 1){
            achievedDreamFragment.updateView();

        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

I tried as well on adapter getItemPosition POSITION_NONE, but it doesn't fix this any ideas what could I try?

LeTadas
  • 3,436
  • 9
  • 33
  • 65

6 Answers6

1

i don't understand fully what you want to achieve but i'm thinkin that you are looking for this:

How to determine when Fragment becomes visible in ViewPager

Community
  • 1
  • 1
ceph3us
  • 7,326
  • 3
  • 36
  • 43
0

I had this same issue. I'm not sure if this is the cleanest way to do this, but it worked for me.

I created a SharedPreferences variable. In the onClick to add the list item in the Fragment, I updated the SharedPreferences variable to "refresh", and also added an Intent to return to the MainActivity. In the MainActivity in onCreate, I check the value of the SharedPreferences varible. If it is equal to "refresh", I updated the variable to "", and added an Intent to return to the Original Fragment.

SillyFidget
  • 197
  • 3
  • 16
0

You can get the instance of tab2 fragment. Then call your updateView() method on that instance directly:

Object anObject = viewPager.getAdapter().instantiateItem(viewPager, 1);
// 1 is the Tab2; viewPager is the ViewPager in main activity
if (anObject instanceof Tab2) {  // Tab2 is your tab2's fragment
    Tab2 tab2 = (Tab2) anObject;
    tab2.updateView();
}
user35603
  • 765
  • 2
  • 8
  • 24
0

For this king of problems 'EventBus' is perfect.

First add dependency: compile 'org.greenrobot:eventbus:3.0.0' in your build.gradle file. For more info see here: EventBus

In Tab2 add these methods:

`
@Override
public void onStart(){
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop(){
    EventBus.getDefault().unregister(this);
    super.onStop();
}
public void onEventItemAdded(EventAddItemTab2 event){
        loadItems(); // reload items from DB/Web using asynctask and set these items to the adapter

}

`

Create a new class in package with name, for example models, this class: public class EventAddItemTab2{ // empty here }

Then in Tab1 when you click on the White button call this:

EventBus.getDefault().post(new EventAddItemTab2());

More info: When you call .post method of EventBus, this automatically will call onEventItemAdded method in Tab2. You can use this pattern for Tab3 too. This solution is perfect for big applications where you must separate the logic. Tab1, Tab2 and Tab3 should doesn't know for each other's existence.

Borislav Kamenov
  • 561
  • 4
  • 12
0

Sorry to say but your requirement is not clear to me, but from what I understand if you want to achieve that with POSITION_NONE flag, then you have call adapter.notifyDatasetChanged() for the viewpager adapter.Then it will refresh all the views and if you have called your method inside one of the overridden methods it will get updated.So you may call it on click of the button.

Pritish
  • 1,284
  • 1
  • 19
  • 42
0

All I needed to do is call this method on my tab2 fragment. It gets called every time fragment is visible!

@Override
public void setMenuVisibility(boolean menuVisible) {
    super.setMenuVisibility(menuVisible);
    if(menuVisible){

        //load listview
    }
}
LeTadas
  • 3,436
  • 9
  • 33
  • 65