RecyclerView中的图像

8 android android-recyclerview

我在Android中使用RecyclerView,我遇到了一些图像问题.

我想显示特定文件夹中的图像列表及其路径名称.

这是我的row.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <TextView
            android:id="@+id/my_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="test" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在我的适配器中,我使用此代码来设置行的图像:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.mTextView.setText(mDataset[position]);
    holder.mImageView.setImageURI(Uri.parse("file://" + mMediaStorageDir.getPath() + "/" + mDataset[position]));
}
Run Code Online (Sandbox Code Playgroud)

图像加载正常,但当我向下滚动整个应用程序变得非常慢.

当我加载一个小的drawable时,这不会发生.是因为图片太大了吗?

Ser*_*kov 16

是.我觉得它太大了.

您需要首先加载您的图像(跟踪免费记忆),然后使用他.

不一定使用毕加索.但你可以使用它.

我建议你看看

例如:

ItemData.java

package com.shustikov.android;

public class ItemData {

    private String title;
    private int imageUrl;

    public ItemData(String title,int imageUrl){

        this.title = title;
        this.imageUrl = imageUrl;
    }

    public String getTitle() {
        return title;
    }

    public int getImageUrl() {
       return imageUrl;
    }
}
Run Code Online (Sandbox Code Playgroud)

RecyclerViewExampleAdapter.java

package com.shustikov.android;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class RecyclerViewExampleAdapter extends RecyclerView.Adapter<RecyclerViewExampleAdapter.ViewHolder> {
    private ItemData[] itemsData;

    public RecyclerViewExampleAdapter(ItemData[] itemsData) {
        this.itemsData = itemsData;
    }

    @Override
    public RecyclerViewExampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.item_layout, null);

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
        viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
     }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtViewTitle;
        public ImageView imgViewIcon;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
            imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
        }
    }


    @Override
    public int getItemCount() {
        return itemsData.length;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但我不知道这是如何使大图像加载更快. (4认同)