我在recyclerview中使用网格布局管理器来检索一些图像。特定的代码是:
mgridLayoutManager = new GridLayoutManager(getActivity(),2);
mgridLayoutManager.setReverseLayout(true);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(mgridLayoutManager);
mBlogList.setNestedScrollingEnabled(false);
当我的图像数为偶数(例如16)时,这没问题。但是,当我的图像数为奇数(例如17)时,由于我使用的是反向布局,因此recyclerview开始从buttom检索图像(从旧到新)。最佳。并且由于有2列和17张图像,因此它使右上角的位置空白。看起来真傻。我想可以在recyclerview的结尾处保留空白点,但是在recyclerview的开头处保留空白点似乎并不令人印象深刻。如果图像数量奇数,该如何解决,以便在最底行有一个空白点?
我在这里获取图像的链接,并使用Glide在recyclerview中显示它。
检索部分是:
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<HomeBlog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<HomeBlog, BlogViewHolder>(
HomeBlog.class,
R.layout.profile_row,
BlogViewHolder.class,
mDatabase
)
{
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, HomeBlog model, int position) {
final String post_key = getRef(position).getKey();
viewHolder.setImage(post_key);
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setImage(String post_key) {
final ImageView imagePView = (ImageView) mView.findViewById(R.id.profilePostImage);
DatabaseReference val = FirebaseDatabase.getInstance().getReference().child("BlogPosts").child(post_key ) .child("image");
val.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String ppc = dataSnapshot.getValue(String.class);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.drawable.loading_background);
try {
Glide.with(mView.getContext()).setDefaultRequestOptions(placeholderRequest).load(ppc).into(imagePView);
}catch(Exception e){}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}