Android 重新创建活动后,PagerAdapter不调用get item

Android 重新创建活动后,PagerAdapter不调用get item,android,android-fragments,android-activity,state,appcompatactivity,Android,Android Fragments,Android Activity,State,Appcompatactivity,我有一个按钮来重新创建活动,它调用: recreate(); 这是创建时活动上的代码,该活动是一个具有不同片段的viewpager,根据返回位置将分配不同的数据,实际上它在重新创建后运行: if (app.cat == null) { app.cat = new ArrayList<Category>(); Ion.with(ctx) .load(app.API + "api/shop/category-li

我有一个按钮来重新创建活动,它调用:

recreate();
这是创建时活动上的代码,该活动是一个具有不同片段的viewpager,根据返回位置将分配不同的数据,实际上它在重新创建后运行:

    if (app.cat == null) {
        app.cat = new ArrayList<Category>();

        Ion.with(ctx)
                .load(app.API + "api/shop/category-list")
                .setBodyParameter("lang", MyApp.lang)
                .setBodyParameter("device_token", app.token)
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        String status = result.get("result").getAsString();

                        if (status.equals("OK")) {
                            JsonArray cat_list = result.get("categories").getAsJsonArray();

                            for(int i = 0; i < cat_list.size(); i++) {
                                JsonObject cat_obj = cat_list.get(i).getAsJsonObject();
                                app.cat.add(new Category(cat_obj.get("id").getAsString(), cat_obj.get("label_name").getAsString(), cat_obj.get("label_name_en").getAsString(), cat_obj.get("img").getAsString()));
                            }

                            setupPager();
                        } else if (result.get("error_code").equals("666")) {
                            createErrorBox(result.get("message").getAsString());
                        } else {
                            Toast.makeText(ctx, result.get("message").getAsString(), Toast.LENGTH_LONG).show();
                        }
                    }
                });
    } else {
        setupPager();
    }
}
问题是,即使重新创建后再次设置了寻呼机,寻呼机适配器也不会调用
getitem()
,也不会返回位置

public class SampleFragmentPagerAdapter extends FragmentStatePagerAdapter {
    private MyApp app;
    private Context context;

    public View getTabView(int position) {
        View v = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
        TextView tv = (TextView) v.findViewById(R.id.title);
        tv.setText(app.cat.get(position).name);
        return v;
    }

    public SampleFragmentPagerAdapter(FragmentManager fm, Context _context, MyApp _app) {
        super(fm);
        context = _context;
        app = _app;
    }

    @Override
    public int getCount() {
        return app.cat.size();
    }

    @Override
    public Fragment getItem(int position) {
        **//does not call after recreate**

        PageFragment p = new PageFragment();
        p.setID(app.cat.get(position).id);
        return p;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return app.cat.get(position).name;
    }
}
最后,片段本身

public class PageFragment extends Fragment {
    private String id;
    private MyApp app;
    private Context ctx;
    private RecyclerView rv;
    private ArrayList<Product> products;

    public PageFragment () {
    }

    public void setID (String _id) {
        id = _id;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ctx = getContext();
        app = (MyApp) getActivity().getApplicationContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.product_list, container, false);

        rv = (RecyclerView) rootView.findViewById(R.id.recycler_view);
        rv.setHasFixedSize(true);

        if (app.product == null) {
            app.product = new HashMap<>();
        }

        **// id is null after re-create**
        products = app.product.get(id);

        ProductAdapter adapter = new ProductAdapter(products, ctx, app);
        rv.setAdapter(adapter);
        rv.setLayoutManager(new GridLayoutManager(getActivity(), 4));

        return rootView;
    }


}
public类PageFragment扩展了片段{
私有字符串id;
私人MyApp;
私有上下文ctx;
私人回收车;
私人ArrayList产品;
公共页面片段(){
}
public void setID(字符串_id){
id=_id;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ctx=getContext();
app=(MyApp)getActivity().getApplicationContext();
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
//为该碎片膨胀布局
视图根视图=充气机。充气(R.layout.product\u列表,容器,假);
rv=(RecyclerView)rootView.findviewbyd(R.id.recycler\u视图);
rv.setHasFixedSize(真);
如果(app.product==null){
app.product=new HashMap();
}
**//重新创建后id为空**
products=app.product.get(id);
ProductAdapter=新的ProductAdapter(产品、ctx、应用程序);
rv.设置适配器(适配器);
setLayoutManager(新的GridLayoutManager(getActivity(),4));
返回rootView;
}
}

非常感谢您的帮助

检查
app.cat
是否为空!在主活动中选中“是”,请参阅更新问题,谢谢一个lotpost代码,您也可以在主活动中重新创建,当更改区域设置后将在菜单中重新创建视图检查
app.cat
是否为空!在主活动中选中“是”,请参阅更新问题,谢谢一个lotpost代码,在主活动中,您也可以在这里重新创建,当稍后发布更改区域设置时,可以在菜单中重新创建视图
public class PageFragment extends Fragment {
    private String id;
    private MyApp app;
    private Context ctx;
    private RecyclerView rv;
    private ArrayList<Product> products;

    public PageFragment () {
    }

    public void setID (String _id) {
        id = _id;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ctx = getContext();
        app = (MyApp) getActivity().getApplicationContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.product_list, container, false);

        rv = (RecyclerView) rootView.findViewById(R.id.recycler_view);
        rv.setHasFixedSize(true);

        if (app.product == null) {
            app.product = new HashMap<>();
        }

        **// id is null after re-create**
        products = app.product.get(id);

        ProductAdapter adapter = new ProductAdapter(products, ctx, app);
        rv.setAdapter(adapter);
        rv.setLayoutManager(new GridLayoutManager(getActivity(), 4));

        return rootView;
    }


}