0

I have CardViews in a ReyclerView list. I set up so that a LongClick on a CardView launches a DialogFragment. On the CardView is a checkbox and I want to exclude the checkbox from the LongClick method; that is, I don't want the DialogFragment to load if the checkbox is LongClicked. I added 'android:longClickable="false"' and 'android:focusable="false"' to the CardView layout but that did not work. How would I achieve?

Adapter file:    
...
public static class ListViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {

    CardView singleCardView;

    TextView cardBlankText1;
    TextView cardBlankTextNumstotal;
    CheckBox chkSelected;
    TextView cardBlankText2;
    TextView cardBlankText4;
    TextView cardBlankText5A;
    TextView cardBlankText5B;
    TextView cardBlankText6;
    TextView cardBlankText7;
    TextView cardBlankText8;

    public ListViewHolder(View itemView) {
        super(itemView);

        itemView.setOnLongClickListener(this);
        singleCardView = (CardView) itemView.findViewById(R.id.singlecard_view1);

        chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
        cardBlankText1 = (TextView) itemView.findViewById(R.id.cardBlankText1);
        cardBlankTextNumstotal = (TextView) itemView.findViewById(R.id.cardBlankTextNumstotal);
        cardBlankText2 = (TextView) itemView.findViewById(R.id.cardBlankText2);
        cardBlankText4 = (TextView) itemView.findViewById(R.id.cardBlankText4);
        cardBlankText5A = (TextView) itemView.findViewById(R.id.cardBlankText5A);
        cardBlankText5B = (TextView) itemView.findViewById(R.id.cardBlankText5B);
        cardBlankText6 = (TextView) itemView.findViewById(R.id.cardBlankText6);
        cardBlankText7 = (TextView) itemView.findViewById(R.id.cardBlankText7);
        cardBlankText8 = (TextView) itemView.findViewById(R.id.cardBlankText8);

        chkSelected.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick (View view){
                return true;
            }
        });
    } 
    @Override
    public boolean onLongClick(View view) {
        clickListener.onItemLongClick(getAdapterPosition(), view);
        return false;
    }
}

public void setonItemClickListener(ClickListener  clickListener) {
    ListAdapter.clickListener = clickListener;
}

public interface ClickListener {
    void onItemLongClick(int position, View view);
}
AJW
  • 1,578
  • 3
  • 36
  • 77

1 Answers1

1

Get the checkbox and set an empty OnLongClickListener on it. This will consume the event and prevent the parent CardView from getting it:

View checkbox = container.findViewById(R.id.checkbox);

checkbox.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
      //make sure to return true here so the parent cardview doesn't get the event.
      return true;
    }
});
Dr. Nitpick
  • 1,662
  • 1
  • 12
  • 16
  • Ok, will try. If I set this up in the RecyclerView's ViewHolder, Android Studio gives error message "Cannot resolve symbol 'container'". Please advise. – AJW Jul 03 '16 at 05:11
  • `container` was the name I gave to your CardView's Layout. If you are in an adapter and setting up the ViewHolder you can replace `container` with the variable for the layout you inflate there. – Dr. Nitpick Jul 03 '16 at 05:17
  • app is crashing, Studio doesn't like the "View checkbox..." line. I will post full Adapter code above, do you know what I am missing? – AJW Jul 03 '16 at 05:25
  • Thanks, AJW that is helpful. All you have to do it remove the line where I make checkbox (`View checkbox = chkSelected.findViewById(R.id.chkSelected)` ) and replace all my usages of the variable `checkbox` with your variable `chkSelected`. – Dr. Nitpick Jul 03 '16 at 14:35
  • The reason it was causing a problem was you were looking inside of the variable 'chkSelected` for the view chkSelected. All you need to do it use the checkbox view you already have. – Dr. Nitpick Jul 03 '16 at 14:43
  • Ok I deleted the "View checkbox..." line and changed the checkbox to chkSelected for the new View.OnLongClickListener method. App still crashing. Do you think that is happening because it is conflicting with the other ClickListener code that is below it? I show all of the Listener code above now. – AJW Jul 03 '16 at 15:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116302/discussion-between-dr-nitpick-and-ajw). – Dr. Nitpick Jul 03 '16 at 15:54