我有一个使用Firebase适配器的非常简单的回收视图。 除了一件奇怪的事情,Everythig正常工作: 逻辑是-当用户上传没有图片的帖子时-相关的Firebase节点相应地更新为“无”,因此-逻辑是-在这种情况下,相关帖子将仅获得100dp的宽度和高度。
当我打开我的应用程序时-一切正常,直到我到达recycleview的底部,然后当我向上时-图片的帖子也变小到100dp ...为什么? 相关代码如下:
public class MainFeedActivity extends AppCompatActivity {
private static final String TAG = "MainFeedActivity";
private RecyclerView postList;
private FloatingActionButton AddNewPostButton, AddNewTextPostButton;
private DatabaseReference UsersRef, PostsRef;
private FirebaseAuth mAuth;
String currentUserID, post_user_uid_for_comment;
private String post_user_uid;
private long PostCount;
private Context mContext;
private int Post_Key_from_anonymus, PostIntKeyFromComment;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_feed);
Log.d(TAG, "onCreate: started.");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mContext = MainFeedActivity.this;
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("users");
PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
AddNewPostButton = (FloatingActionButton) findViewById(R.id.add_new_post_button);
AddNewTextPostButton = (FloatingActionButton) findViewById(R.id.add_new_textpost_button);
//The next code is working on displaying all users posts
postList = (RecyclerView) findViewById(R.id.all_users_post_list);
postList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainFeedActivity.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
postList.setLayoutManager(linearLayoutManager);
AddNewPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToPostActivity();
}
});
AddNewTextPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendUserToTextPostActivity();
}
});
DisplayAllUsersPosts();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
private void DisplayAllUsersPosts() {
Query SortPostsInDecendingOrder = PostsRef.orderByChild("userRank");
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Posts, PostsViewHolder>
(
Posts.class,
R.layout.all_post_layout,
PostsViewHolder.class,
SortPostsInDecendingOrder
) {
@Override
protected void populateViewHolder(final PostsViewHolder viewHolder, Posts model, int position) {
final String PostKey = getRef(position).getKey();
final int PostIntKey = position; //var for store post position to return back the user here. Should be declare only here!!!!!!
viewHolder.setFullname(model.getFullname());
viewHolder.setTime(model.getTime());
viewHolder.setDate(model.getDate());
viewHolder.setDescription(model.getDescription());
viewHolder.setPostimage(model.getPostimage());
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
// String fullname=dataSnapshot.child(PostKey).child("fullname").getValue().toString();
// Toast.makeText(MainFeedActivity.this, fullname+"'s post", Toast.LENGTH_SHORT).show();
String degel = "none"; //"https://firebasestorage.googleapis.com/v0/b/bluesky-ec76e.appspot.com/o/country%20icons%2Floading.png?alt=media&token=c69f2f90-0c36-44c7-a8b8-a3e4fce336df";
String deleted = "This message was deleted by user";
if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted)) {
// Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.parseColor("#3700B3"));
viewHolder.PostDesc.setTextSize(35);
viewHolder.PostDesc.setMaxLines(5);
} else if (dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
//Toast.makeText(MainFeedActivity.this, "+"+PostKey+"+", Toast.LENGTH_SHORT).show();
int newHeight = 100; // New height in pixels
int newWidth = 100; // New width in pixels
// Apply the new height for ImageView programmatically
viewHolder.PostImage.getLayoutParams().height = newHeight;
// Apply the new width for ImageView programmatically
viewHolder.PostImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
viewHolder.PostImage.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
viewHolder.PostDesc.setTextColor(Color.GRAY);
viewHolder.PostDesc.setTextSize(10);
// viewHolder.PostDesc.setMaxLines(5);
} else if (!dataSnapshot.child(PostKey).child("postimage").getValue().toString().equals(degel)
&& !dataSnapshot.child(PostKey).child("description").getValue().toString().equals(deleted))
{
if (viewHolder.PostDesc.getLineCount() == 1) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(35);
} else if (viewHolder.PostDesc.getLineCount() == 2) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(30);
} else if (viewHolder.PostDesc.getLineCount() == 3) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(25);
} else if (viewHolder.PostDesc.getLineCount() == 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(20);
} else if (viewHolder.PostDesc.getLineCount() > 4) {
viewHolder.PostDesc.setTextColor(Color.BLUE);
viewHolder.PostDesc.setTextSize(15);
}
}
// post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
Toast.makeText(MainFeedActivity.this, "Not Exist", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
PostsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(PostKey)) {
post_user_uid_for_comment = dataSnapshot.child(PostKey).child("uid").getValue().toString();
} else {
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
viewHolder.CommentPostButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent commentsIntent = new Intent(MainFeedActivity.this, CommentsActivity.class);
commentsIntent.putExtra("Post_Key", PostKey);
startActivity(commentsIntent);
}
});
}
};
postList.setAdapter(firebaseRecyclerAdapter);
}
public static class PostsViewHolder extends RecyclerView.ViewHolder {
View mView;
ImageView CommentPostButton;
TextView PostDesc;
String currentUserID;
SquareImageView PostImage;
public PostsViewHolder(View itemView) {
super(itemView);
mView = itemView;
CommentPostButton = (ImageView) mView.findViewById(R.id.speech_bubble);
PostImage = (SquareImageView) mView.findViewById(R.id.post_image);
PostDesc = (TextView) mView.findViewById(R.id.post_description);
try {
currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
} catch (Exception e) {
/* This is a generic Exception handler which means it can handle
* all the exceptions. This will execute if the exception is not
* handled by previous catch blocks.
*/
Toast.makeText(mView.getContext(), "User is not exist", Toast.LENGTH_SHORT).show();
}
}
}
public void setFullname(String fullname) {
TextView username = (TextView) mView.findViewById(R.id.post_user_name);
username.setText(fullname);
}
public void setTime(String time) {
TextView PostTime = (TextView) mView.findViewById(R.id.post_time);
PostTime.setText(" " + time);
}
public void setDate(String date) {
TextView PostDate = (TextView) mView.findViewById(R.id.post_date);
PostDate.setText(" " + date);
}
public void setDescription(String description) {
TextView PostDescription = (TextView) mView.findViewById(R.id.post_description);
PostDescription.setText(description);
}
public void setPostimage(String postimage) {
ImageView PostImage = (ImageView) mView.findViewById(R.id.post_image);
Picasso.get().load(postimage).fit().into(PostImage);
}
}
}