Android 在回收器视图中显示图像,类似于facebook时间线

Android 在回收器视图中显示图像,类似于facebook时间线,android,facebook,image,android-recyclerview,Android,Facebook,Image,Android Recyclerview,我正在创建一个android应用程序,我被困在一个点上。我想在facebook时间线上显示帖子。帖子可能有图像,也可能没有。我已经为其他帖子元素创建了一个卡片视图,我可以在帖子中显示它们。我有从服务器发送的图片链接,现在我想下载图片,并显示在特定的文章,其中有图像。我该怎么做?若用户有10篇文章,那个么可能会有一两篇图片。如何检查哪个帖子有图像,然后在下载后在该帖子中显示图像。谁能帮我一下吗 如果你需要更多信息,请告诉我。我的卡片视图如下所示: <?xml version="1.0" en

我正在创建一个android应用程序,我被困在一个点上。我想在facebook时间线上显示帖子。帖子可能有图像,也可能没有。我已经为其他帖子元素创建了一个卡片视图,我可以在帖子中显示它们。我有从服务器发送的图片链接,现在我想下载图片,并显示在特定的文章,其中有图像。我该怎么做?若用户有10篇文章,那个么可能会有一两篇图片。如何检查哪个帖子有图像,然后在下载后在该帖子中显示图像。谁能帮我一下吗

如果你需要更多信息,请告诉我。我的卡片视图如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageViewUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/human_image"/>

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imageViewUser"
        android:layout_toEndOf="@+id/imageViewUser"
        android:textSize="18sp"
        android:text="titleText"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"/>

        <TextView
            android:id="@+id/textViewNoOfDays"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewTitle"
            android:textSize="14sp"
            android:layout_marginLeft="10dp"
            android:text="No of Days"
            android:layout_toRightOf="@+id/imageViewUser"
            android:layout_alignBottom="@+id/imageViewUser"/>

        <TextView
            android:id="@+id/textViewPostDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageViewUser"
            android:layout_marginTop="20dp"
            android:text="ABCDE"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp"/>

        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#c0c0c0"
            android:layout_below="@+id/postImage"
            android:layout_marginTop="10dp"/>

        <ImageButton
            android:id="@+id/imageButtonPin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@mipmap/ic_pin_grey600_18dp"/>

        <ImageView
            android:id="@+id/postImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewPostDescription"
            />








    </RelativeLayout>






</android.support.v7.widget.CardView>




</RelativeLayout>

我想这就是你要找的,
cardwiew
。它测试从服务器获取的图像是否为
null
。这也是对图像大小的响应

这就是我如何让它工作的

适配器类:

public class StoriesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Success> successList;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    private Context mContext;

    public StoriesAdapter(Context context, List<Success> successList) {
        this.successList = successList;
        this.mContext = context;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.success_list, parent, false);
        StoryItemViewHolder vh = new StoryItemViewHolder(v);

        return vh;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        final Success success = successList.get(position);
        StoryItemViewHolder holder = (StoryItemViewHolder) viewHolder;
        // Feed image
        if (!success.getThumbnailUrl().equals("null")) {//check if null
            holder.thumbNail.setImageUrl(success.getThumbnailUrl(), imageLoader);
            holder.thumbNail.setVisibility(View.VISIBLE);
            holder.thumbNail.setResponseObserver(new FeedImageView.ResponseObserver() {
                        @Override
                        public void onError() {
                        }

                        @Override
                        public void onSuccess() {
                        }
                    });
        } else {
            holder.thumbNail.setVisibility(View.GONE);
        }

    }
    @Override
    public int getItemCount() {
        return (null != successList ? successList.size() : 0);
    }

}

您必须从服务器获取一些json。检查json中的图像键。如果它存在并且它的值不为空,请使用一些图像加载库(如来自bumptech的Glide)在图像视图中加载图像,否则将图像视图可见性设置为在viewholder中消失。这就是你要找的吗?是的,很像这个。你能给我一个链接让我使用它吗。我想从json提供的链接下载图像,这需要花费很多时间。。这是我提到的库:。向下滚动,你会发现如何使用这个。这将为您下载图像,并在图像视图中进行设置。如果仍然需要花费大量时间来显示图像,可能是因为您的图像非常沉重,因为您有一个良好的连接。Glide还有一个选项,可以在实际图像加载之前放置占位符图像。当前,我正在从响应中提取图像链接,并将它们存储在数组列表中,我正在下载所有图像,我不知道如何检查图像所属的帖子。如果您有任何想法,请告诉我。
public class StoryItemViewHolder extends RecyclerView.ViewHolder {
    public FeedImageView thumbNail;

    public StoryItemViewHolder(View view) {
        super(view);
        this.thumbNail= (FeedImageView) view.findViewById(R.id.newsImage);
    }

}