如何在一个活动中处理相同布局的两个recyclerview?

问题描述 投票:0回答:1

我正在项目中使用MVVM结构。在一项活动中,我有两个recyclerviews和一个适配器。它们都使用相同的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都获得相同类别“未排序”的数据,但是数据通过两种不同的算法。如果我这样做,那么两个recyclerviews将具有通过第二种算法得到的相同数据。

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

 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。然后,在第二个新适配器中,我也在viewholder类中对其进行了更改。我的第一个适配器看起来像:

 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);
    }
}

当我为每个recyclerview设置不同的适配器时,什么都没有改变。与之前的情况一样,只有第二个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;
    }
}
}

Dao:

@Query ("SELECT * FROM Unsorted WHERE date = :currentDate")
LiveData<List<Unsorted>> getUnsortedWhereDateIs (String currentDate);
java android android-recyclerview recycler-adapter
1个回答
0
投票

您的问题是,您正在观察单个数据源,因此在两个适配器中始终具有相同的数据。您需要在视图模型中添加另一个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)
}

接下来,在您的片段/活动中:

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);                           
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.