如何修复片段重叠?

时间:2019-06-27 07:41:02

标签: java android android-fragments

我在Activity中仅动态创建了一个片段,并将数据添加到片段中。当我改变我的数据片段再次创建。当我想返回活动状态时,他们将显示第一个先前的数据,然后在我按返回按钮时进入“主要活动”。当我按下“后退”按钮时,它们仅进入主要活动而不是先前的数据。

 SearchFragment searchFragment = new SearchFragment(MainActivity.this);
                searchFragment.setArguments(extra);
                fragmentTransaction = manager.beginTransaction();
                fragmentTransaction.add(R.id.myFragmwent_layout,searchFragment,"first");
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
 SearchFragment searchFragment = (SearchFragment) 
 manager.findFragmentByTag("first");
                fragmentTransaction = manager.beginTransaction();
                if (searchFragment!=null){
                    fragmentTransaction.remove(searchFragment);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                }

搜索片段类

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (getArguments()!=null){
            keyword = getArguments().getString("keyword");
        }
        View view;
        view = inflater.inflate(R.layout.fragment_search, container, false);
        mRecyclerView = view.findViewById(R.id.recycler_view_Frag);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        categoryItemArrayList = new ArrayList<>();

        sQueue = Volley.newRequestQueue(getContext());
        JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.POST, url+keyword,
                null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        try {
                            for (int i = 0; i < response.length(); i++) {
                                JSONObject data = response.getJSONObject(i);
                                String creatorName = data.getString("name");
                                String imageUrl = data.getString("image");
                                String articleId = data.getString("article_id");
                                categoryItemArrayList.add(new CategoryItem(imageUrl, creatorName, articleId));
                            }
                            mCustomadapter = new CustomAdapter(getContext(), categoryItemArrayList);
                            mRecyclerView.setAdapter(mCustomadapter);
                            mCustomadapter.setOnItemClickListener(SearchFragment.this);
                            mCustomadapter.updateData();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), "data not found", Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        });
        sQueue.add(arrayRequest);

        return view;
    }

2 个答案:

答案 0 :(得分:1)

在您的情况下,请使用替换而不是添加

SearchFragment searchFragment = new SearchFragment(MainActivity.this);
                searchFragment.setArguments(extra);
                fragmentTransaction = manager.beginTransaction();
                fragmentTransaction.replace(R.id.myFragmwent_layout,searchFragment,"first");
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

答案 1 :(得分:1)

FragmentTransaction.add()将多个片段添加到一个容器中,它们将一个接一个地分层放置。如果您的片段具有透明背景,您将看到这种效果,并且能够同时与多个片段进行交互。

如果您使用FragmentTransaction.replace(R.id.container,fragment),它将删除容器中已经存在的所有片段,并将新片段添加到同一容器中。

因此,您需要修改代码并使用fragmentTransaction.replace而不是fragmentTrasaction.add

SearchFragment searchFragment = new SearchFragment(MainActivity.this);
searchFragment.setArguments(extra);
fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.myFragmwent_layout,searchFragment,"first");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();