mercredi 27 avril 2016

How to add sections for checked and unchecked items in recyclerview?

I have a list of tasks in recyclerview. Each task item has a check box to show if the task is completed or pending.

Now I want to show two sections in my list. One for checked items and another for unchecked items.

I have gone through this library to add the sections.

http://ift.tt/1VDKx7l

But how can I divide the items in list on the basis of they are checked or not? Also I want to add the task into another section if checked or unchecked , i.e onClick.

If task is unchecked, and if I check it, it should get added to the completed section and vise versa.

I have a recyclerview now with swipe layout. Following is the adapter.

adapetr:

 public class IAdapter extends RecyclerSwipeAdapter<IAdapter.ItemViewHolder> , SectionedRecyclerViewAdapter<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 getSectionCount() {
        return 2;
    }

    @Override
    public int getItemCount(int section) {
        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.getSystemService(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;
    }
}

Can anyone help please? Thank you..




Aucun commentaire:

Enregistrer un commentaire