在ViewAdapter上加载图像资源

时间:2015-07-12 19:17:19

标签: java android android-imageview android-adapterview

我有一个ReciclerView,它使用名为" SimpleItemRecyclerViewAdapter"的适配器。 ReciclerView中的每个项目都在项目的#draw;"夹。我能够在TextView上设置测试,但我不确定如何为图像执行此操作。这是我得到的输出......图像应该出现在黑色方块上,是一个ImageView小部件。

enter image description here

以下是我的适配器的代码:

package uk.co.example.calculator.adapters;

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;

import java.util.List;

import uk.co.example.calculator.R;
import uk.co.example.calculator.ui.activities.Currency;

public class SimpleItemRecyclerViewAdapter extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.RecyclerViewSimpleTextViewHolder> {

    // The items to display in your RecyclerView
    private List<Currency> items;

    // Provide a suitable constructor (depends on the kind of dataset)
    public SimpleItemRecyclerViewAdapter(List<Currency> items) {
        this.items = items;
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return this.items.size();
    }

    // Replace the contents of a view (invoked by the layout manager)
    // Involves populating data into the item through holder
    @Override
    public void onBindViewHolder(RecyclerViewSimpleTextViewHolder holder, int position) {
        Currency currency = items.get(position);
        holder.getName().setText(currency.name);
        holder.getSymbol().setText(currency.symbol);
        holder.getAsset().?????????; // WHAT SHOULD I PUT HERE TO LOAD THE IMAGE FROM THE RAW FOLDER
    }

    // Create new items (invoked by the layout manager)
    // Usually involves inflating a layout from XML and returning the holder
    @Override
    public RecyclerViewSimpleTextViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).
                inflate(R.layout.currency_list_card_layout, viewGroup, false);
        return new RecyclerViewSimpleTextViewHolder(itemView);
    }


    public class RecyclerViewSimpleTextViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView symbol;
        private ImageView asset;

        public RecyclerViewSimpleTextViewHolder(View v) {
            super(v);
            name = (TextView) v.findViewById(R.id.currency_name);
            symbol = (TextView) v.findViewById(R.id.currency_symbol);
            asset = (ImageView) v.findViewById(R.id.currency_asset);
        }

        public TextView getName() {
            return name;
        }

        public TextView getSymbol() {
            return symbol;
        }

        public ImageView getAsset() {
            return asset;
        }

    }
}

非常感谢有关如何显示可绘制文件夹中包含的图像的任何详细帮助。

2 个答案:

答案 0 :(得分:2)

尝试使用库进行图像加载。我的建议是毕加索(https://github.com/square/picasso

有了它,你可以通过简单地调用:

轻松地从drawable加载图像
Picasso.with(context).load(R.drawable.drawableName).into(holder.getAsset());

答案 1 :(得分:2)

您可以在onBindViewHolder

中访问您的图片视图

例如,放入onBindViewHolder:

Resources resources = getResources();
holder.getAsset().setImageDrawable(resources.getDrawable(R.drawable.myimage));