lundi 18 septembre 2017

How to select CheckBox onLongClick in a recyclerview

I Have a recyclerview when long Click is perform the recyclerview switches into actionMode and then the user can select items in the recyclerview for deletion. so the problem that i am facing right now is when long click is perform the item that is long click is not selected, though the recyclerview switches to actionMode. So how can i check the item immediatly after longclick is perform?

Here is the code for Recyclerview Adapter

     @Override
    public void onBindViewHolder(final pet_viewholder holder, final int position) {
        holder.pet_name.setText(items.get(position).getPet_name());
        holder.pet_breed.setText(items.get(position).getPet_breed());
        holder.pet_age.setText(items.get(position).getPet_age());
        holder.pet_bday.setText(items.get(position).getPet_bday());
        holder.pet_name.setTag(items.get(position).getPet_name());

    if(mHome.is_in_action_mode){
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.chregis_pet.performClick();
                if(holder.chregis_pet.isChecked()){
                    holder.cardView.setBackgroundColor(Color.GRAY);
                }
                else
                {
                    holder.cardView.setBackgroundColor(Color.TRANSPARENT);
                }
            }
        });
    }
    else{
        holder.view.setOnClickListener(null);
        holder.cardView.setBackgroundColor(Color.TRANSPARENT);
        holder.chregis_pet.setVisibility(View.GONE);
        holder.chregis_pet.setChecked(false);
    }
        if(holder.chregis_pet.isChecked()){
            holder.cardView.setBackgroundColor(Color.GRAY);
        }
        else
        {
            holder.cardView.setBackgroundColor(Color.TRANSPARENT);
        }
 public static class pet_viewholder extends RecyclerView.ViewHolder implements  View.OnClickListener {
       public CheckBox chregis_pet;
        TextView pet_name;
        TextView pet_breed;
        TextView pet_age;
        TextView pet_bday;
        ImageView pet_image;
        RecyclerView recyclerView;
        View view;
        CardView cardView;
        HomeActivity homeActivity;

        public pet_viewholder(View itemView, HomeActivity homeActivity) {
            super(itemView);
            this.view=itemView.findViewById(R.id.view);
            pet_name=(TextView)itemView.findViewById(R.id.name_pet);
            pet_breed=(TextView)itemView.findViewById(R.id.breed_pet);
            pet_age=(TextView)itemView.findViewById(R.id.age_pet);
            pet_bday=(TextView)itemView.findViewById(R.id.bday_pet);
            pet_image=(ImageView) itemView.findViewById(R.id.pic_breed);
            recyclerView=(RecyclerView)itemView.findViewById(R.id.reg_pet);
            cardView=(CardView)itemView.findViewById(R.id.pet_card);
            chregis_pet=(CheckBox)itemView.findViewById(R.id.chkregis_pet);
            this.homeActivity=homeActivity;
            view.setOnLongClickListener(homeActivity);
            this.chregis_pet.setOnClickListener(this);

And here is the code for HomeActivity where i implemented the Recyclerviews actionMode

         @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if(mtoggle.onOptionsItemSelected(item))
            {
                return true;
            }
            else if(item.getItemId()==android.R.id.home)
            {
                clearMode();
                adapter.notifyDataSetChanged();
            }
            else if(item.getItemId()==R.id.nav_register)
            {
                if(counter==0){
                    Toast.makeText(HomeActivity.this, "No selected pets", Toast.LENGTH_SHORT).show();
                }
                else
                {

                    AlertDialog.Builder delete_items=new AlertDialog.Builder(HomeActivity.this);
                    delete_items.setMessage("Do you want to remove selected pets?").setCancelable(false)
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    is_in_action_mode = false;
                                    db_adapter adapter1=(db_adapter)adapter;
                                    adapter1.updateAdapter(selection_list);
                                    Toast.makeText(HomeActivity.this, "Pets has been removed", Toast.LENGTH_SHORT).show();
                                    clearMode();
                                }
                            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });


          AlertDialog alertDialog=delete_items.create();
                alertDialog.setTitle("Pet Guide 101");
                alertDialog.show();
            }

        }


        return super.onOptionsItemSelected(item);
    }
@Override
    public boolean onLongClick(View v) {
        Vibrator vibrator=(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(400);
        mtoolbar.getMenu().clear();
        mtoolbar.inflateMenu(R.menu.action_mode_menu);
        txt.setText("0 item is selected");
        is_in_action_mode=true;
        adapter.notifyDataSetChanged();
        mtoggle.setDrawerIndicatorEnabled(false);
        return true;
    }
    public void preparedselection(View view, int position){
            if(((CheckBox)view).isChecked()){
                selection_list.add(arrayList.get(position));
                counter=counter+1;
                updateCounter(counter);
            }
        else
            {
                selection_list.remove(arrayList.get(position));
                counter=counter-1;
                updateCounter(counter);
            }
    }

    public void updateCounter(int counter){

        if(counter==0){
            txt.setText("0 item is selected");
        }
        else{
            txt.setText(counter+" item is selected");
        }
    }
    public void clearMode()
    {
        is_in_action_mode=false;
        mtoolbar.getMenu().clear();
        mtoggle.setDrawerIndicatorEnabled(true);
        txt.setText("Home");
        counter=0;
        selection_list.clear();
    }

    @Override
    public void onBackPressed() {
        if(is_in_action_mode)
        {
            clearMode();
            adapter.notifyDataSetChanged();
        }
else
        {
            super.onBackPressed();
        }

    }




Aucun commentaire:

Enregistrer un commentaire