[我试图找到如何将适配器的getAdapterPosion()结果返回给调用方Activity,但我无法弄清楚。
Adapter类中的回调代码
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.RowViewHolder> {
// Adapter class code
...
class RowViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//more code
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
ListActivity.position = getAdapterPosition();
final Country currentCountry = countriesList.get(getAdapterPosition());
}
}
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
...
}
整个适配器类(如果要运行它)
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.RowViewHolder> {
private ItemClickListener mClickListener;
FragmentManager fm;
Activity listActivity;
private Context context;
private List<Country> countriesList;
public ListAdapter(Context context, Activity listActivity) {
this.fm = fm;
this.listActivity = listActivity;
this.context = context;
countriesList = new ArrayList<>();
}
@NotNull
@Override
public RowViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context)
.inflate(R.layout.row_list, parent,false);
// context = parent.getContext();
return new RowViewHolder(itemView);
}
class RowViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView viewName;
ImageView viewFlag;
LinearLayout countryEntry;
RowViewHolder(View view) {
super(view);
viewName = view.findViewById(R.id.name);
viewFlag = view.findViewById(R.id.flag);
countryEntry = view.findViewById(R.id.rowId);
view.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
ListActivity.position = getAdapterPosition();
final Country currentCountry = countriesList.get(getAdapterPosition());
//todo: can i return currentCountry to the listActivity in order to pass it?
}
}
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
@Override
public void onBindViewHolder(@NotNull RowViewHolder holder, int position) {
final Country currentCountry = countriesList.get(position);
int id = currentCountry.getId();
holder.viewName.setText(countriesList.get(position).getName());
Uri imageUrl = Uri.parse(String.valueOf(countriesList.get(position).getFlag()));
SvgLoader.pluck()
.with(listActivity)//Replace new MainActivity with activity
.setPlaceHolder(R.mipmap.ic_launcher, R.mipmap.ic_launcher)
.load(imageUrl, holder.viewFlag);
// holder.viewFlag.setImageURI(Uri.parse(String.valueOf(countriesList.get(position).getFlag())));
// holder.countryEntry.setOnClickListener(new View.OnClickListener() {
// });
}
@Override
public int getItemCount() {
return countriesList.size();
}
public void attachCountriesList(List<Country> countriesList) {
// getting list from Fragment.
this.countriesList = countriesList;
}
}
来自活动的呼叫是adapter.setClickListener(this);
。我一直在尝试将此调用的结果分配给一个int变量,但它不起作用。
谢谢!
活动不调用适配器。他们实例化它们,然后在RecyclerView上设置适配器。没有什么可退回的。您需要的是对适配器的引用。您可以通过多种方式从活动中获取信息。
设置适配器之前:
ListAdapter adapter = new ListAdapter(parameter, parameter);
recycler.setAdapter(adapter);
获得职位:
int position = adapter.getAdapterPosition();
设置适配器后:
ListAdapter adapter = (ListAdapter)recycler.getAdapter();
使您的代码复杂的事物:
在ListAdapter中,您向构造函数传递Context和ListActivity。 ListActivity已经是一个Context子类,无论何时需要Context,都可以使用ListActivity。
您正在使onClick过程复杂化。您将适配器项设置为clickListeners,然后将活动作为侦听器。当您单击项目时,您将经过2个侦听器(项目本身和listActivity)。您的活动无需成为侦听器(将其称为clickListener不会使其成为一个侦听器,因此它只是一个侦听器)。由于您已经在适配器中引用了您的“活动”,因此您已经可以与其进行通信。因此,不要在onClick中包含以下代码:
public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); ListActivity.position = getAdapterPosition(); final Country currentCountry = countriesList.get(getAdapterPosition()); }
您可能有这个:
public void onClick(View view) {
listActivity.showItemDetails( view, getAdapterPosition) ; //you name your method what u want of course, just make sure is public.
listActivity.setCountries(currentCountries); //since you have the activity's reference u can pass anything u like including currentCountry
}
然后,您可以摆脱创建的接口以及设置器和获取器。以及其他使该工作奏效的因素。您不需要它!