在RecyclerView

时间:2018-01-25 19:03:51

标签: java android android-recyclerview singleton adapter

我正在使用RecyclerView制作待办事项列表应用。单击任务时,将出现一个对话框,用户将能够编辑任务的名称。一旦用户存在对话框,我想在RecyclerView中反映任务的变化。

我有一个名为ListActivity的活动,它托管一个包含RecyclerView的片段。同样,我在名为AlertDialog的类中创建了EditTaskFragment。最后,我将arraylist中的所有任务对象存储在名为Singleton的{​​{1}}类中。

在用户更改TaskLabString RecyclerViewonActivityResult()中的任务项的名称后,检索ListFragment。但我不确定如何更新存储在Singleton中的任务对象,然后如何让ViewHolder更改任务项的Title字段。

我的ListFragment类

public class ListFragment extends Fragment {

private static final int REQUEST_TITLE = 0;
private RecyclerView mRecyclerView;
private TaskAdapter mAdapter;

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

    mRecyclerView = view.findViewById(R.id.the_task_list);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    updateUI();

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_OK && requestCode == ListFragment.REQUEST_TITLE) {
       // Do something.
    }

}

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

public void updateUI() {
    List<Task> taskList = TaskLab.get(getActivity()).getTaskList();

    if(mAdapter == null) {
        mAdapter = new TaskAdapter(taskList);
        mRecyclerView.setAdapter(mAdapter);

    }else {
        mAdapter.notifyDataSetChanged();
    }
}

private class TaskAdapter extends RecyclerView.Adapter<TaskHolder>{

    private List<Task> mTaskList;

    public TaskAdapter(List<Task> taskList) {

        mTaskList = taskList;
    }

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

    @Override
    public void onBindViewHolder(TaskHolder holder, int position) {
        Task currentTask = mTaskList.get(position);
        holder.bindData(currentTask);
    }

    @Override
    public int getItemCount() {
        return mTaskList.size();
    }
}

private class TaskHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    private Task mTask;
    private TextView mTaskTitle;
    private CheckBox mSolved;

    private static final String DIALOG = "edit_dialog";

    public TaskHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);

        mTaskTitle = itemView.findViewById(R.id.task_title);
        mSolved = itemView.findViewById(R.id.task_solved);
    }

    public void bindData(Task task) {
        mTask = task;
        mTaskTitle.setText(mTask.getTitle());
        mSolved.setChecked(mTask.isSolved());
    }

    @Override
    public void onClick(View view) {
        FragmentManager manager = getFragmentManager();
        EditTaskFragment dialog = EditTaskFragment.newInstance(mTask.getTitle());
        dialog.setTargetFragment(ListFragment.this, REQUEST_TITLE);
        dialog.show(manager, DIALOG);
    }
}

}

我的EditTaskFragment类

public class EditTaskFragment extends DialogFragment {

private EditText mTaskTitle;
private String mTaskName;

private static final String ARG_TASK_TITLE = "task_title";
public static final String EXTRA_TASK_TITLE = "list_app_task_title";

private void sendResult(int resultCode, String taskName) {
    if(getTargetFragment() == null) {
        return;
    }

    Intent intent = new Intent();
    intent.putExtra(EXTRA_TASK_TITLE,taskName);

    getTargetFragment().onActivityResult(getTargetRequestCode(),resultCode, intent);
}

public static EditTaskFragment newInstance(String taskName) {

    Bundle args = new Bundle();
    args.putString(ARG_TASK_TITLE, taskName);

    EditTaskFragment fragment = new EditTaskFragment();
    fragment.setArguments(args);

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mTaskName = getArguments().getString(ARG_TASK_TITLE);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_edit_task_dialog, null);
    mTaskTitle = view.findViewById(R.id.edit_task_title);
    mTaskTitle.setText(mTaskName);

    return new AlertDialog.Builder(getActivity())
            .setView(view)
            .setTitle(R.string.edit_task_dialog_title)
            .setPositiveButton(R.string.edit_task_dialog_positive, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    sendResult(Activity.RESULT_OK, mTaskTitle.getText().toString());
                }
            })
            .setNegativeButton(R.string.edit_task_dialog_negative, null)
            .create();
}
}

My Singleton类 - TaskLab

public class TaskLab {

private static TaskLab sTaskLab;
private List<Task> mTaskList;

private TaskLab(Context context){
    mTaskList = new ArrayList<>();
    for(int i = 0; i < 10; i++) {
        Task task = new Task();
        task.setTitle("Task #" + i);
        task.setSolved(i%2 == 0);
        mTaskList.add(task);
    }
}

public static TaskLab get(Context context) {
    if(sTaskLab == null) {
        sTaskLab = new TaskLab(context);
    }

    return sTaskLab;
}

public List<Task> getTaskList(){
    return mTaskList;
}

public Task getTask(int id){
    for(Task task : mTaskList) {
        if(task.getId().equals(id)){
            return task;
        }
    }

    return null;
}
}

1 个答案:

答案 0 :(得分:0)

EditTaskFragment.sendResult()中,您正在调用目标片段的onActivityResult()这是错误的onActivityResult()意味着由框架而不是程序员调用。 您需要做的是使用片段的封闭活动,以便在片段之间传递数据

  1. 定义界面TaskChangedListener
  2. 让您的活动实施它,以及ListFragment
  3. EditTaskFragment.sendResult()中,使用((TaskChangedListener) getActivity()).onTaskChanged(taskName)
  4. 在活动的onTaskChanged()中,您需要找到ListFragment(您可以从片段管理器中通过标记找到它),然后调用它{{1}从那里开始。
  5. onTaskChanged()的{​​{1}}中执行:

    列出taskList = TaskLab.get(getActivity())。getTaskList(); mAdapter.clear(); mAdapter.addAll(TASKLIST); mAdapter.notifyDataSetChanged();

  6. 您需要将onTaskChanged()ListFragment方法添加到clear()课程中:

    addAll()

    下一步