如何根据类别实现带有节头的RecyclerView?

如何根据类别实现带有节头的RecyclerView?,第1张

概述我想在列表中实现部分.我有一份任务清单. List有一个自定义适配器,它扩展了recyclerview刷卡适配器,因为我已经实现了向recyclerview的滑动手势. 因此,现在任务列表与已完成和待处理任务一起显示.每个列表项都有一个复选框,显示任务已完成或待处理. 如果选中复选框,则任务完成,反之亦然.现在我想用标题创建两个部分.一个用于已完成的任务,另一个用于待处理任 所以完成的任务应该显示 @H_502_1@我想在列表中实现部分.我有一份任务清单. List有一个自定义适配器,它扩展了recyclervIEw刷卡适配器,因为我已经实现了向recyclervIEw的滑动手势.

因此,现在任务列表与已完成和待处理任务一起显示.每个列表项都有一个复选框,显示任务已完成或待处理.

如果选中复选框,则任务完成,反之亦然.现在我想用标题创建两个部分.一个用于已完成的任务,另一个用于待处理任

所以完成的任务应该显示在已完成的部分内,反之亦然.此外,如果未检查任务,即挂起,如果用户选中复选框,则该项应从待处理部分中删除,并应添加到已完成的部分,反之亦然.

我检查了一个库的部分.

https://github.com/afollestad/sectioned-recyclerview

但是当我尝试实现库时,我得到了错误,即适配器无法扩展两个类,因为之前我已经扩展了recyclelervIEw swipe库.

适配器:

