有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我在我的项目中使用了MVVM结构。在一个活动中,我有两个recyclerviews和一个适配器。他们都使用相同的任务\u布局。我这样设置数据:

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\u布局,其中我只更改了元素的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);

作为交换:

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

共 (1) 个答案

  1. # 1 楼答案

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