RecyclerView

时间:2018-05-30 13:29:22

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

我创建了一个包含CardView的Firebase RecyclerView。在CardView内部有一个ImageView和一个TextView。如果图像与其描述一起上传(TextView),则ImageView和TextView都可见,否则只有TextView可见。问题是,当RecyclerView包含超过11个项目时,ImageViews会自动消失,只有TextViews可见。不知道为什么会这样。 以下是代码:

public class MainView extends 
    AppCompatActivity {
    private RecyclerView mPlacelist; protected void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState); mPlacelist= (RecyclerView) findViewById(R.id.Place_list); mPlacelist.setHasFixedSize(true);

LinearLayoutManager mLayoutManager=new LinearLayoutManager(MainView.this); mLayoutManager.setReverseLayout(true); mLayoutManager.setStackFromEnd(true); mPlacelist.setLayoutManager(mLayoutManager); } public static class  MainViewHolder extends RecyclerView.ViewHolder {
    View mView;


    public MainViewHolder(View itemView,int viewType) {
        super(itemView);
        mView = itemView;


    }
    public void setDescription(String description) {
        TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
        post_desc.setText(description);
    }

    public void setImage(final Context ctx, final String image) {
        ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
        //If image exist
        if (image != null) {

            Picasso.with(ctx).load(image).fit().centerInside().into(post_image);

        } else if (image == null) {

           //no image then imageview invisible
            Picasso.with(ctx).cancelRequest(post_image);
            post_image.setVisibility(View.INVISIBLE);
            post_image.setVisibility(View.GONE);

        }
    }

   }

final FirebaseRecyclerAdapter<MainBlogView, MainView.MainViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<MainBlogView, MainView.MainViewHolder>(
        MainBlogView.class, R.layout.recyclerplace, MainView.MainViewHolder.class, databaseReference ) {
    @Override
    protected void populateViewHolder(final MainView.MainViewHolder viewHolder, MainBlogView model, int position) {





       viewHolder.setDescription(model.getDescription());
        viewHolder.setImage(getApplicationContext(), model.getImage());
        viewHolder.setTimestamp(model.getTimestamp());

}  @Override
        public int getItemViewType(int position) {

            return super.getItemViewType(position);
        }

    };
    firebaseRecyclerAdapter.notifyDataSetChanged();
    mPlacelist.setAdapter(firebaseRecyclerAdapter);



} }

感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:3)

而不是这段代码:

if (image != null) {

        Picasso.with(ctx).load(image).fit().centerInside().into(post_image);
}

使用此:

if (image != null) {

        post_image.setVisibility(View.VISIBLE);
Picasso.with(ctx).load(image).fit().centerInside().into(post_image);

    }

你得到了这个&#34;错误&#34;因为你的第12行没有图像而且你的代码隐藏了它,而RecyclerView会重复使用行来提高性能。