滚动时的RecyclerView问题

时间:2018-11-27 13:23:15

标签: android scroll android-recyclerview adapter android-viewholder

我有一个可扩展的RecyclerView,当将数据加载到Recyclerview中时,一切似乎都很好,但是当我滚动到Bodem时,上面各项的数据都被静音了。在普通回收者视图中,我可以通过将所有内容绑定到OnBindViewHolder中来解决此问题,但是现在更加困难了。 这是我的可扩展适配器代码:

public class CalendarAdapter extends ExpandableRecyclerViewAdapter<CalendarHeaderViewHolder, CalendarItemViewHolder> {

private Context ctx;

public CalendarAdapter(List<? extends ExpandableGroup> group, Context ctx){
    super(group);
    this.ctx = ctx;
}
@Override
public CalendarHeaderViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.group_view_holder, parent, false);
    return new CalendarHeaderViewHolder(view);
}

@Override
public CalendarItemViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.calendar_item, parent, false);
    return new CalendarItemViewHolder(view,ctx);
}

@Override
public void onBindChildViewHolder(CalendarItemViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
    final CalenderItem calenderItem = ((CalenderHeader) group).getItems().get(childIndex);
    holder.onBind(calenderItem, group);
}

@Override
public void onBindGroupViewHolder(CalendarHeaderViewHolder holder, int flatPosition, ExpandableGroup group) {
    holder.setGroupName(group);
}

}

我将我的Holder绑定到CalendarItemViewHolder

public class CalendarItemViewHolder extends ChildViewHolder {
private TextView tvDescription;
private TextView tvSubDescription;
private TextView tvDate;
private RelativeLayout ivIcon;
private CalenderItem item;
private LinearLayout llItemCal;
private onInteractionListener listener;
private Context ctx;

public CalendarItemViewHolder(View itemView, Context ctx){
    super(itemView);

    tvDescription = (TextView) itemView.findViewById(R.id.tvDescription);
    tvSubDescription = (TextView)itemView.findViewById(R.id.tvSubDescription);
    ivIcon = (RelativeLayout) itemView.findViewById(R.id.ivIcon);
    tvDate = (TextView) itemView.findViewById(R.id.tvDate);
    llItemCal = (LinearLayout) itemView.findViewById(R.id.llItemCal);
    this.ctx = ctx;
    listener = (onInteractionListener) ctx;

}

public void onBind(CalenderItem calenderItem, ExpandableGroup group){
    this.item = calenderItem;

    llItemCal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//MOVE TO DETAIL SCREEN
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        tvDescription.setText(Html.fromHtml(calenderItem.getActivitie().getTitle(), Html.FROM_HTML_MODE_COMPACT));
    } else {
        tvDescription.setText(Html.fromHtml(calenderItem.getActivitie().getTitle()));
    }
//HERE IS A PROBLEM WHEN SCROLLING
    if(calenderItem.getCalType()== null || calenderItem.getCalType().equals("")){
        tvSubDescription.setVisibility(View.GONE);
    }else{
        tvSubDescription.setText(calenderItem.getCalType());
    }
  //HERE IS ALSO A PROBLEM WHEN SCROLLING 
    if (calenderItem.getDate() != null){
        tvDate.setText(calenderItem.getDate());
        tvDate.setVisibility(View.VISIBLE);
    }

 //HERE EVERYTHING SEEMS OKE
    Drawable background = ctx.getResources().getDrawable(R.drawable.circle);
    switch (item.getType()){
        case PROTHESE:
            background.setColorFilter(ContextCompat.getColor(ctx, R.color.blue_grey_200), PorterDuff.Mode.SRC_IN);
            ivIcon.setBackground(background);
            break;
        case NOTHING:
            background.setColorFilter(ContextCompat.getColor(ctx, R.color.blue_grey_400), PorterDuff.Mode.SRC_IN);
            ivIcon.setBackground(background);
            break;
        case DANGER:
            background.setColorFilter(ContextCompat.getColor(ctx, R.color.blue_grey_800), PorterDuff.Mode.SRC_IN);
            ivIcon.setBackground(background);
            break;
    }
}

public interface onInteractionListener {
    public void moveToDetailFragment(CalenderItem calItem);
}
}

滚动时,tvDate会随机显示在列表中

我需要在哪里编写此代码来解决此问题?

2 个答案:

答案 0 :(得分:0)

请尝试以下代码。

//HERE IS ALSO A PROBLEM WHEN SCROLLING 
    if (calenderItem.getDate() != null){
        tvDate.setText(calenderItem.getDate());
        tvDate.setVisibility(View.VISIBLE);
    }else {
      tvDate.setVisibility(View.GONE);
    }

答案 1 :(得分:0)

必须是这样的:

//HERE IS A PROBLEM WHEN SCROLLING
        if(calenderItem.getCalType()== null || calenderItem.getCalType().equals("")){
            tvSubDescription.setVisibility(View.GONE);
        }else{
            tvSubDescription.setText(calenderItem.getCalType());
            tvSubDescription.setVisibility(View.VISIBLE);
        }
      //HERE IS ALSO A PROBLEM WHEN SCROLLING 
        if (calenderItem.getDate() != null){
            tvDate.setText(calenderItem.getDate());
            tvDate.setVisibility(View.VISIBLE);
        } else {
    tvDate.setVisibility(View.GONE);
    }