gpt4 book ai didi

java - 如何在一个 Activity 中处理相同布局的两个回收 View ?

转载 作者:行者123 更新时间:2023-12-01 17:01:11 25 4
gpt4 key购买 nike

我在我的项目中使用 MVVM 结构。在一项 Activity 中,我有两个回收器 View 和一个适配器。它们都使用相同的tasks_layout。我这样设置数据:

final UnsortedAdapter adapter = new UnsortedAdapter();
eatFrog.setHasFixedSize(true);
eatFrog.setLayoutManager(new LinearLayoutManager(this));
eatFrog.setAdapter(adapter);

biologicalRhythms.setAdapter(adapter);
biologicalRhythms.setLayoutManager(new LinearLayoutManager(this));
biologicalRhythms.setHasFixedSize(true);

viewModel.getUnsortedWhereDateIs(currentDate).observe(this, new Observer<List<Unsorted>>() {
@Override
public void onChanged(List<Unsorted> unsorted) {
adapter.setData(EatAFrog(unsorted));
eatAFrogList = EatAFrog(unsorted);

adapter.setData(BiologicalRhythms(unsorted));
biologicalList = BiologicalRhythms(unsorted);

}
});

重点是,每个recyclerview都获取同一类“Unsorted”的数据,但数据经过了两种不同的算法。如果我这样做,那么两个 recyclerview 将具有相同的数据,经过第二个算法。

我尝试做不同的事情。首先,我尝试将适配器的另一个实例用于第二个回收器 View ,但在这种情况下,第一个回收器 View 中没有显示数据:

 final UnsortedAdapter adapter = new UnsortedAdapter();
eatFrog.setHasFixedSize(true);
eatFrog.setLayoutManager(new LinearLayoutManager(this));
eatFrog.setAdapter(adapter);
final UnsortedAdapter adapter2 = new UnsortedAdapter();
biologicalRhythms.setAdapter(adapter2);
biologicalRhythms.setLayoutManager(new LinearLayoutManager(this));
biologicalRhythms.setHasFixedSize(true);

其次,我尝试创建另一个适配器类,与第三个相同。我还创建了另一个布局 - tasks2_layout,其中我只更改了元素的 id。然后在第二个新适配器中,我还在 View 持有者类中更改了它们。我的第一个适配器如下所示:

 List<Unsorted> list = new ArrayList<>();

@NonNull
@Override
public UnsortedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.tasks_layout , parent, false);
return new UnsortedAdapter.UnsortedViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull UnsortedViewHolder holder, int position) {
Unsorted data = list.get(position);
holder.title.setText(data.getName());
holder.date.setText(data.getDate());
holder.category.setText(String.valueOf(data.getCategory()));
holder.attach.setText(String.valueOf(data.isAttach()));
holder.to.setText(String.valueOf(toTime(data.getDuration() + data.getTimeBegin())));
holder.from.setText(String.valueOf(toTime(data.getTimeBegin())));
}

public void setData(List<Unsorted> unsortedList){
this.list = unsortedList;
notifyDataSetChanged();
}

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

class UnsortedViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private TextView date;
private TextView from;
private TextView to;
private TextView category;
private TextView attach;

public UnsortedViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv_title);
date = itemView.findViewById(R.id.tv_date);
from = itemView.findViewById(R.id.tv_from2);
to = itemView.findViewById(R.id.tv_to2);
category = itemView.findViewById(R.id.tv_category);
attach = itemView.findViewById(R.id.tv_attach);
}
}

第二个,新的:

    List<Unsorted> list = new ArrayList<>();

@NonNull
@Override
public UnsortedViewHolder2 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.tasks2_layout , parent, false);
return new UnsortedAdapter2.UnsortedViewHolder2(itemView);
}

@Override
public void onBindViewHolder(@NonNull UnsortedViewHolder2 holder, int position) {
Unsorted data = list.get(position);
holder.title.setText(data.getName());
holder.date.setText(data.getDate());
holder.category.setText(String.valueOf(data.getCategory()));
holder.attach.setText(String.valueOf(data.isAttach()));
holder.to.setText(String.valueOf(toTime(data.getDuration() + data.getTimeBegin())));
holder.from.setText(String.valueOf(toTime(data.getTimeBegin())));
}

public void setData1(List<Unsorted> unsortedList){
this.list = unsortedList;
notifyDataSetChanged();
}

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

class UnsortedViewHolder2 extends RecyclerView.ViewHolder{
private TextView title;
private TextView date;
private TextView from;
private TextView to;
private TextView category;
private TextView attach;

public UnsortedViewHolder2(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv_title1);
date = itemView.findViewById(R.id.tv_date1);
from = itemView.findViewById(R.id.tv_from3);
to = itemView.findViewById(R.id.tv_to3);
category = itemView.findViewById(R.id.tv_category1);
attach = itemView.findViewById(R.id.tv_attach1);
}
}

当我为每个回收器 View 设置不同的适配器时,没有任何变化。与之前的情况一样,只有第二个 recyclerview 显示一些数据。

