问题
That is it, can i remove a view from firebase recycler view adapter, but not deleting in the database?How?
final FirebaseRecyclerAdapter<Game,GameViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Game, GameViewHolder>(
Game.class,
R.layout.row,
GameViewHolder.class,
mdatabase
) {
@Override
protected void populateViewHolder(GameViewHolder viewHolder, Game model, final int position) {
viewHolder.setTitle(model.getGame_name());
viewHolder.setJugado(model.getJugado());
viewHolder.setCreator(model.getCreator_name());
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
回答1:
FirebaseRecyclerAdapter doesn't support this. The whole point of FirebaseRecyclerAdapter is that it stays in sync with the contents of the location that you originally gave it. If something deletes data from the database, it will be automatically and unconditionally deleted from your adapter.
If you want this behavior, you will have to implement your own RecyclerView adapter, preferably one that does not use an active listeners to be notified of every change to the location of the data. You would have to listen to the entire contents just once, then send that to an adapter to populate a RecyclerView.
回答2:
And how about this in ViewHolder?
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemView.setVisibility(View.INVISIBLE);
}
});
回答3:
Yes you can. You have to add this in your GameViewHolder
class:
- Create a function and get reference of your cardview
- Create a layout params and Create an instance of the parent of your cardview then set the height and the width equals to 0
public void Remove() {
cardView = mView.findViewById(R.id.cardview);
cardView.setLayoutParams(new FrameLayout.LayoutParams(0,0));
}
- Use the function
viewHolder.remove();
来源:https://stackoverflow.com/questions/41925871/delete-data-from-firebase-recycler-view-adapter-without-deleting-in-the-database