【问题标题】:going to fragment from recyclerview and when i press back button recyclerview adding same data again从 recyclerview 分片,当我按下返回按钮时,recyclerview 再次添加相同的数据
【发布时间】:2017-02-05 18:44:23
【问题描述】:

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

所以在这里我想返回页面但不重新添加数据。

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

片段一:

 @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;
    }

recyclerAdapter 类:

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 - 我使用回收站视图创建列表的第一个片段

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

当我从第二个片段按下返回按钮时 添加到回收站视图中的相同数据是屏幕截图:

【问题讨论】:

  • 如果您关闭并重新打开应用程序会发生什么。启动后是否显示唯一或重复的项目?

标签: android android-fragments android-recyclerview


【解决方案1】:

在添加之前清除列表(即您的情况下的通知)。

【讨论】:

  • 我在我的 fragment1 List new_data 中完成了这个;在 oncreate 方法中,我将其初始化为 new_data=new ArrayList();投票给你
猜你喜欢
  • 2015-05-30
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
相关资源
最近更新 更多