recycleview onBindView & onViewAttachedToWindow 重复混淆
recycleview onBindView & onViewAttachedToWindow duplicate confused
以下是recycleview.adapter的代码:
private class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder> {
private MyRecycleViewAdapter.ViewHolder viewHolder=null;
@Override
public MyRecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getActivity()).inflate( R.layout.device_tmperature,parent,false);
viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.e(TAG, "onBindView position " + position+" suppose to be seen "+holder.getLayoutPosition()+" ℃" );
viewHolder.degree.setText(holder.getLayoutPosition()+" ℃");
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return 10;
}
@Override
public void onViewAttachedToWindow(ViewHolder holder) {
Log.e(TAG, "onViewAttachedToWindow position " + holder.getLayoutPosition()+" suppose to be seen "+holder.getLayoutPosition()+" ℃" );
viewHolder.degree.setText(holder.getLayoutPosition()+" ℃");
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView degree = null;
CardView cv_device=null;
public ViewHolder(View itemView) {
super(itemView);
degree= (TextView) itemView.findViewById(R.id.tv_degree);
cv_device = (CardView) itemView.findViewById(R.id.cv_device);
cv_device.setCardElevation(5);
cv_device.setRadius(5);
}
}
}
一开始是正确的,但是当我向下滑动时,在位置4显示“6℃”,而应该是“5℃”。而下面的也一头雾水
好的,我工作 out.It 应该使用 holder 而不是 viewholder。
以下是recycleview.adapter的代码:
private class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder> {
private MyRecycleViewAdapter.ViewHolder viewHolder=null;
@Override
public MyRecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getActivity()).inflate( R.layout.device_tmperature,parent,false);
viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.e(TAG, "onBindView position " + position+" suppose to be seen "+holder.getLayoutPosition()+" ℃" );
viewHolder.degree.setText(holder.getLayoutPosition()+" ℃");
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return 10;
}
@Override
public void onViewAttachedToWindow(ViewHolder holder) {
Log.e(TAG, "onViewAttachedToWindow position " + holder.getLayoutPosition()+" suppose to be seen "+holder.getLayoutPosition()+" ℃" );
viewHolder.degree.setText(holder.getLayoutPosition()+" ℃");
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView degree = null;
CardView cv_device=null;
public ViewHolder(View itemView) {
super(itemView);
degree= (TextView) itemView.findViewById(R.id.tv_degree);
cv_device = (CardView) itemView.findViewById(R.id.cv_device);
cv_device.setCardElevation(5);
cv_device.setRadius(5);
}
}
}
一开始是正确的,但是当我向下滑动时,在位置4显示“6℃”,而应该是“5℃”。而下面的也一头雾水
好的,我工作 out.It 应该使用 holder 而不是 viewholder。