如何使用 Adapter 将 ImageView 动态添加到布局中

How to dynamically add ImageView to layout using Adapter

我想通过我的适配器将 ImageViews 动态添加到我的 RelativeLayout。我的代码没有产生任何错误,但是,没有创建 ImageView(使用层次视图确认)。

我用来添加 ImageView 的代码在 OnBindViewHolder() 方法中。

// Create the basic adapter extending from RecyclerView.Adapter
// Note that we specify the custom ViewHolder which gives us access to our views

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

// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access

public static class ViewHolder extends RecyclerView.ViewHolder{
    // Your holder should contain a member variable
    // for any view that will be set as you render a row
    public TextView nameTextView;
    public TextView dateTextView;
    public TextView creatorTextView;
    public ImageButton thumbnail;
    public ImageView image;
    public RelativeLayout relativeLayout;

    // We also create a constructor that accepts the entire item row
    // and does the view lookups to find each subview
    public ViewHolder(View itemView) {
        // Stores the itemView in a public final member variable that can be used
        // to access the context from any ViewHolder instance.
        super(itemView);

        relativeLayout = (RelativeLayout) itemView.findById(R.id.view); 
        nameTextView = (TextView) itemView.findViewById(R.id.name);
        dateTextView = (TextView) itemView.findViewById(R.id.date);
        thumbnail = (ImageButton) itemView.findViewById(R.id.imageButton);
        creatorTextView = (TextView) itemView.findViewById(R.id.creator_name);
    }
}

// Store a member variable for the contacts
private ArrayList<Rally> mRallys;
private Context mContext;

// Pass in the contact array into the constructor
public RallyAdapter(Context context, ArrayList<Rally> rallys) {
    this.mRallys = rallys;
    this.mContext = context;
}

// Usually involves inflating a layout from XML and returning the holder
@Override
public RallyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    // Inflate the custom layout
    View rallyView = inflater.inflate(R.layout.rally, parent, false);

    // Return a new holder instance
    ViewHolder viewHolder = new ViewHolder(rallyView);

    return viewHolder;
}

// Involves populating data into the item through holder
@Override
public void onBindViewHolder(RallyAdapter.ViewHolder viewHolder, final int position) {

    // Get the data model based on position
    final Rally rally = mRallys.get(position);

    TextView dateTV = viewHolder.dateTextView;

    ImageButton imageButton = viewHolder.thumbnail;

    Picasso.with(mContext).load(rally.getThumbnail()).fit().centerCrop().into(imageButton);

    List<String> image_urls = rally.getTransportationImgs();
    List<String> methods = rally.getTransportationStrs();

    Log.d("IMG URLS", image_urls.toString());

    for (int i = 0; i < methods.size(); i++) {

        String method = methods.get(i);

        for (int j = 0; j < image_urls.size(); j++) {

            String img_url = image_urls.get(j);

            if (img_url.toLowerCase().contains(method.toLowerCase()) == true) {

                viewHolder.image = new ImageView(mContext);
                dateTV = viewHolder.dateTextView;
                imageButton = viewHolder.thumbnail;

                RelativeLayout rallyLayout = new RelativeLayout(mContext);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);

                params.addRule(RelativeLayout.RIGHT_OF, imageButton.getId());
                params.addRule(RelativeLayout.BELOW, dateTV.getId());
                params.addRule(RelativeLayout.END_OF, imageButton.getId());
                params.height = 65;
                params.width = 65;

                viewHolder.image.setLayoutParams(params);

                rallyLayout.addView(viewHolder.image);

                viewHolder.relativeLayout.addview(rallyLayout);

                Picasso.with(mContext).load(img_url).fit().centerCrop().into(viewHolder.image);
            }
        }
    }
}

@Override
public int getItemCount() {
    // TODO Auto-generated method stub
    return mRallys.size();
}

我尝试通过 getView() 方法完成此操作,但由于某种原因,似乎没有调用 getView() 函数。

您正在将图像添加到相对布局,但此相对布局未添加到您的视图。

在 onCreate 中,您应该通过设置根元素必须有一个 android:id="@+id/view" 来保留对扩充 R.layout.rally 的视图的引用。 (如果没有你的 xml 文件,我不确定,但你似乎使用了相对布局作为根元素)

然后在你的viewholder中添加:

public RelativeLayout relativeLayout;

relativeLayout = (RelativeLayout) itemView.findById(R.id.view); 

然后在onBindViewHolder中,放在rallyLayout.addView(viewHolder.image);

之后
viewHolder.relativeLayout.addview(rallyLayout)

您可能还需要调整布局参数,但您似乎知道该怎么做。

只是一点点评论,尽量不要混合布局参数,即使它们产生相同的结果RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);