【问题标题】:Update fragment view after onCreateView in FragmentPagerAdapter在 FragmentPagerAdapter 中的 onCreateView 之后更新片段视图
【发布时间】:2013-08-27 09:50:15
【问题描述】:

我正在尝试使用操作栏中的选项卡和视图寻呼机来实现一个活动。每个选项卡都有相同的片段。 我需要在调用 onCreateView 后更新片段,传递对象。

寻呼机适配器:

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;
    }
}

片段:

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

==> 调用 onCreateView 后,我需要在 PagerAdapter getItem() 中调用 loadPosts(...)。

非常感谢任何帮助。

【问题讨论】:

  • 也许你可以使用广播接收器和意图

标签: android android-fragments android-viewpager fragmentpageradapter


【解决方案1】:

您应该像这样将您的类别作为 Extra 传递给片段:

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

然后在片段中的 onCreate() 中,您可以使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    相关资源
    最近更新 更多