这不是一个真正的问题,但我一直在寻找的是,让应用程序从firebase数据库服务器获取图像时,让cardview显示我在我的cardview中设置的图像。像大多数应用程序一样,例如Facebook(cardview中的图像显示空白图片,除非它从服务器获取数据)。
现在我的应用程序所做的是,除非应用程序未在线连接或取出数据,否则cardview会缩小到wrap_content大小。我正在添加图片,以便清除问题的后果。
当数据提取完成时,它看起来像这样:
现在我发布了我的xml文件,其中为用户提供了我自己的图像,直到图像从服务器中取出。
1.fragment_blog.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.aadarshkumarsingh.project1.Blog">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0E0E0"
android:id="@+id/blog_list"
android:layout_marginTop="55dp">
</android.support.v7.widget.RecyclerView>
1.(a)在fragment_blog.xml的recyclerview中查看
blogrow.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="45dp"
android:layout_height="43dp"
android:adjustViewBounds="true"
android:id="@+id/post_userpic"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
android:src="@drawable/user_pic"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/post_username"
android:text="username"
android:textColor="#000000"
android:paddingTop="14dp"
android:paddingLeft="10dp"
android:textSize="14dp"
android:textStyle="normal|bold"
android:fontFamily="sans-serif"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="posted this"
android:layout_marginLeft="4dp"
android:textSize="14dp"
android:textStyle="normal"
android:fontFamily="sans-serif"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time"
android:id="@+id/post_time"
android:textColor="#BDBDBD"
android:layout_marginLeft="11dp"
android:layout_marginTop="3dp"
android:textSize="12dp"
android:textStyle="normal"
android:fontFamily="sans-serif"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/post_title"
android:text="Post Title Goes Here"
android:padding="10dp"
android:textSize="16dp"
android:textStyle="normal|bold"
android:fontFamily="sans-serif"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/post_image"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="@mipmap/add_btn" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/post_desc"
android:padding="10dp"
android:textColor="#000000"
android:paddingBottom="15dp"
android:textSize="16dp"
android:text="The Post Description Goes Here"/>
</LinearLayout>
我使用Picasso Square库来获取图像并且工作正常。希望能够解决,因为我一直在寻找解决方案,但实际问题的解决方案不可用。 先感谢您。
显示数据库中数据的Java类:
2.Blog.java
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<NewsBlog, BlogViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<NewsBlog, BlogViewHolder>(
NewsBlog.class,
R.layout.blog_row,
BlogViewHolder.class,
mChildRef) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, NewsBlog model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setImage(getContext(),model.getImage());
viewHolder.setDesc(model.getDesc());
viewHolder.setUsername(model.getUsername());
viewHolder.setPost_time(model.getPost_time());
viewHolder.setUserpic(getContext(),model.getUserpic());
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setImage(Context ctx, String image){
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
public void setDesc(String desc){
TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setUsername(String username){
TextView mUsername = (TextView) mView.findViewById(R.id.post_username);
mUsername.setText(username);
}
public void setPost_time(Long post_time) {
TextView time = (TextView) mView.findViewById(R.id.post_time);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM d, ''yy 'at' HH:mma");
time.setText(simpleDateFormat.format(new Date(post_time)));
}
public void setUserpic(Context ctx, String userpic){
ImageView userImage = (ImageView) mView.findViewById(R.id.post_userpic);
Picasso.with(ctx).load(userpic).into(userImage);
}
}
以上是将图像设置为cardview的持有者。