如何在回收站视图中获取Drawable图像名称

时间:2017-11-05 18:16:38

标签: java android android-recyclerview

我在回收站视图中有一个图像列表。我想显示背景中使用的图像名称。但是当我点击任何图像时,它会在log中返回" android.graphics.drawable.BitmapDrawable@1cca205" 。如何显示图像名称?我想在switch case中使用这个图像名称并执行一些任务。

主要Activity.java

public static String[] AddCustomitems = {"Blink single message", "Blink double message", "Message", "Scroll", "Split", "Temp"};
    public static final Integer[] customviewsDrawable = {R.drawable.custom_blink, R.drawable.custom_blink_double, R.drawable.custom_message, R.drawable.custom_scroll, R.drawable.custom_split_double, R.drawable.temp};
    public static final Integer defaultAdd =R.drawable.custom_add;
    ArrayList<ItemModel> arrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arrayList.add(new ItemModel(defaultAdd));
        for(int i =0;i<customviewsDrawable.length;i++){
            ItemModel itemModel = new ItemModel();
            arrayList.add(new ItemModel(customviewsDrawable[i]));
        }
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        //Set RecyclerView type according to intent value
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
        adapter = new MyAdapter(MainActivity.this, arrayList);
        recyclerView.setAdapter(adapter);// set adapter on recyclerview
        adapter.notifyDataSetChanged();// Notify the adapter
}

MyAdapter.java

 @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
        final ItemModel model = arrayList.get(holder.getAdapterPosition());

        final RecyclerViewHolder mainHolder = (RecyclerViewHolder) holder;// holder

        Bitmap image = BitmapFactory.decodeResource(context.getResources(), model.getImage());// This will convert drawbale image into
        // bitmap
        mainHolder.imageview.setImageBitmap(Bitmap.createScaledBitmap(image, 330, 500, false));
        mainHolder.imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("Image name:", String.valueOf(mainHolder.imageview.getDrawable())); 
            }
        });
        setAnimation(holder.cardView, position);
    }

2 个答案:

答案 0 :(得分:0)

我没有机会尝试,但这应该给你一个名字。

mainHolder.imageview.getResources().getResourceName(mainHolder.imageview.getId()))

这只显示imageview名称。

所以我检查一下,这是唯一的方法:

mainHolder.imageview.setImageBitmap(Bitmap.createScaledBitmap(image, 330, 500, false));
mainHolder.imageview.setTag("yourImageName");
        mainHolder.imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("Image name:", String.valueOf(mainHolder.imageview.getTag("yourImageName");)); 
            }
        });

答案 1 :(得分:-1)

将图像放在可绘制文件夹中,并通过R.drawable.image_name访问它。