我是否需要将一个项目从 onBindViewHolder() 共享到 ViewHolder?
Did i need to share an item from onBindViewHolder() to ViewHolder?
我是 android developpement 的大三学生,我正在寻找我的 Viewholder / onBindViewHolder 的最佳实现。
这是 android 工作室在构建片段(列表)时给我们的内容:
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
//some code
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).content);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.item_number);
mContentView = (TextView) view.findViewById(R.id.content);
}
//some code
}
}
为什么将项目发送到 ViewHolder ?为什么他不在 onBindViewHolder 中用于更新 TextViews ?
像这样:
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
//some code
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(holder.mItem.id);
holder.mContentView.setText(holder.mItem.content);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.item_number);
mContentView = (TextView) view.findViewById(R.id.content);
}
//some code
}
}
最后是我目前正在做的事情。
不将项目发送到 ViewHolder :
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
//some code
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
DummyItem mItem = mValues.get(position);
holder.mIdView.setText(mItem.id);
holder.mContentView.setText(mItem.content);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.item_number);
mContentView = (TextView) view.findViewById(R.id.content);
}
//some code
}
}
最好的方法是什么?
我不太清楚你在说什么,但据我了解,你的视图持有者可以包含所有 UI 以及方法和逻辑,但我更喜欢第三种方法比其他的更清晰 2.
话虽这么说,请看 AdaMc331 解释了这些方法的工作原理以及您应该如何使用它们。
我是 android developpement 的大三学生,我正在寻找我的 Viewholder / onBindViewHolder 的最佳实现。
这是 android 工作室在构建片段(列表)时给我们的内容:
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
//some code
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).content);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.item_number);
mContentView = (TextView) view.findViewById(R.id.content);
}
//some code
}
}
为什么将项目发送到 ViewHolder ?为什么他不在 onBindViewHolder 中用于更新 TextViews ? 像这样:
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
//some code
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(holder.mItem.id);
holder.mContentView.setText(holder.mItem.content);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.item_number);
mContentView = (TextView) view.findViewById(R.id.content);
}
//some code
}
}
最后是我目前正在做的事情。 不将项目发送到 ViewHolder :
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
//some code
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
DummyItem mItem = mValues.get(position);
holder.mIdView.setText(mItem.id);
holder.mContentView.setText(mItem.content);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.item_number);
mContentView = (TextView) view.findViewById(R.id.content);
}
//some code
}
}
最好的方法是什么?
我不太清楚你在说什么,但据我了解,你的视图持有者可以包含所有 UI 以及方法和逻辑,但我更喜欢第三种方法比其他的更清晰 2.
话虽这么说,请看