android-RecyclerView每隔几个项目都是相同的-展开式项目
作者:互联网
我对recyclerView有问题.我正在使用此布局在recyclerView:https://github.com/AAkira/ExpandableLayout中扩展cardView.
如果我单击某个项目将其展开,然后向下滚动,则第7个元素也会被展开.我该怎么做才能停止这种影响?我知道这是因为recyclerView会记住视图持有者的状态,并且onBindiewHolder方法中的mIsViewExpanded设置为每第7个项目为true.预先感谢您提供任何解决方案!
这是我的观看者的代码:
class EventsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView edit, cancel, expandIcon;
private ExpandableLayout expandableArea;
private boolean mIsViewExpanded = false;
public EventsViewHolder(View itemView) {
super(itemView);
expandIcon = (ImageView) itemView.findViewById(R.id.card_item_expand_icon);
expandIcon.setOnClickListener(this);
//divider = (View) itemView.findViewById(R.id.event_card_divider);
}
private void collapseArea(){
expandableArea.collapse();
edit.setVisibility(View.GONE);
cancel.setVisibility(View.GONE);
mIsViewExpanded = false;
expandIcon.setImageResource(R.drawable.vector_drawable_ic_expand_more_black___px);
}
private void expandArea(){
expandableArea.expand();
edit.setVisibility(View.VISIBLE);
cancel.setVisibility(View.VISIBLE);
mIsViewExpanded = true;
expandIcon.setImageResource(R.drawable.vector_drawable_ic_expand_less_black___px);
}
@Override
public void onClick(View view) {
if(mIsViewExpanded){
collapseArea();
isItemExpanded[getAdapterPosition()] = false;
}
else {
expandArea();
isItemExpanded[getAdapterPosition()] = true;
}
}
}
当我单击CardView中的图标时,expandableArea会根据mIsViewExpanded值而扩展或崩溃.
编辑.
适配器代码:
public class EventsRecyclerViewAdapter extends RecyclerView.Adapter<EventsRecyclerViewAdapter.EventsViewHolder> implements PopupMenu.OnMenuItemClickListener, PopupMenu.OnDismissListener {
public interface EventsRecyclerViewAdapterInterface {
public void emptyAdapter();
public void itemDeleted(int id);
}
private List<Event> listData = new ArrayList<>();
private LayoutInflater inflater;
private MainActivity context;
private View view;
private int positionToCancel;
private boolean[] isItemExpanded;
private EventsRecyclerViewAdapterInterface deleteListener;
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
removeItem(positionToCancel);
return true;
}
@Override
public void onDismiss(PopupMenu menu) {
menu.dismiss();
}
public EventsRecyclerViewAdapter(Activity context, EventsRecyclerViewAdapterInterface deleteListener) {
this.inflater = LayoutInflater.from(context);
this.context = (MainActivity) context;
this.deleteListener = deleteListener;
}
@Override
public EventsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view = inflater.inflate(R.layout.item_event_card, parent, false);
return new EventsViewHolder(view);
}
@Override
public void onBindViewHolder(EventsViewHolder holder, int position) {
Event item = listData.get(position);
if(!isItemExpanded[position]){
if(holder.mIsViewExpanded){
holder.collapseArea();
}
}
else {
holder.expandArea();
}
holder.title.setText(item.getName());
holder.place.setText(item.getLocation());
DateTimeFormatter formatter = DateTimeFormat.forPattern(StaticValues.DATE_TIME_PATTERN_FOR_VIEW);
holder.time.setText(formatter.print(item.getDate()));
holder.edit.setOnClickListener(view1 -> takeDetailsEvent(item));
holder.cancel.setOnClickListener(view1 -> {
positionToCancel = position;
MyPopupMenu popup = new MyPopupMenu(view1.getContext(), view1, context);
popup.inflate(R.menu.event_delete_menu);
popup.setOnMenuItemClickListener(this);
popup.setOnDismissListener(this);
popup.show();
});
holder.container.setOnClickListener(view1 -> {
Intent intent = new Intent(view1.getContext(), EventDetailsActivity.class);
intent.putExtra("eventId", item.getApiId());
view1.getContext().startActivity(intent);
});
}
private void removeItem(int position) {
deleteListener.itemDeleted(listData.get(position).getApiId());
listData.remove(position);
notifyDataSetChanged();
if (getItemCount() == 0) {
deleteListener.emptyAdapter();
}
}
private void takeDetailsEvent(Event event) {
Intent intent = new Intent(view.getContext(), CreateEventActivity.class);
intent.putExtra("id", event.getApiId());
view.getContext().startActivity(intent);
}
public void setListData(List<Event> eventList) {
listData = eventList;
isItemExpanded = new boolean[listData.size()];
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return listData.size();
}
}
我现在就是这样处理的.在适配器类中,我有一个布尔数组,其长度与listData相同.在与列表项相对应的位置上,我保留值:展开时为true,折叠时为false.在onBindViewHolder中,我检查位置是否展开,如果不展开,则将其折叠.这不是理想的解决方案,因为在滚动过程中项目会崩溃,而且看起来也不好.
解决方法:
RecyclerView不会创建新视图,而是会在滚动时重新使用现有视图(ScrappedViews).所以发生的事是当您扩展CardView并滚动时,由于CardView被重用,您将获得另一个扩展视图.
因此,只需在Adapter的onBindView()中折叠/展开视图,因为您需要将状态与适配器列表中的每个项目相关联.
要在滚动过程中解决扩展/折叠动画,只需设置setDuration(0),它使您可以扩展/折叠而没有任何延迟或动画,从而获得更流畅的滚动体验.
标签:android-recyclerview,expandablelistview,android 来源: https://codeday.me/bug/20191026/1938904.html