public class IAdapter extends RecyclerSwipeAdapter<IAdapter.ItemVIEwHolder> {    public ArrayList<Task> items;    Context conext;    public int _mID;    List<Task> itemsPendingRemoval = new ArrayList<>();    public IAdapter(Context context,ArrayList<Task> item) {        this.conext=context;       this.items=item;    }    @OverrIDe    public int getItemCount() {        return items.size();    }    public static class ItemVIEwHolder extends RecyclerVIEw.VIEwHolder {        Task task;        CheckBox cb;        SwipeLayout swipeLayout;        TasktableHelper tasktableHelper;        ItemVIEwHolder(final VIEw itemVIEw) {            super(itemVIEw);            tasktableHelper= new TasktableHelper(itemVIEw.getContext());            swipeLayout = (SwipeLayout) itemVIEw.findVIEwByID(R.ID.swipe);            cb = (CheckBox) itemVIEw.findVIEwByID(R.ID.checkBox);        }    }    @OverrIDe    public voID onBindVIEwHolder(final ItemVIEwHolder itemVIEwHolder,final int i) {        itemVIEwHolder.cb.setText(items.get(i).getTitle());        itemVIEwHolder.task = items.get(i);        int taskID = itemVIEwHolder.task.getID();        itemVIEwHolder.task = itemVIEwHolder.tasktableHelper.getTask(taskID);        int status = itemVIEwHolder.task.getStatus();        if(status == 0)        {            itemVIEwHolder.cb.setTextcolor(color.WHITE);        }        else {            itemVIEwHolder.cb.setChecked(true);            itemVIEwHolder.cb.setTextcolor(color.parsecolor("#B0BEC5"));        }       itemVIEwHolder.cb.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton buttonVIEw,boolean isChecked) {                if (isChecked) {                    itemVIEwHolder.cb.setTextcolor(color.parsecolor("#B0BEC5"));                    itemVIEwHolder.task.setStatus(1);                    itemVIEwHolder.tasktableHelper.updateStatus(itemVIEwHolder.task);                }                else                {                    itemVIEwHolder.cb.setTextcolor(color.WHITE);                    itemVIEwHolder.task.setStatus(0);                    itemVIEwHolder.tasktableHelper.updateStatus(itemVIEwHolder.task);                }            }        });        final Task item = items.get(i);        itemVIEwHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Right,itemVIEwHolder.swipeLayout.findVIEwByID(R.ID.bottom_wrapper_2));        itemVIEwHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);        itemVIEwHolder.swipeLayout.setondoubleclickListener(new SwipeLayout.DoubleClickListener() {            @OverrIDe            public voID ondoubleclick(SwipeLayout layout,boolean surface) {                Toast.makeText(conext,"DoubleClick",Toast.LENGTH_SHORT).show();            }        });        itemVIEwHolder.swipeLayout.findVIEwByID(R.ID.trash2).setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                mItemmanger.removeShownLayouts(itemVIEwHolder.swipeLayout);                items.remove(i);                notifyItemRemoved(i);                notifyItemRangeChanged(i,items.size());                mItemmanger.closeAllitems();                itemVIEwHolder.tasktableHelper.deleteTask(item);                _mID = item.getAlertID();                cancelNotification();                Toast.makeText(vIEw.getContext(),"Deleted " + itemVIEwHolder.cb.getText().toString() + "!",Toast.LENGTH_SHORT).show();            }        });        itemVIEwHolder.swipeLayout.findVIEwByID(R.ID.done).setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                itemVIEwHolder.task.setStatus(1);                itemVIEwHolder.tasktableHelper.updateStatus(itemVIEwHolder.task);                itemVIEwHolder.cb.setChecked(true);                Toast.makeText(conext,"Task Completed.",Toast.LENGTH_SHORT).show();            }        });        itemVIEwHolder.swipeLayout.getSurfaceVIEw().setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                boolean mEditMode;                int ID = item.getID();                int priority = item.getTaskPriority();                String Title = item.getTitle();                String alertDate = item.getAlertDate();                String alertTime = item.getAlertTime();                String dueDate = item.getDueDate();                String dueTime = item.getDueTime();                _mID = item.getAlertID();                int ListID = item.getList();                mEditMode = true;                Intent i = new Intent(conext,AddTaskActivity.class);                i.putExtra("taskID",ID);                i.putExtra("taskTitle",Title);                i.putExtra("taskPriority",priority);                i.putExtra("taskAlertTime",alertTime);                i.putExtra("taskAlertDate",alertDate);                i.putExtra("taskDueDate",dueDate);                i.putExtra("taskDueTime",dueTime);                i.putExtra("taskListID",ListID);                i.putExtra("EditMode",mEditMode);                i.putExtra("AlertID",_mID);                conext.startActivity(i);            }        });        mItemmanger.bindVIEw(itemVIEwHolder.itemVIEw,i);    }    @OverrIDe    public ItemVIEwHolder onCreateVIEwHolder(VIEwGroup vIEwGroup,int position) {            VIEw itemVIEw = LayoutInflater.                    from(vIEwGroup.getContext()).                    inflate(R.layout.card_layout,vIEwGroup,false);            return new ItemVIEwHolder(itemVIEw);    }        @OverrIDe    public voID onAttachedToRecyclerVIEw(RecyclerVIEw recyclerVIEw) {        super.onAttachedToRecyclerVIEw(recyclerVIEw);    }    public voID remove(int position) {       Task item = items.get(position);        if (itemsPendingRemoval.contains(item)) {            itemsPendingRemoval.remove(item);        }        if (items.contains(item)) {            items.remove(position);            notifyItemRemoved(position);        }    }    public voID cancelNotification()    {        AlarmManager alarmManager = (AlarmManager)conext.getSy@R_502_6563@Service(Context.ALARM_SERVICE);        Intent intent = new Intent(conext,NotificationReceiver.class);        PendingIntent pendingIntent = PendingIntent.getbroadcast(conext,_mID,intent,0);        alarmManager.cancel(pendingIntent);    }    @OverrIDe    public int getSwipeLayoutResourceID(int position) {        return R.ID.swipe;    }}

编辑:

扩展sectionedRecyclervIEw适配器后的onBindVIEwHolder方法:

