如何在没有finish()的情况下从另一个类刷新RecyclerView

时间:2018-03-15 19:12:53

标签: android android-fragments android-recyclerview android-adapter

我在MainActivity类中创建了一个片段,它显示了带有ArrayList的RecyclerView。在NewNote类中,我添加了一个新项目列表,并将更改后的列表传递给内部存储上的文件。当我在添加新笔记后从MainActivity返回NewNote时,RecyclerView会显示旧列表而不是新列表。当我重新启动应用程序时,MainActivity显示新列表,这要归功于onCreate方法,该方法使用内部存储中的新列表再次加载片段。 我的问题是如何重新加载显示列表,就像我重新启动我的应用程序。我在堆栈上读了很多类似的问题(大多数答案是notifyDataSetChanged()),但我不知道在我的情况下该做什么,尝试了我发现的一切。 这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {
private FloatingActionButton addNewNoteButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListFragment fragment = new ListFragment();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragmencik, fragment);
    fragmentTransaction.commit();

    addNewNoteButton = findViewById(R.id.add_new_note);
    addNewNoteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, NewNote.class);
            startActivity(intent);
        }
    });
}
}

NewNote.java

public class NewNote extends AppCompatActivity {

private EditText text;
private ArrayList<Note> noteArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_note_view);
    noteArrayList = new ArrayList<>();
    text= findViewById(R.id.text);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
if(item.getIcon().getConstantState().equals(getResources().getDrawable(R.drawable.ic_save_white_36dp).getConstantState()))
    {
        Note note = new Note(text.getText().toString());
        addNewNoteToList(note);
    }
    return super.onOptionsItemSelected(item);
}

public void addNewNoteToList(Note note)
{
    SharedPreferences sharedPreferences = getSharedPreferences("notes file", MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPreferences.getString("notes list", null);
    Type type= new TypeToken<ArrayList<Note>>() {}.getType();
    noteArrayList = gson.fromJson(json, type);
    if(noteArrayList==null)
        noteArrayList=new ArrayList<>();
    noteArrayList.add(note);

    SharedPreferences.Editor editor = sharedPreferences.edit();
    Gson gson2 = new Gson();
    String json2 = gson2.toJson(noteArrayList);
    editor.putString("notes list", json2);
    editor.apply();

}
}

ListFragment.java

public class ListFragment extends Fragment {

private ArrayList<Note> noteArrayList;
private ListAdapter listAdapter;

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

    loadArrayList();

    View view = inflater.inflate(R.layout.fragment_list, container, false);

    RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);

    listAdapter = new ListAdapter(noteArrayList);
    recyclerView.setAdapter(listAdapter);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);

    return view;
}

public void loadArrayList()
{

    SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("notes file", Context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPreferences.getString("notes list", null);
    Type type= new TypeToken<ArrayList<Note>>() {}.getType();
    noteArrayList = gson.fromJson(json, type);
}
}

ListAdapter.java

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
private ArrayList<Note> mCustomObjects;

public class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private TextView mItemText;

    public ListViewHolder(View itemView) {

        super(itemView);
        mItemText = itemView.findViewById(R.id.itemText);
        // mItemImage = itemView.findViewById(R.id.itemImage);
    }}

@Override
public int getItemCount() {
    int size=0;
    if(mCustomObjects!=null)
        size=mCustomObjects.size();
    return size;
}

public ListAdapter(ArrayList<Note> arrayList) {
    mCustomObjects = arrayList;
}

@Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
    return new ListViewHolder(view);
}

@Override
public void onBindViewHolder(ListViewHolder holder, int position) {
    if(mCustomObjects!=null) {
        Note note = mCustomObjects.get(position);
        holder.mItemText.setText(note.getText());
    }
}}

1 个答案:

答案 0 :(得分:0)

你可以做两件事:

第一个。如果你要添加一个新的音符,但你有一个返回(返回)按钮,你可以启动一个activityForResult并比较是否已经添加了音符然后刷新我的列表,用户按下返回就是什么都不做。

第二个。您可以使用eventbus发布事件,以通知回收站视图必须更新它,然后刷新数据。

我假设您使用滑动刷新,如果没有实现它。 https://developer.android.com/training/swipe/add-swipe-interface.html