android-RecyclerView每隔几个项目都是相同的-展开式项目

android-RecyclerView每隔几个项目都是相同的-展开式项目,第1张

概述我对recyclerView有问题.我正在使用此布局在recyclerView:https://github.com/AAkira/ExpandableLayout中扩展cardView.如果我单击某个项目将其展开,然后向下滚动,则第7个元素也会被展开.我该怎么做才能停止这种影响?我知道这是因为recyclerView会记住视图持有者的状态,并且onBindie

我对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;@OverrIDepublic boolean onMenuItemClick(MenuItem menuItem) {    removeItem(positionToCancel);    return true;}@OverrIDepublic voID ondismiss(PopupMenu menu) {    menu.dismiss();}public EventsRecyclerVIEwAdapter(Activity context, EventsRecyclerVIEwAdapterInterface deleteListener) {    this.inflater = LayoutInflater.from(context);    this.context = (MainActivity) context;    this.deleteListener = deleteListener;}@OverrIDepublic EventsVIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) {    vIEw = inflater.inflate(R.layout.item_event_card, parent, false);    return new EventsVIEwHolder(vIEw);}@OverrIDepublic 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();}@OverrIDepublic int getItemCount() {    return ListData.size();}

}

我现在就是这样处理的.在适配器类中,我有一个布尔数组,其长度与ListData相同.在与列表项相对应的位置上,我保留值:展开时为true,折叠时为false.在onBindVIEwHolder中,我检查位置是否展开,如果不展开,则将其折叠.这不是理想的解决方案,因为在滚动过程中项目会崩溃,而且看起来也不好.

解决方法:

RecyclerVIEw不会创建新视图,而是会在滚动时重新使用现有视图(ScrappedVIEws).所以发生的事是当您扩展CardVIEw并滚动时,由于CardVIEw被重用,您将获得另一个扩展视图.

因此,只需在Adapter的onBindVIEw()中折叠/展开视图,因为您需要将状态与适配器列表中的每个项目相关联.

要在滚动过程中解决扩展/折叠动画,只需设置setDuration(0),它使您可以扩展/折叠而没有任何延迟或动画,从而获得更流畅的滚动体验.

总结

以上是内存溢出为你收集整理的android-RecyclerView每隔几个项目都是相同的-展开式项目全部内容,希望文章能够帮你解决android-RecyclerView每隔几个项目都是相同的-展开式项目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1090854.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存