如何使用Kotlin在RecyclerView.ViewHolder中绑定视图 [英] How to bind view in RecyclerView.ViewHolder with kotlin
问题描述
让我感到困惑的是如何绑定Recycleler.ViewHolder
中的视图.
这是我的简单适配器,如何在不使用ButterKnife
的情况下使用kotlin-android-extensions
将其转换为kotlin?
What makes me puzzled is how to bind view in Recycleler.ViewHolder
.
This is my simple adapter and how to Convert it to kotlin use kotlin-android-extensions
without ButterKnife
?
public class RoomAdapter extends RecyclerView.Adapter<ViewHolder> {
private OnItemClickListener mListener;
private List<LocationBean> mRooms;
static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tv_title)
TextView tvTitle;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
public void setData(List<LocationBean> rooms) {
mRooms = rooms;
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_first_select, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.tvTitle.setText(mRooms.get(position).getLocation());
holder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClickListener(v, holder.getLayoutPosition());
}
});
}
@Override
public int getItemCount() {
return mRooms == null ? 0 : mRooms.size();
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public interface OnItemClickListener {
void onItemClickListener(View v, int pos);
}
}
推荐答案
发布的解决方案有效,但我愿意喜欢添加一些东西. Viewholder模式的目的是对每个视图只进行一次昂贵的findViewById
调用,然后将这些引用保存在ViewHolder
中,并在需要绑定视图时从那里访问视图.
The posted solution works, but I'd like to add something to it. The purpose of the viewholder pattern is to only make the expensive findViewById
calls once for every view, and then hold those references inside the ViewHolder
, and access the views from there whenever you need to bind one.
但是,每次绑定Viewholder时,在onBindViewHolder
方法中调用holder.itemView.tv_title.text
都会导致findViewById
调用在itemView
中查找ID为tv_title
的View
.这基本上消除了性能提升和视图持有者的缓存想法.
However, calling holder.itemView.tv_title.text
in the onBindViewHolder
method will cause a findViewById
call to find the View
that has the id tv_title
within the itemView
every time the viewholder is bound. This basically eliminates the performance gains and caching idea that viewholders are for.
您可以通过将属性添加到ViewHolder
中,并使用通过扩展程序进行的调用对其进行初始化,从而同时使用viewholder模式和Kotlin Android扩展程序,如下所示:
You can make use of the viewholder pattern and Kotlin Android Extensions at the same time by adding a property to your ViewHolder
, and initializing it with a call made with Extensions, like so:
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val title = itemView.tv_title
}
然后您可以通过此属性访问View
:
Then you can access the View
through this property:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.title.text = mRooms!!.get(position).getLocation()
holder.itemView.setOnClickListener { v ->
mListener?.onItemClickListener(v, holder.layoutPosition)
}
}
最后,我建议摆脱!!
运算符,而执行空检查:
Lastly, I'd suggest getting rid of the !!
operator, and performing a null check instead:
mRooms?.let { rooms ->
holder.title.text = rooms[position].getLocation()
}
这篇关于如何使用Kotlin在RecyclerView.ViewHolder中绑定视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!