问题
I searched on the web and fount that there's a difference between view.invisible and view.gone, the vie.gone must disappear without keeping the space but it doesn't happen to me, how can I fix it? please help, check out the links of the two pictures.
Picture of the result on the Android Emulator: https://i.stack.imgur.com/AC1Xr.png
Database: https://i.stack.imgur.com/Pwhno.png
@Override
protected void onStart() {
super.onStart();
final FirebaseRecyclerAdapter<MyReading, MyReadingViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter <MyReading, MyReadingViewHolder>
(
MyReading.class,
R.layout.list_reading,
MyReadingViewHolder.class,
mQuery )
{
@Override
protected void populateViewHolder(final MyReadingViewHolder viewHolder, MyReading model, int position) {
final String wordNote_key = getRef(position).getKey();
viewHolder.setWord(model.getWord());
viewHolder.setNote(model.getNote());
mDatabase.child(wordNote_key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String currentUid = (String) dataSnapshot.child("uid").getValue();
if(firebaseauth.getCurrentUser().getUid().equals(currentUid)){
viewHolder.nView.setVisibility(View.VISIBLE);
}
else
{
viewHolder.nView.setVisibility(View.GONE);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
viewHolder.nView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent singleIntent = new Intent(WordNote.this, HandleWordNote.class);
singleIntent.putExtra("listWords", wordNote_key);
startActivity(singleIntent);
}
});
}
};
listWN.setAdapter(firebaseRecyclerAdapter);
}
回答1:
EDIT
Ok, so after seeing the updated image, I see the real problem. In your query, add the constraint to only return the data snapshot with the current user's uid. The problem is that even though your viewholder's view is GONE
, the viewholder itself will still hold the position, since there is a model object for that position. Rather then hacking away the FirebaseRecyclerAdapter
, just filter out the data you don't want in the list by using your Query
object that you pass in.
Have you tried notifying the adapter of the change? Just because you modified the child view, doesn't mean the adapter will automatically resize the viewHolder to the proper size.
mDatabase.child(wordNote_key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String currentUid = (String) dataSnapshot.child("uid").getValue();
if(firebaseauth.getCurrentUser().getUid().equals(currentUid)){
viewHolder.nView.setVisibility(View.VISIBLE);
}
else
{
viewHolder.nView.setVisibility(View.GONE);
}
//Notify the adapter the viewholder at this position changed.
notifiyItemChanged(position);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
回答2:
There is a workaround which you can set the height of the viewHolder's view to zero.
val layoutParamter = holder.mView.layoutParams
layoutParamter.height = 0
holder.mView.layoutParams = layoutParamter
来源:https://stackoverflow.com/questions/44705131/view-gone-in-recyclerview-still-keeps-space