@OverrIDepublic voID onBindVIEwHolder(ItemVIEwHolder itemVIEwHolder,int section,int i,int absoluteposition) {    itemVIEwHolder.cb.setText(items.get(i).getTitle());    itemVIEwHolder.task = items.get(i);    int taskID = itemVIEwHolder.task.getID();    itemVIEwHolder.task = itemVIEwHolder.tasktableHelper.getTask(taskID);    int status = itemVIEwHolder.task.getStatus();    if(status == 0)    {        itemVIEwHolder.cb.setTextcolor(color.WHITE);    }    else {        itemVIEwHolder.cb.setChecked(true);        itemVIEwHolder.cb.setTextcolor(color.parsecolor("#B0BEC5"));    }   itemVIEwHolder.cb.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {        @OverrIDe        public voID onCheckedChanged(Compoundbutton buttonVIEw,boolean isChecked) {            if (isChecked) {                itemVIEwHolder.cb.setTextcolor(color.parsecolor("#B0BEC5"));                itemVIEwHolder.task.setStatus(1);                itemVIEwHolder.tasktableHelper.updateStatus(itemVIEwHolder.task);            }            else            {                itemVIEwHolder.cb.setTextcolor(color.WHITE);                itemVIEwHolder.task.setStatus(0);                itemVIEwHolder.tasktableHelper.updateStatus(itemVIEwHolder.task);            }        }    });    final Task item = items.get(i);    itemVIEwHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Right,itemVIEwHolder.swipeLayout.findVIEwByID(R.ID.bottom_wrapper_2));    itemVIEwHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);    itemVIEwHolder.swipeLayout.setondoubleclickListener(new SwipeLayout.DoubleClickListener() {        @OverrIDe        public voID ondoubleclick(SwipeLayout layout,boolean surface) {            Toast.makeText(conext,Toast.LENGTH_SHORT).show();        }    });    itemVIEwHolder.swipeLayout.findVIEwByID(R.ID.trash2).setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw vIEw) {            mItemmanger.removeShownLayouts(itemVIEwHolder.swipeLayout);            items.remove(i);            notifyItemRemoved(i);            notifyItemRangeChanged(i,items.size());            mItemmanger.closeAllitems();            itemVIEwHolder.tasktableHelper.deleteTask(item);            _mID = item.getAlertID();            cancelNotification();            Toast.makeText(vIEw.getContext(),Toast.LENGTH_SHORT).show();        }    });    itemVIEwHolder.swipeLayout.findVIEwByID(R.ID.done).setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {            itemVIEwHolder.task.setStatus(1);            itemVIEwHolder.tasktableHelper.updateStatus(itemVIEwHolder.task);            itemVIEwHolder.cb.setChecked(true);            Toast.makeText(conext,Toast.LENGTH_SHORT).show();        }    });    itemVIEwHolder.swipeLayout.getSurfaceVIEw().setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {            boolean mEditMode;            int ID = item.getID();            int priority = item.getTaskPriority();            String Title = item.getTitle();            String alertDate = item.getAlertDate();            String alertTime = item.getAlertTime();            String dueDate = item.getDueDate();            String dueTime = item.getDueTime();            _mID = item.getAlertID();            int ListID = item.getList();            mEditMode = true;            Intent i = new Intent(conext,AddTaskActivity.class);            i.putExtra("taskID",ID);            i.putExtra("taskTitle",Title);            i.putExtra("taskPriority",priority);            i.putExtra("taskAlertTime",alertTime);            i.putExtra("taskAlertDate",alertDate);            i.putExtra("taskDueDate",dueDate);            i.putExtra("taskDueTime",dueTime);            i.putExtra("taskListID",ListID);            i.putExtra("EditMode",mEditMode);            i.putExtra("AlertID",_mID);            conext.startActivity(i);        }    });    mItemmanger.bindVIEw(itemVIEwHolder.itemVIEw,i);}

我怎样才能做到这一点?有人可以帮忙吗?

谢谢..

