Androidx RecyclerView 中的 ViewHolder 问题

Problem With ViewHolder In Androidx RecyclerView

androidX 中,当我创建一个 RecyclerView Adapter 时,我无法将 ViewHolder 更改为我的私有 ViewHolder。 请看这段代码:

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
}

当我将 RecyclerView.ViewHolder 更改为我的高级 ViewHolder 时:

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView name;
    ImageView image;

    public MyViewHolder(View itemView) {
        super(itemView);

        name = (TextView) itemView.findViewById(R.id.name);
        image = (ImageView) itemView.findViewById(R.id.image);
    }
}

onBindViewHolder 完全崩溃。我该如何解决这个问题?

编辑放置 ViewHolder 代码:

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class CountryImageAdapter extends RecyclerView.Adapter {

  ArrayList personNames;
  ArrayList personImages;
  Context context;

  public CountryImageAdapter(Context context, ArrayList personNames, ArrayList personImages) {
    this.context = context;
    this.personNames = personNames;
    this.personImages = personImages;
  }

  @NonNull
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    // infalte the item Layout
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_gridlayout, parent, false);
    // set the view's size, margins, paddings and layout parameters
    MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
    return vh;
  }

  @Override
  public void onBindViewHolder(MyViewHolder holder, int position) {


    holder.name.setText(personNames.get(position));
    holder.image.setImageResource(personImages.get(position));
    // implement setOnClickListener event on item view.
    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        // open another activity on item click
        Intent intent = new Intent(context, ListOfClickActivity.class);
        intent.putExtra("TAG", "Country=" + personImages.get(position));
        context.startActivity(intent);
      }
    });

  }

  @Override
  public int getItemCount() {
    return personNames.size();
  }


  public class MyViewHolder extends RecyclerView.ViewHolder {
    // init the item view's
    TextView name;
    ImageView image;

    public MyViewHolder(View itemView) {
      super(itemView);
      // get the reference of item view's
      name = (TextView) itemView.findViewById(R.id.name);
      image = (ImageView) itemView.findViewById(R.id.image);
    }
  }

}

您必须在 CountryImageAdapter 中使用预付款 ViewHolder 才能使用。

public class CountryImageAdapter extends RecyclerView.Adapter<CountryImageAdapter.MyViewHolder> {
    ....

    public CountryImageAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        ....
    }

    @Override
    public void onBindViewHolder(@NonNull CountryImageAdapter.MyViewHolder holder, int position) {

        ....
    }
}