简体   繁体   English

ViewHolder ImageView Android

[英]ViewHolder ImageView Android

I've designed a Chord object that is made of two Strings and an ImageView. 我设计了一个由两个String和一个ImageView组成的Chord对象。

public class Accordo {
private String nome, note;
private ImageView imagine;

// all the get and set methods
}

when I want to refer to it in my custom ViewHolder, more specifically when I @override the onBindViewHolder() method, I write: 当我想在自定义ViewHolder中引用它时,更具体地说,当我@override onBindViewHolder()方法时,我写:

holder.nome.setText(chord.getNome());
holder.note.setText(chord.getNote());

for TextViews I can use the setText() method. 对于TextView,我可以使用setText()方法。 But what is the equivalent for ImageViews ? 但是ImageViews的等效项是什么?

EDIT I could use setImageResource() but it takes an int from a drawable. 编辑我可以使用setImageResource()但是它从drawable中获取一个int值。

Since I need a recyclerView with a different Image for every row, I need something that I can setup later in another class and have something like: 由于我需要每行带有不同图像的recyclerView,因此我需要稍后可以在另一个类中进行设置的东西,例如:

private void prepareMovieData() {
        Movie movie = new Movie("Mad Max: Fury Road", "Action & Adventure", "**LocallyStoredImage**");
        movieList.add(movie);
        ...
    }

Consider this approach: 考虑这种方法:

  • First, inside the class (Activity or Fragment) that shows the RecyclerView items, define a primitive array of drawables like this: 首先,在显示RecyclerView项目的类(活动或片段)内部,定义一个可绘制对象的原始数组,如下所示:

     private int[] menuIcons = { R.drawable.user, R.drawable.customer, R.drawable.list, R.drawable.leads }; 
  • Also create an instance of your Adapter here for later; 还要在此处创建适配器的实例,以备后用。

     private SampleRecyclerAdapter mAdapter; 

Still from your Activity or Fragment, initialize your values 仍然从您的活动或片段中,初始化您的值

private void initRecyclerViewData(){

   List<SampleData> data = new ArrayList<>();

   data.add(new DataObject("Title of row 1", menuIcons[0]));
   data.add(new DataObject("Title of row 2", menuIcons[1]));
   data.add(new DataObject("Title of row 3", menuIcons[2]));
   data.add(new DataObject("Title of row 4", menuIcons[3]));

   //now initialize your adapter - we will create the adapter next
   mAdapter = new SampleRecyclerAdapter(data);
   mRecyclerView.setAdapter(mAdapter);
   mAdapter.notifyDataSetChanged();
}

Now that we have initialized our adapter, let us see how we can show the images inside the adapter - when the view is bound; 现在我们已经初始化了适配器,让我们看看如何在适配器内部显示图像-绑定视图时;

//inside your Adapter class;

....

private List<DataObject> mMenuItemObjects;

public SampleRecyclerAdapter(List<DataObject> menuItemObjects) {
    mMenuItemObjects = menuItemObjects;
}

@Override
public void onBindViewHolder(MainMenuViewHolder holder, int position) {
    holder.itemName.setText(mMenuItemObjects.get(position).getName());

    holder.itemIcon.setImageResource(mMenuItemObjects.get(position).getIcon());
}

That code above means that you have an ArrayList of type DataObject. 上面的代码意味着您具有DataObject类型的ArrayList Remember, when we instantiated our adapter in the activity, we passed an array of data each of which had a title and an icon (drawable). 记住,当我们在活动中实例化适配器时,我们传递了一组数据,每个数据都有一个标题和一个图标(可绘制)。

That is the easiest way to set your image on the recyclerviewer inside onBindViewHolder method. 这是在onBindViewHolder方法内的recyclerviewer上设置图像的最简单方法。

NOTE: You must create a ViewHolder class and DataObject (latter is a plain old java object). 注意:您必须创建一个ViewHolder类和DataObject(后者是一个普通的旧Java对象)。

Good luck and I hope this helps! 祝您好运,希望对您有所帮助!

等效的图像将是setImageResource()。

holder.imagine.setImageResource(chord.getImage());
  1. Make a class which would hold your titles and images 开设一个可以容纳您的标题和图像的课程
  2. Make a ArrayList of that particular class. 创建该特定类的ArrayList。
  3. Add Data to the Arraylist 将数据添加到阵列列表
  4. Call adapter.notifydatasetchanged() after your list.add(...) 在您的list.add(...)之后调用adapter.notifydatasetchanged() . list.add(...)
  5. use something like Picasso in your onBindViewHolder() to load images(guessing that the images come from some kind of database in URI Form) onBindViewHolder()使用类似Picasso的图像来加载图像(猜测图像来自URI形式的某种数据库)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM