Tôi đang học MVP và bị nhầm lẫn nơi và cách tôi nên triển khai onClickListener trong khi không hủy hoại khái niệm mvp tại đây.Triển khai onItemClickListener sử dụng mẫu MVP
Làm theo hướng dẫn sau: https://android.jlelse.eu/recyclerview-in-mvp-passive-views-approach-8dd74633158
Triển khai của tôi.
Adaptor:
public class RepositoriesRecyclerAdapter extends RecyclerView.Adapter<RepositoriesRecyclerAdapter.RepoViewHolder> {
private final RepositoriesListPresenter presenter;
public RepositoriesRecyclerAdapter(RepositoriesListPresenter repositoriesPresenter) {
this.presenter = repositoriesPresenter;
}
@Override
public RepositoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RepositoryViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.cell_repo_view, parent, false));
}
@Override
public void onBindViewHolder(RepositoryViewHolder holder, int position) {
presenter.onBindRepositoryRowViewAtPosition(position, holder);
}
@Override
public int getItemCount() {
return presenter.getRepositoriesRowsCount();
}
}
RepositoryViewHolder của
public class RepositoryViewHolder extends RecyclerView.ViewHolder implements RepositoryRowView {
TextView titleTextView;
TextView starsCountTextView;
public RepositoryViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.repoTitleText);
starsCountTextView = itemView.findViewById(R.id.repoStarsCountText);
}
@Override
public void setTitle(String title) {
titleTextView.setText(title);
}
@Override
public void setStarCount(int starCount) {
starsCountTextView.setText(String.format("%s ★", starCount));
}
}
RepositoryRowView
interface RepositoryRowView {
void setTitle(String title);
void setStarCount(int starCount);
}
Tất cả các hướng dẫn mà tôi thấy là về việc tạo đối tượng OnClickListener trong Adaptor và sau đó sử dụng nó trong ViewHolder, nhưng trong này thực hiện, tôi ghi đè tất cả các chức năng bộ điều hợp trong trình bày của tôi và vượt qua trênClickListener (công cụ liên quan đến Android) sẽ mâu thuẫn với mẫu mvp. Phải làm gì trong trường hợp này. Có lẽ ai đó có thể viết một giải pháp - thực sự bối rối.
Mục tiêu chính của tôi sẽ được bấm vào một mục recyclerview và nhận được tên mục (thông qua bánh mì nướng)
Đặt ví dụ, nếu tôi muốn thay đổi màu của mục đã chọn? Tôi không thể chuyển mục trở lại trình bày (như bạn đã đề cập trước đó), nhưng tôi có thể chuyển vị trí (như bạn đã trình bày), bây giờ VIEW (fragment) thay đổi màu sắc hoặc thứ gì đó với vị trí đó như thế nào? – JoshuaMad
@JoshuaMad Bạn có thể sử dụng sự trừu tượng mà bạn đã có sẵn. Bạn có thể chuyển vị trí và RepositoryRowView (được thực hiện bởi ViewHolder của bạn, vì vậy bạn chỉ có thể thêm "this" làm tham số) cho người trình bày. Hơn bạn có thể thêm phương thức vào lớp RepositoryRowView. Cái gì đó là "markRowAs ..." và việc thực hiện nó sẽ thực hiện thay đổi màu sắc. –