滚动时 RecyclerView 问题
RecyclerView problems when scrolling
我有一个可扩展的 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);
}
}
然后我在 CalendarItemViewHolder 中绑定我的 Holder
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 随机显示在我的列表中
我需要在哪里写这段代码来解决这个问题?
请尝试使用以下代码。
//HERE IS ALSO A PROBLEM WHEN SCROLLING
if (calenderItem.getDate() != null){
tvDate.setText(calenderItem.getDate());
tvDate.setVisibility(View.VISIBLE);
}else {
tvDate.setVisibility(View.GONE);
}
一定是这样:
//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);
}
我有一个可扩展的 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);
}
}
然后我在 CalendarItemViewHolder 中绑定我的 Holder
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 随机显示在我的列表中
我需要在哪里写这段代码来解决这个问题?
请尝试使用以下代码。
//HERE IS ALSO A PROBLEM WHEN SCROLLING
if (calenderItem.getDate() != null){
tvDate.setText(calenderItem.getDate());
tvDate.setVisibility(View.VISIBLE);
}else {
tvDate.setVisibility(View.GONE);
}
一定是这样:
//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);
}