Android 转到recyclerview的片段,当我按下back按钮时,recyclerview再次添加相同的数据

Android 转到recyclerview的片段,当我按下back按钮时,recyclerview再次添加相同的数据,android,android-fragments,android-recyclerview,Android,Android Fragments,Android Recyclerview,当用户单击链接时,我正在从RecyclerView调用另一个片段以显示其内容,我使用fragmentTransaction.addToBackStack(null)返回上一个片段当我删除此链接时没有问题,但backpage未保存,请帮助我 所以这里我想回到后面的页面,但不重新添加数据 我的第二个问题是,当我转到第二个片段时,我想在工具栏/操作栏上添加backbutton图标 片段一: @Override public View onCreateView(LayoutInflater i

当用户单击链接时,我正在从RecyclerView调用另一个片段以显示其内容,我使用
fragmentTransaction.addToBackStack(null)
返回上一个片段当我删除此链接时没有问题,但backpage未保存,请帮助我

所以这里我想回到后面的页面,但不重新添加数据

我的第二个问题是,当我转到第二个片段时,我想在工具栏/操作栏上添加backbutton图标

片段一:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_notification, container, false);
        recyclerView=(RecyclerView)view.findViewById(R.id.notification_recyclerview);
        load_data_from_server();
        linearLayoutManager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        notificationAdapter=new NotificationAdapter(notification,getActivity());
        recyclerView.setAdapter(notificationAdapter);


        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                //super.onScrolled(recyclerView, dx, dy);
                if(linearLayoutManager.findLastCompletelyVisibleItemPosition()==notification.size()-1)
                {
                    databaseHelper=new DatabaseHelper(getActivity());
                    Cursor all_data=databaseHelper.get_all_scrolled_data_from_news(notification.get(notification.size()-1).getServer_id());
                    if(all_data.getCount()>0)
                    {
                        Toast.makeText(getActivity(),"total"+all_data.getCount(),Toast.LENGTH_SHORT).show();
                        try {
                            while (all_data.moveToNext())
                            {
                                Notification_Data n_data=new Notification_Data(all_data.getInt(0),all_data.getInt(1),all_data.getString(2),all_data.getString(3));
                                notification.add(n_data);
                            }
                            notificationAdapter.notifyDataSetChanged();

                        }
                        finally
                        {
                            all_data.close();
                        }
                    }
                    else
                    {
                        Toast.makeText(getActivity(),"No more data available",Toast.LENGTH_SHORT).show();
                    }

                }

            }
        });

        swipeRefreshLayout=(SwipeRefreshLayout)view.findViewById(R.id.notification_swiperefresh);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                refreshItems();
            }
        });




        return view;
    }
回收器适配器等级:

NotificationDescriptionFragment noti_desc=new NotificationDescriptionFragment();
                Bundle bundle=new Bundle();
                bundle.putInt("server_id",noti_data.get(position).getServer_id());
                noti_desc.setArguments(bundle);
                MainActivity mainActivity=(MainActivity)context;
                fragmentTransaction=mainActivity.getSupportFragmentManager().beginTransaction();
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.replace(R.id.main_container,noti_desc);
                fragmentTransaction.commit();





public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        //int value=getArguments().getInt("server_id");

        View view= inflater.inflate(R.layout.fragment_notification_description, container, false);
        TextView notification_content=(TextView) view.findViewById(R.id.notification_content);


        databaseHelper=new DatabaseHelper(getActivity());
        Cursor c=databaseHelper.get_contnt_by_id_from_news(server_id);
        while (c.moveToNext())
        {

            notification_content.setText(Html.fromHtml(c.getString(0)));

        }


        return view;
    }
截图:

1-我用回收器视图创建列表的第一个片段

现在,当用户单击任何要转到另一个内容片段的项目时,这是 第二段:

当我按下第二个片段的“后退”按钮时 将相同数据添加到回收器视图中的屏幕截图如下:

添加前清除列表(即您的案例中的通知)。

如果关闭并重新打开应用程序会发生什么情况。启动后是否显示唯一或重复的项目?我在fragment1列表中显示了新的\u数据;在oncreate方法中,我将其初始化为new_data=new ArrayList();投票支持你