简体   繁体   中英

Update fragment view after onCreateView in FragmentPagerAdapter

I'm trying to implement an activity with tabs in action bar and a view pager. I have the same fragment for every tab. I need to update fragments after onCreateView is called, passing objects.

Pager adapter :

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    public MyFragmentPagerAdapter() {
        super(getSupportFragmentManager());
    }

    @Override
    public int getCount() {
        return cat_pages.getList_categories().size();
    }

    @Override
    public Fragment getItem(int position) {
        Log.i("position fragment", "i = " + position); 

    final Handler uiHandler = new Handler();
    int index = position;

    final Category cat = cat_pages.getList_categories().get(index);
    Log.i("position fragment", "name = " + cat.getName()); 

    mCategoriesFragment = new ListPostFragment();

        return mCategoriesFragment;
    }
}

Fragment :

public class ListPostFragment extends Fragment {

// Layouts
private LinearLayout layout_loading;
public static GridView list_view_articles;
private TextView txt_nothing, txt_title;

// The list of headlines that we are displaying
private List<Post> mPostsList = new ArrayList<Post>();
private List<Post> list_posts = new ArrayList<Post>();

// The list adapter for the list we are displaying
private ArticlesAdapter mListAdapter;

private Category mCurrentCat;

Category clicked_cat;
String activity;
String title;

public ListPostFragment() {
        super();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mListAdapter = new ArticlesAdapter(getActivity(), mPostsList);
}

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

    layout_loading = (LinearLayout) rootView.findViewById(R.id.layout_loading);
    list_view_articles = (GridView) rootView.findViewById(R.id.gridViewArticles);
    txt_nothing = (TextView) rootView.findViewById(R.id.txt_no_result);
    txt_title = (TextView) rootView.findViewById(R.id.txt_title);
    Log.i("frag", "frag create view");

    return rootView;
}

@Override
public void onStart() {
    super.onStart();
    list_view_articles.setAdapter(mListAdapter);
}

public void loadPosts(Category clicked_cat, List<Post> list_posts, String title) {

    Log.i("frag", "frag load");
    layout_loading.setVisibility(View.VISIBLE);
    list_view_articles.setVisibility(View.GONE);
    txt_title.setVisibility(View.GONE);

    mPostsList.clear();

    layout_loading.setVisibility(View.GONE);

    if(list_posts != null){
        mPostsList.addAll(list_posts);

    if(mPostsList.size() > 0){
        list_view_articles.setVisibility(View.VISIBLE);
        txt_nothing.setVisibility(View.GONE);
    }else{
            list_view_articles.setVisibility(View.GONE);
        txt_nothing.setVisibility(View.VISIBLE);
    }

    mListAdapter.notifyDataSetChanged();
    }else{ // Nothing to display
        list_view_articles.setVisibility(View.GONE);
    txt_nothing.setVisibility(View.VISIBLE);
    }
}
}

==> What I need to to call loadPosts(...) in PagerAdapter getItem(), once onCreateView has been called.

Any help is very appreciated.

You should pass your Category as an Extra to the fragment like so:

Bundle bundle = new Bundle();
bundle.putSerializable("CATEGORY", cat);
mCategoriesFragment.setArguments(bundle);

and then in onCreate() in the fragment you can read it using:

Bundle bundle = this.getArguments();
Category cat = (Category) bundle.getSerializable("CATEGORY");
// now you can call loadPosts()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM