我使用了具有可扩展布局的Firebase UI RecyclerView,但是每当我修改Firebase实时数据库中的数据时,列表视图和更新后的数据就会重叠。请帮助我解决此问题。
我尝试使用notifydatasetchanged();使用适配器。
这是发生的事情的图像:
以下是代码:
MainActivity类
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<Item> items = new ArrayList();
FirebaseRecyclerAdapter<Item,ItemViewHolder> adapter;
SparseBooleanArray expandState = new SparseBooleanArray();
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView =findViewById(R.id.lst_item);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
retrieveData();
setData();
}
private void setData() {
Query query = FirebaseDatabase.getInstance().getReference().child("Items");
FirebaseRecyclerOptions<Item> options = new FirebaseRecyclerOptions.Builder<Item>()
.setQuery(query,Item.class)
.build();
adapter = new FirebaseRecyclerAdapter<Item, ItemViewHolder>(options) {
@Override
public int getItemViewType(int position) {
if(items.get(position).isExpandable()) {
return 1;
}
else{
return 0;
}
}
@Override
protected void onBindViewHolder(@NonNull ItemViewHolder holder, final int position, @NonNull final Item model) {
switch (holder.getItemViewType())
{
case 0:
{
ItemViewHolder viewHolder = holder;
viewHolder.setIsRecyclable(false);
viewHolder.txt_item_text.setText(model.getText());
viewHolder.setImage(getBaseContext(), model.getImage());
viewHolder.setiItemClickListener(new iItemClickListener() {
@Override
public void onClick(View view, int position) {
Toast.makeText(MainActivity.this,""+items.get(position).getText(),Toast.LENGTH_SHORT);
}
});
}
break;
case 1:
{
final ItemViewHolder viewHolder = holder;
viewHolder.setIsRecyclable(false);
viewHolder.txt_item_text.setText(model.getText());
viewHolder.setImage(getBaseContext(), model.getImage());
viewHolder.expandableLinearLayout.setInRecyclerView(true);
viewHolder.expandableLinearLayout.setExpanded(expandState.get(position));
viewHolder.expandableLinearLayout.setListener(new ExpandableLayoutListenerAdapter() {
@Override
public void onPreOpen() {
changeRotate(viewHolder.button,0f,180f).start();
expandState.put(position,true);
}
@Override
public void onPreClose() {
changeRotate(viewHolder.button,180f,0f).start();
expandState.put(position,false);
}
});
viewHolder.button.setRotation(expandState.get(position)?180f:0f);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.expandableLinearLayout.toggle();
}
});
viewHolder.txt_child_item_text.setText(model.getSubText());
viewHolder.txt_child_item_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,""+viewHolder.txt_child_item_text.getText(),Toast.LENGTH_SHORT).show();
}
});
viewHolder.setiItemClickListener(new iItemClickListener() {
@Override
public void onClick(View view, int position) {
Toast.makeText(MainActivity.this,""+model.getText(),Toast.LENGTH_SHORT).show();
}
});
}
break;
default:
break;
}
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
if(viewType==0)
{
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_without_child,viewGroup,false);
return new ItemViewHolder(itemView, viewType == 1);
}
else {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_with_child,viewGroup,false);
return new ItemViewHolder(itemView, viewType == 1);
}
}
};
expandState.clear();
for(int i=0;i<items.size();i++)
expandState.append(i, false);
EqualSpaceItemDecoration itemDecoration = new EqualSpaceItemDecoration(32);
recyclerView.addItemDecoration(itemDecoration);
recyclerView.setAdapter(adapter);
}
private ObjectAnimator changeRotate(RelativeLayout button, float from, float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(button,"rotation",from,to);
animator.setDuration(300);
animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
return animator;
}
private void retrieveData() {
items.clear();
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("Items");
db.keepSynced(true);
db.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot itemSnapShot:dataSnapshot.getChildren()){
Item item = itemSnapShot.getValue(Item.class);
items.add(item);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("Error",""+ databaseError.getMessage());
}
});
}
@Override
protected void onStart() {
super.onStart();
if(adapter != null) {
adapter.startListening();
}
}
@Override
protected void onStop() {
super.onStop();
if(adapter != null)
adapter.stopListening();
}
}
ViewHolder类
public class ItemViewHolder extends RecyclerView.ViewHolder {
public TextView txt_item_text,txt_child_item_text;
public RelativeLayout button;
public ExpandableLinearLayout expandableLinearLayout;
public ImageView topic_icon;
iItemClickListener iItemClickListener;
public void setiItemClickListener(shah.vatsal.kinitrotopics.Interface.iItemClickListener iItemClickListener) {
this.iItemClickListener = iItemClickListener;
}
public ItemViewHolder(@NonNull View itemView, boolean isExpandable) {
super(itemView);
if(isExpandable){
txt_item_text = itemView.findViewById(R.id.txt_item_text);
txt_child_item_text = itemView.findViewById(R.id.txt_child_item_text);
button = itemView.findViewById(R.id.button);
expandableLinearLayout = itemView.findViewById(R.id.expandableLayout);
}
else {
txt_item_text = itemView.findViewById(R.id.txt_item_text);
}
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
iItemClickListener.onClick(view,getAdapterPosition());
}
});
}
public void setImage(Context ctx, String image) {
topic_icon = itemView.findViewById(R.id.topic_icon);
Picasso.with(ctx).load(image).into(topic_icon);
}
}