片段显示了海誓山盟的顶部两倍 [英] Fragment shows twice on top of eachother

查看:121
本文介绍了片段显示了海誓山盟的顶部两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即时通讯显示片段在我的活动,但似乎有些片段显示(instatiating)的两倍。我双重检查我的code,但在我看来都应该没问题。问题是,当我告诉片段的确​​定,但呈现出另一个片段,并呈现出相同的片段后,它就像重装调用同一个片段一遍,它显示了更新后的数据 - 尽管没有数据更新已完成

I im displaying fragments in my activity, but seems that some fragments are displaying(instatiating) twice. I'm double checking my code but and in my opinion all should be ok. The issue is when I show the fragment its ok, but after showing another fragment and showing the same fragment it's like reloading the calling the same fragment all over again and it shows updated data - although no data updating was done.

这是我打电话给我的片段:

this is how I call my fragments:

public void showFragment(Fragment fragment, Bundle bundle) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        fragment.setRetainInstance(true);
        //TODO animation
        if (!fragment.isVisible()) {
            //  ft.setCustomAnimations(R.anim.slide_in, R.anim.slide_out, R.anim.slide_in, R.anim.slide_out);
            fragment.setArguments(bundle);
            ft.replace(R.id.main_frag_container, fragment);
            ft.commit();
        }
        dismissLoading();
    }

这里是我的片段之一:

And here is one of my fragments:

import android.app.Fragment;
public class ProductFragment extends Fragment implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {
        private final String TAG = ProductFragment.class.getSimpleName();

        private View view;

        private int index = -1;
        private int top = 0;

        private Spinner categorySpinner;
        private ListView listView;
        private ArrayAdapter<CharSequence> cityAdapter, categoryAdapter;
        private ArrayList<Product> items;
        private ProductsAdapter adapter;


        private OnProductSelected productSelectedListener;

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            if (activity != null) {
                productSelectedListener = (OnProductSelected) activity;
            }
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            view = inflater.inflate(R.layout.fragment_products, container, false);
            Log.d(TAG, "PROducts frag started " + getArguments().getSerializable(ActivityConstants.PRODUCT_ITEMS));
            listView = (ListView) view.findViewById(R.id.products_listView);

            if (getArguments().getSerializable(ActivityConstants.PRODUCT_ITEMS) != null) {
                loadList((ArrayList<Product>) getArguments().getSerializable(ActivityConstants.PRODUCT_ITEMS));
            }

            initWidgets(view);
            return view;
        }

        public void loadList(ArrayList<Product> data) {
            items = data;
            adapter = new ProductsAdapter(getActivity(), items);
            Log.d(TAG, "ITEMS SIZE" + items.size());
            listView.setAdapter(adapter);

        }

        @Override
        public void onPause() {
            super.onPause();
            try {
                index = listView.getFirstVisiblePosition();
                View v = listView.getChildAt(0);
                top = (v == null) ? 0 : v.getTop();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        @Override
        public void onResume() {
            super.onResume();

    //        setListAdapter(mAdapter);
            if (index != -1) {
                listView.setSelectionFromTop(index, top);
            }


        }

        /**
         * Initialize fragment widgets
         */
        private void initWidgets(View view) {
            categorySpinner = (Spinner) view.findViewById(R.id.products_spinner_cat);
            getActivity().invalidateOptionsMenu();
            ((MainActivity) getActivity()).initActionBar(true);
            listView.setOnItemClickListener(this);

            //set citySpinner adapter
            cityAdapter = ArrayAdapter.createFromResource(getActivity(),
                    R.array.cities, android.R.layout.simple_spinner_item);
            cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            //set categorySpinner adapter
            categoryAdapter = ArrayAdapter.createFromResource(getActivity(),
                    R.array.categores, android.R.layout.simple_spinner_item);
            categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            categorySpinner.setAdapter(categoryAdapter);
            categorySpinner.setOnItemSelectedListener(this);

            categorySpinner.setSelection(0);
            ((MainActivity) getActivity()).dismissLoading();
            Log.d(TAG, "Fragment inited");
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "onItemClick " + position);
            productSelectedListener.onProductSelected(items.get(position));
        }

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.d(TAG, "POSITION " + position);

            if (position == 0) {
                position += 1;
            }
            getListing(getResources().getStringArray(R.array.categores)[position].toLowerCase());


        }


        private void getListing(String category) {
            AppHttpClient.getInstance();
            String serverURL = ServerUtil.SERVER_BASE_URL + ServerUtil.PRODUCTS + category;
            Log.d(TAG, "Server url: " + serverURL);
            AppHttpClient.executeMethod(ServerUtil.METHOD_GET_PRODUCTS, serverURL, null, getActivity());
        }


        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Log.d(TAG, parent.getId() + " ");
        }

    }

有人可以帮助我解决这个问题? 谢谢!

Can someone help me solve this? Thanks!

推荐答案

如果您使用您发布从一个片段切换到另一个code,我会说这可能来自你如何让你传递给片段您showFragment()方法。如果你把它传递给showFragment()之前创建一个新的片段,一个新片段被创建并添加在别人前头是正常的。

If you use the code you posted to switch from one fragment to another, I would say it may come from how you get the Fragment you pass to your showFragment() method. If you create a new Fragment before passing it to showFragment(), it is normal that a new fragment is created and added on top of the others.

您应该使用getFragmentManager()。findFragmentByTag(字符串标记),以得到一个片段已经到位,并在片段堆栈的顶部重新添加。

you should use getFragmentManager().findFragmentByTag(String Tag) to get a fragment already in place and re-add it on top of the Fragment stack.

这篇关于片段显示了海誓山盟的顶部两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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