在两个选项卡之间刷新RecyclerView

时间:2015-09-16 18:39:36

标签: android android-adapter android-recyclerview android-tablayout

我的应用中有两个标签。因此,当我从一个选项卡的RecyclerView中删除一个列表时,它应该被添加到RecyclerView中。我已从第一个标签中删除了该列表,并将其添加到第二个标签的Recyclerview列表中。但是,Recyclerview不会改变。当我进入下一个活动并返回选项卡时,它会显示更改。 简而言之,我只需要更新发送到适配器的数据。

我已经使用过了

  

notifyDataSetChanged()和swapAdapter()

但没有奏效。 有谁知道谁让它工作? 谢谢 !

Tab 1片段:

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_active_patients, container, false);
    activePatientsList = (RecyclerView) view.findViewById(R.id.active_patients_recyclerview);
    activePatientsList.setLayoutManager(new LinearLayoutManager(getActivity()));
    final ActivePatientRecyclerAdapter activePatientRecyclerAdapter = new ActivePatientRecyclerAdapter(getActivity()
            ,activePatientList);

    activePatientRecyclerAdapter.setOptionsCLickedListener(new ActivePatientRecyclerAdapter.OptionsCLickedListener() {
        @Override
        public void popUpClicked(final View view, final int position) {
            PopupMenu popupMenu = new PopupMenu(getActivity(), view);
            MenuInflater menuInflater = popupMenu.getMenuInflater();
            menuInflater.inflate(R.menu.menu_active_patients_additional_options,
                    popupMenu.getMenu());
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    int id = menuItem.getItemId();
                    if (id == R.id.action_discharge) {

                        SQLiteDBUse db = new SQLiteDBUse(getActivity());
                        long result = db.updateName("4", in_patients_name[position]);
                        if (result > -1) {

                            Snackbar.make(view, in_patients_name[position] + " Removed", Snackbar.LENGTH_SHORT).show();

                            activePatientList.remove(position);
                            activePatientRecyclerAdapter.notifyItemRemoved(position);
                        }
                        return false;
                    }
                       return false;
                }

                ;
            });
            popupMenu.show();

        }
    });
    activePatientsList.setAdapter(activePatientRecyclerAdapter);

    return view;
}

Tab 2片段:

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


    outPatientList = (RecyclerView) view.findViewById(R.id.out_patients_recyclerview);
    outPatientList.setLayoutManager(new LinearLayoutManager(getActivity()));
    patientListRecyclerAdapter = new PatientListRecyclerAdapter(getActivity()
            , patientList);
        }
    });
    outPatientList.setAdapter(patientListRecyclerAdapter);
    return view;
}

@Override
public void onResume() {
    super.onResume();
    patientListRecyclerAdapter.notifyDataSetChanged();
}

0 个答案:

没有答案