使用viewpager的片段事务返回空白视图 [英] Fragment transaction with viewpager returns blank view

查看:115
本文介绍了使用viewpager的片段事务返回空白视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Tablayout与View分页器一起使用以显示片段.我当前的设置是获取类别"的列表视图,然后用户单击一个类别,然后返回一个新的片段,其中包含该类别中可用折扣的列表.

I am using tablayout with view pager to display fragments. My current setup is that I get a listview of 'Categories' and the user clicks on a category then returns a new fragment containing a list of available discounts within that category.

我的安装程序创建了新的DiscountList片段,但是当原始片段执行其事务时不会更新视图.有人可以帮助我找出正确的解决方案吗?我不确定我的viewpager是否正确或缺少步骤.如果需要,我可以提供更多信息.谢谢!

My setup creates the new DiscountList fragment but does not update the view when the original fragment does its transaction. Could someone help me figure out the correct solution? I am not sure if my viewpager is incorrect or I am missing a step. I can provide more information, if needed. Thanks!

CategoryListFragment.java:

CategoryListFragment.java:

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

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
            transaction.replace(container.getId(), DiscountListFragment.newInstance(position))
                    .addToBackStack(null)
                    .commit();
        }
    });

    return view;
}

DiscountListFragment.java

DiscountListFragment.java

public static DiscountListFragment newInstance(int categoryId) {
    DiscountListFragment discountListFragment = new DiscountListFragment();

    Bundle args = new Bundle();
    args.putInt("categoryId", categoryId);
    discountListFragment.setArguments(args);

    return discountListFragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        categoryId = getArguments().getInt("categoryId");
    }
    reactiveLocationProvider = new ReactiveLocationProvider(getActivity());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_search_discount_list, container, false);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

ViewPager.java

ViewPager.java

/**
 * Retrieves the selected tab and returns it. If new tab needs to be added
 * simply increment case and return a new fragment
 * @param position the position of tab selected
 * @return the selected tab
 */

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            return new HomeFragment();
        case 1:
            return new CategoryListFragment();
        case 2:
            return new MapViewFragment();
        case 3:
            return new MessagesFragment();
        default:
            return null;
    }
}

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

推荐答案

替换ViewPager中的片段的最佳方法是创建一个装入到寻呼机中的根片段.根片段默认情况下会加载fragment1,以后可以用fragment2替换. 有人为此做了一个很好的例子,并将其放在Github上: https://github.com/danilao/fragments-viewpager-example

The best way to replace a fragment within a ViewPager is to create a root fragment that is loaded into the pager. The root fragment loads fragment1 by default, which can later be replaced by fragment2. Someone made a good example of this and put it up on Github: https://github.com/danilao/fragments-viewpager-example

我将采用与示例稍有不同的方法,并让根片段负责切换片段,而不是子片段本身.但这是一个架构决定,由您决定.

I would take a slightly different approach than the example, and have the root fragment be responsible for switching the fragments, rather than the child fragment itself. But that is an architectural decision, and is up to you.

这篇关于使用viewpager的片段事务返回空白视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