应该持有人参考适配器?

我正在阅读来自Google的示例代码,代码是这样的:

public class AttractionListFragment extends Fragment { ... private class AttractionAdapter extends RecyclerView.Adapter<ViewHolder> implements ItemClickListener { public List<Attraction> mAttractionList; private Context mContext; public AttractionAdapter(Context context, List<Attraction> attractions) { super(); mContext = context; mAttractionList = attractions; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { Log.d("TEST", "onCreateViewHolder"); LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.list_row, parent, false); return new ViewHolder(view, this); } @Override public void onBindViewHolder(ViewHolder holder, int position) { Attraction attraction = mAttractionList.get(position); holder.mTitleTextView.setText(attraction.name); holder.mDescriptionTextView.setText(attraction.description); Glide.with(mContext) .load(attraction.imageUrl) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .placeholder(R.drawable.empty_photo) .override(mImageSize, mImageSize) .into(holder.mImageView); String distance = Utils.formatDistanceBetween(mLatestLocation, attraction.location); if (TextUtils.isEmpty(distance)) { holder.mOverlayTextView.setVisibility(View.GONE); } else { holder.mOverlayTextView.setVisibility(View.VISIBLE); holder.mOverlayTextView.setText(distance); } } @Override public long getItemId(int position) { return position; } @Override public int getItemCount() { return mAttractionList == null ? 0 : mAttractionList.size(); } @Override public void onItemClick(View view, int position) { View heroView = view.findViewById(android.R.id.icon); DetailActivity.launch( getActivity(), mAdapter.mAttractionList.get(position).name, heroView); } } private static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView mTitleTextView; TextView mDescriptionTextView; TextView mOverlayTextView; ImageView mImageView; ItemClickListener mItemClickListener; public ViewHolder(View view, ItemClickListener itemClickListener) { super(view); mTitleTextView = (TextView) view.findViewById(android.R.id.text1); mDescriptionTextView = (TextView) view.findViewById(android.R.id.text2); mOverlayTextView = (TextView) view.findViewById(R.id.overlaytext); mImageView = (ImageView) view.findViewById(android.R.id.icon); mItemClickListener = itemClickListener; view.setOnClickListener(this); } @Override public void onClick(View v) { mItemClickListener.onItemClick(v, getAdapterPosition()); } } interface ItemClickListener { void onItemClick(View view, int position); } 

}

你可以看到每个ViewHolder持有对适配器的引用(ItemClickListener),因为适配器已经保持对ViewHolder的引用,有谁知道这种实现是否会导致内存泄漏? 如果我的理解错误,请纠正我。

不要以为会这样。 ViewHolder不能在没有适配器的情况下存在,并且会在适配器之前很好地被垃圾收集,因为RecyclerView被滚动时,ViewHolders会一直被适配器回收。 正在使用的接口模式只是通知适配器某个ViewHolder已被点击的常用方式。 我想你可能会泄漏内存,如果你通过在适配器内部的一个成员variables中持有对特定ViewHolder的引用来做相反的事情,那么可能会阻止ViewHolder被GC化。 不过,上面的代码看起来很标准。