final UnsortedAdapter adapter = new UnsortedAdapter();
eatFrog.setHasFixedSize(true);
eatFrog.setLayoutManager(new LinearLayoutManager(this));
eatFrog.setAdapter(adapter);
final UnsortedAdapter2 adapter2 = new UnsortedAdapter();
biologicalRhythms.setAdapter(adapter2);
biologicalRhythms.setLayoutManager(new LinearLayoutManager(this));
biologicalRhythms.setHasFixedSize(true);

在 onChange 中:

 adapter.setData(EatAFrog(unsorted));
eatAFrogList = EatAFrog(unsorted);

adapter2.setData1(BiologicalRhythms(unsorted));
biologicalList = BiologicalRhythms(unsorted);

您能建议我如何解决这个问题吗?感谢您的帮助。

MyViewModel 类:

public class UnsortedViewModel extends AndroidViewModel {
private DatesRepository repository2;
private UnsortedRepository repository;
private SortedRepository repository3;
private LiveData<List<Unsorted>> allUnsorted;

public UnsortedViewModel(@NonNull Application application) {
super(application);
repository3 = new SortedRepository(application);
repository2 = new DatesRepository(application);
repository = new UnsortedRepository(application);
allUnsorted = repository.getAllUnsorted();
}

public LiveData<List<Unsorted>> getUnsortedWhereDateIsAlgo1(String currentDate) throws ExecutionException, InterruptedException{
return repository.getUnsortedWhereDateIs(currentDate);
}
public LiveData<List<Unsorted>> getUnsortedWhereDateIsAlgo2(String currentDate) throws ExecutionException, InterruptedException{
return repository.getUnsortedWhereDateIs(currentDate);
}

MutableLiveData<List<Unsorted>> unsortedWhereDateIsAlgo1 = new MutableLiveData<>();
MutableLiveData<List<Unsorted>> unsortedWhereDateIsAlgo2 = new MutableLiveData<>();

public void insertDate(Dates dates) {
repository2.insertDate(dates);
}

public LiveData<List<Unsorted>> getAllUnsorted() {
return allUnsorted;
}

public void insert(Unsorted data){
repository.insert(data);
}

public void deleteAllUnsorted(){
repository.deleteAllUnsorted();
}

public void deleteWhereDateIs(String currentDate){
repository.deleteWhereDateIs(currentDate);
}

public void insertSorted(Sorted data){
repository3.insertSorted(data);
}
}

存储库:

public class UnsortedRepository {
private UnsortedDao unsortedDao;
private LiveData<List<Unsorted>> allUnsorted;

public UnsortedRepository(Application application){
UnsortedDatabase database = UnsortedDatabase.getInstance(application);
unsortedDao = database.unsortedDao();
allUnsorted = unsortedDao.getAllUnsorted();
}


public LiveData<List<Unsorted>> getUnsortedWhereDateIs(String currentDate)
throws ExecutionException, InterruptedException{
return new
GetUnsortedWhereDateIsAsyncTask(unsortedDao).execute(currentDate).get();
}

private static class GetUnsortedWhereDateIsAsyncTask extends
AsyncTask<String, Void, LiveData<List<Unsorted>>> {
private UnsortedDao unsortedDao;

private GetUnsortedWhereDateIsAsyncTask(UnsortedDao unsortedDao){
this.unsortedDao = unsortedDao;
}
@Override
protected LiveData<List<Unsorted>> doInBackground(String ... strings) {
LiveData<List<Unsorted>> list =
unsortedDao.getUnsortedWhereDateIs(strings[0]);
return list;
}
}
}

道:

@Query ("SELECT * FROM Unsorted WHERE date = :currentDate")
LiveData<List<Unsorted>> getUnsortedWhereDateIs (String currentDate);

最佳答案

您的问题是,您正在观察单个数据源,因此两个适配器中始终具有相同的数据。您需要在 View 模型中添加另一个 LiveData。使用这样的东西:

unsortedWhereDateIsAlgo1 : MutableLiveData<List<Unsorted>>()
unsortedWhereDateIsAlgo2 : MutableLiveData<List<Unsorted>>()

并将算法结果发布到正确的 LiveData 中:

fun algo1() {
//some code/operations
unsortedWhereDateIsAlgo1.postValue(result)
}

fun algo2() {
//some different code/operations
unsortedWhereDateIsAlgo2.postValue(result)
}

接下来,在您的 fragment/Activity 中:

viewModel.getUnsortedWhereDateIsAlgo1(currentDate).observe(this, new Observer<List<Unsorted>>() {
@Override
public void onChanged(List<Unsorted> unsorted) {
final UnsortedAdapter adapter = new UnsortedAdapter();
adapter.setData(EatAFrog(unsorted));
eatFrog.setHasFixedSize(true);
eatFrog.setLayoutManager(new LinearLayoutManager(this));
eatFrog.setAdapter(adapter);
}
});

viewModel.getUnsortedWhereDateIsAlgo2(currentDate).observe(this, new Observer<List<Unsorted>>() {
@Override
public void onChanged(List<Unsorted> unsorted) {
final UnsortedAdapter adapter2 = new UnsortedAdapter();
adapter2.setData(BiologicalRhythms(unsorted));
biologicalRhythms.setAdapter(adapter2);
biologicalRhythms.setLayoutManager(new LinearLayoutManager(this));
biologicalRhythms.setHasFixedSize(true);
}
});

关于java - 如何在一个 Activity 中处理相同布局的两个回收 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61504648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com