我如何为我的recyclerview代码创建多个选择?



我想在recyclerview中选择多个项目

我有数据检索它从firebase数据库到recyclerview然后我想选择许多项目从回收器视图更新它

在youtube中有一些本地数据的方法我不能为我的数据库做

我怎么能做到呢?

这段代码只是检索数据从数据库到recyclerView我应该添加什么来选择许多项目?

MainActivity

private RecyclerView rv_worker;
private FirebaseDatabase db;
private ArrayList <WorkerModel> list;
private Worker_Add_money_Adapter worker_add_money_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.worker_add_money);
rv_worker = findViewById(R.id.RV);
db = FirebaseDatabase.getInstance();
rv_worker.setHasFixedSize(true);
rv_worker.setLayoutManager(new LinearLayoutManager(this));

getList();
}
private void  getList()
{
db.getReference("Employees").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
list=new ArrayList <>();
worker_add_money_adapter = new Worker_Add_money_Adapter(Worker_add_money.this,list
, Worker_add_money.this);
rv_worker.setAdapter(worker_add_money_adapter);
for (DataSnapshot child :snapshot.getChildren())
{
WorkerModel workerModel =child.getValue(WorkerModel.class);
list.add(workerModel);
worker_add_money_adapter.notifyItemInserted(list.size()-1);
worker_add_money_adapter.getItemCount();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(Worker_add_money.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onItemClick(final int position) {
}

适配器

public class Worker_Add_money_Adapter extends RecyclerView.Adapter <Worker_Add_money_Adapter.ViewHolder>  {
private Context context;
private List <WorkerModel> list;
private RecyclerView_InterFace recyclerView_interFace;
public Worker_Add_money_Adapter(Context context, List <WorkerModel> list, RecyclerView_InterFace recyclerView_interFace) {
this.context = context;
this.list = list;
this.recyclerView_interFace = recyclerView_interFace;
}
@NonNull
@Override
public Worker_Add_money_Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new Worker_Add_money_Adapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card_worker_add_money, parent, false));
}
@Override
public void onBindViewHolder(@NonNull Worker_Add_money_Adapter.ViewHolder holder, final int position) {
final WorkerModel workerModel = list.get(position);
holder.tv_name.setText(workerModel.getName());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_name;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tx_name);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recyclerView_interFace.onItemClick(getAdapterPosition());
}
});
}
}

}

您要求的这个需求需要分为3部分

  1. 从firebase获取数据并将列表设置为recycle view

    我猜你已经成功地完成了这一步

  2. 使回收器视图选择多个瓷砖

    按此链接实现

  3. 获取列表中所有选中的项目

    • 在你的适配器类中全局声明

      List<YourDataModel> list = new ArrayList<YourDataModel>();
      
    • 实现onClick或OnTap回收站视图

      recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
      @Override
      public void onClick(View view, int position) {
      }
      @Override
      public void onLongClick(View view, int position) {
      list.add(yourDataList.get[position]);
      }
      

      }));

    • 现在列表将包含所有选中项目的数据

相关内容

  • 没有找到相关文章

最新更新