解决方法 将回收站视图拆分为多个部分的最简单方法是使用包含标题和项目的布局,然后在标题相同时更改可见性.

布局:

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content">    <TextVIEw        androID:ID="@+ID/tvheader"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_gravity="start"        androID:padding="16dp"        androID:textAppearance="?androID:attr/textAppearanceMedium"        androID:textcolor="@color/primary"        androID:textStyle="bold"        tools:text="A" />    <TextVIEw        androID:ID="@+ID/tvname"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_below="@+ID/tvheader"        androID:background="?androID:attr/selectableItemBackground"        androID:padding="16dp"        androID:textAppearance="?androID:attr/textAppearanceMedium"        tools:text="Adam" /></relativeLayout>

适配器(2018年Kotlin版):

class ContactAdapter @Inject constructor() : RecyclerVIEw.Adapter<ContactAdapter.VIEwHolder>() {    var onItemClick: ((Contact) -> Unit)? = null    var contacts = emptyList<Contact>()    overrIDe fun getItemCount(): Int {        return contacts.size    }    overrIDe fun onCreateVIEwHolder(parent: VIEwGroup,vIEwType: Int): VIEwHolder {        return VIEwHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_contact,parent,false))    }    overrIDe fun onBindVIEwHolder(holder: VIEwHolder,position: Int) {        val name = contacts[position].name        holder.header.text = name.substring(0,1)        holder.name.text = name        // if not first item,check if item above has the same header        if (position > 0 && contacts[position - 1].name.substring(0,1) == name.substring(0,1)) {            holder.headerTextVIEw.visibility = VIEw.GONE        } else {            holder.headerTextVIEw.visibility = VIEw.VISIBLE        }    }    inner class VIEwHolder(itemVIEw: VIEw) : RecyclerVIEw.VIEwHolder(itemVIEw) {        val headerTextVIEw: TextVIEw = itemVIEw.tvheader        val nameTextVIEw: TextVIEw = itemVIEw.tvname        init {            itemVIEw.setonClickListener {                onItemClick?.invoke(contacts[adapterposition])            }        }    }}

可能也有帮助:RecyclerView itemClickListener in Kotlin

旧Java适配器版本:

public class RecyclerAdapter extends RecyclerVIEw.Adapter<RecyclerAdapter.DataVIEwHolder> {    private List<Contact> mData;    @Inject    public RecyclerAdapter() {        mData = new ArrayList<>();    }    public voID setData(List<Contact> data) {        mData = data;    }    public Contact getItem(int position){        return mData.get(position);    }    @OverrIDe    public DataVIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {        VIEw itemVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact,false);        return new DataVIEwHolder(itemVIEw);    }    @OverrIDe    public voID onBindVIEwHolder(final DataVIEwHolder holder,int position) {        Contact contact = mData.get(position);        holder.headerTextVIEw.setText(contact.getname().substring(0,1));        holder.nameTextVIEw.setText(contact.getname());        // if not first item check if item above has the same header        if (position > 0 && mData.get(position - 1).getname().substring(0,1).equals(contact.getname().substring(0,1))) {            holder.headerTextVIEw.setVisibility(VIEw.GONE);        } else {            holder.headerTextVIEw.setVisibility(VIEw.VISIBLE);        }    }    @OverrIDe    public int getItemCount() {        return mData.size();    }    public class DataVIEwHolder extends RecyclerVIEw.VIEwHolder {        @BindVIEw(R.ID.text_header)        TextVIEw headerTextVIEw;        @BindVIEw(R.ID.text_name)        TextVIEw nameTextVIEw;        public DataVIEwHolder(VIEw itemVIEw) {            super(itemVIEw);            ButterKnife.bind(this,itemVIEw);        }    }}
总结

以上是内存溢出为你收集整理的如何根据类别实现带有节头的RecyclerView?全部内容,希望文章能够帮你解决如何根据类别实现带有节头的RecyclerView?所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存