How do I change the color of cardview after a longclick on it?

Asked

Viewed 475 times

0

inserir a descrição da imagem aqui

How do I make it happen?

I would like to after a longClick in the cardview the color of the same change indicating which or which ones I selected!

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
            View.OnLongClickListener {
        View selectedOverlay;
              /*...*/
}

 public ViewHolder(View itemView){
   selectedOverlay = itemView.findViewById(R.id.selected_overlay);
 }


@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Item item = items.get(position);
        // Highlight the item if it's selected
        holder.selectedOverlay.setVisibility(isSelected(position) ? View.VISIBLE : View.INVISIBLE);
    }
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardUseCompatPadding="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="8dp" >

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            style="@style/Base.TextAppearance.AppCompat.Headline" />

        <TextView
            android:id="@+id/subtitle"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            style="@style/Base.TextAppearance.AppCompat.Subhead" />

    </LinearLayout>

    <View
        android:id="@+id/selected_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/dim_foreground_disabled_material_dark"
        android:visibility="invisible" />

</android.support.v7.widget.CardView>
  • Edit your question and add the code you are using.

1 answer

1

You’ve tried this before?

 <--your object-->.setOnLongClickListener(new View.OnLongClickListener() {

    //<--your object-->deve ser substituido pelo cardview em que se faz o clik.
    @Override
    public boolean onLongClick(View v) {

    //muda cor do cardview
    return true;

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.