4

Actually i want to Enabled/Disable a specific row of RecyclerView so in Adapter Class i implement my Logic in onBindViewHolder function but when i apply the Enabled Property than it doesn't create any effect on my holder item. So can anyone tell me how it is done.

Note:

  1. I know there are many similar questions out there but i tried most of them but it didn't work.

  2. Visiblity Property is working on my holder Item

Code :

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
  RegsiteredCarDisplayItem displayItem = displayItems.get(position);

    holder.modelName.setText(displayItem.getMyOwnedCar().getCarModelName());        if(selected_usersList.contains(displayItems.get(position).getMyOwnedCar()))
        holder.childView.setBackgroundColor(ContextCompat.getColor(context, R.color.buttonbackground));

    else
        holder.childView.setBackgroundColor(ContextCompat.getColor(context, R.color.textViewBackground));

    if (displayItems.get(position).getVisible()){
        Log.d("AIAITRUE", String.valueOf(displayItems.get(position)));
        holder.childView.setEnabled(true);
        holder.childView.setClickable(true);

    }else{
            // here i want to Disable my Holder View but the below line is not working, but i use another property like alpha instaed of Enabled property than property(refrence alpha) is working 
           holder.childView.setEnabled(false);
        }

}

XML:

 <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?android:attr/selectableItemBackground"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardElevation="0dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardPreventCornerOverlap="false"

  >
<RelativeLayout
    android:id="@+id/parentView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="false">
    <RelativeLayout
        android:id="@+id/relativelayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/padding"
        android:layout_marginBottom="@dimen/padding"
        android:orientation="vertical"
        android:clickable="false"
        tools:ignore="UselessParent">


        <ImageView
            android:id="@+id/icons"
            android:layout_width="50dp"
            android:layout_height="30dp"


    android:layout_alignBottom="@id/linearlayout"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="false"
            android:layout_marginEnd="@dimen/margin_five"
            android:layout_marginRight="@dimen/margin_five"
            android:layout_marginBottom="-15dp"
            android:background="@color/colorAccent"
            android:clickable="false"
            android:adjustViewBounds="true"
            tools:ignore="RtlHardcoded"/>
        <LinearLayout
            android:id="@+id/linearlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/icons"
            android:layout_marginBottom="@dimen/padding"
            android:orientation="horizontal"
            android:clickable="false"
            tools:ignore="RtlHardcoded">
            <TextView
                android:id="@+id/modelNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/padding"
                android:gravity="start"
                android:clickable="false"
                android:textSize="@dimen/text_size"
                android:drawablePadding="@dimen/drawable_padding"
                android:drawableTint="@color/buttonbackground"
                tools:ignore="RtlSymmetry"
                tools:targetApi="m"
                android:textColor="@color/colorAccent"/>
        </LinearLayout>


        <TextView
            android:id="@+id/registration_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearlayout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="@dimen/left_margin_registration_number"
            android:layout_marginStart="@dimen/left_margin_registration_number"
            android:clickable="false"
            android:textColor="@color/text_color"/>


        <ImageView
            android:id="@+id/next_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/linearlayout"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="false"
            android:layout_marginBottom="-10dp"
            android:clickable="false"
            android:foregroundGravity="right"
            android:tint="@color/buttonbackground"
            app:srcCompat="@drawable/ic_next"/>



    </RelativeLayout>


</RelativeLayout>


</android.support.v7.widget.CardView>

VieHolder Class:

class ViewHolder extends RecyclerView.ViewHolder{
        TextView carName,modelName,registrationNumber; View cardView;
    RelativeLayout childView;ImageView imageView; ImageView imageicon;
    public ViewHolder(View itemView) {
        super(itemView);
        Typeface custom_font = Typeface.createFromAsset(context.getAssets(),  "fonts/helvetica-neue-ce-55-roman.ttf");
        Typeface custom_fonts = Typeface.createFromAsset(context.getAssets(),  "fonts/helvetica-neue-ce-35-thin.ttf");
        cardView = itemView.findViewById(R.id.cardView);
        modelName=(TextView)itemView.findViewById(R.id.modelNumber);
        registrationNumber=(TextView)itemView.findViewById(R.id.registration_number);
        childView =(RelativeLayout)itemView.findViewById(R.id.parentView);
        imageView=(ImageView) itemView.findViewById(R.id.next_icon);
        imageicon=(ImageView)itemView.findViewById(R.id.icons);
        modelName.setTypeface(custom_font);
        registrationNumber.setTypeface(custom_fonts);


    }


}
techDigi
  • 251
  • 3
  • 18

8 Answers8

4

Check that you are actually clicking on the view you think you are clicking on. You may be overlaying the view for which you are trying to suppress clicks with another view so, even if you disabled the view you are interested in, you will see clicks from the overlaying view. You can check the id of the view passed into the click handler to check this out.

If this is the case, then setting other attributes such as background color and alpha would still work.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
2

try this for all views:

holder.modelName.clearFocus();
            holder.modelName.setEnabled(false);
            holder.modelName.setClickable(false);
//registrationNumber
 holder.registrationNumber.clearFocus();
            registrationNumber.modelName.setEnabled(false);
            registrationNumber.modelName.setClickable(false);

also try below code at end not at start:

if (displayItem.getVisible()){
          /*  holder.ll_listitem.setEnabled(true);
            holder.ll_listitem.setClickable(true);*/

        }else{
            Log.d("OnFalse",String.valueOf(displayItem.getVisible()));
           holder.ll_listitem.clearFocus();
            holder.ll_listitem.setEnabled(false);
            holder.ll_listitem.setClickable(false);
        }
Bapusaheb Shinde
  • 839
  • 2
  • 13
  • 16
2

I don't see where you change the value of displayItems.get(position).getVisible().

Are you sure to call notifyItemChanged(int) or notifyDataSetChanged() when the state change ?

Gut
  • 331
  • 1
  • 10
1

use this code :

        holder.childView.setEnabled(false);
        holder.childView.setClickable(false);
Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28
0

It might be possible that the view for which you have called setEnabled(false) is actually getting disabled but you are clicking on any of the child views, which are still enabled. To disable those views, you can refer to this question.

Karan
  • 356
  • 2
  • 6
0

You have to do some little changes, You can put one extra layout of gray color on top of your rowHolder for disable functionality.

For Example:

<?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"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardElevation="0dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:id="@+id/parentView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false">

        <RelativeLayout
            android:id="@+id/relativelayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:clickable="false"
            android:orientation="vertical"
            tools:ignore="UselessParent">


            <ImageView
                android:id="@+id/icons"
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:layout_alignBottom="@id/linearlayout"
                android:layout_alignParentLeft="true"
                android:layout_centerHorizontal="false"
                android:layout_centerVertical="true"
                android:layout_marginBottom="-15dp"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                android:adjustViewBounds="true"
                android:background="@color/colorAccent"
                android:clickable="false"
                tools:ignore="RtlHardcoded" />

            <LinearLayout
                android:id="@+id/linearlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_toRightOf="@id/icons"
                android:clickable="false"
                android:orientation="horizontal"
                tools:ignore="RtlHardcoded">

                <TextView
                    android:id="@+id/modelNumber"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clickable="false"
                    android:drawablePadding="5dp"
                    android:drawableTint="@color/colorAccent"
                    android:gravity="start"
                    android:paddingLeft="5dp"
                    android:textColor="@color/colorAccent"
                    android:textSize="15sp"
                    tools:ignore="RtlSymmetry"
                    tools:targetApi="m" />
            </LinearLayout>


            <TextView
                android:id="@+id/registration_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/linearlayout"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:clickable="false"
                android:textColor="@color/colorPrimary" />


            <ImageView
                android:id="@+id/next_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/linearlayout"
                android:layout_alignParentRight="true"
                android:layout_centerHorizontal="false"
                android:layout_centerVertical="true"
                android:layout_marginBottom="-10dp"
                android:clickable="false"
                android:foregroundGravity="right"
                android:tint="@color/colorAccent"
                app:srcCompat="@drawable/ic_profle_icon" />


        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rLayoutDisable"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#99000000"
            android:visibility="visible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Disable"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                android:textStyle="bold" />

        </RelativeLayout>

    </RelativeLayout>


</android.support.v7.widget.CardView>

And Now set visibility of rLayoutDisable visible or gone and remove click event of other layout if you needed.

like this:

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        RegsiteredCarDisplayItem displayItem = displayItems.get(position);

        holder.modelName.setText(displayItem.getMyOwnedCar().getCarModelName());
        if (selected_usersList.contains(displayItems.get(position).getMyOwnedCar()))
            holder.childView.setBackgroundColor(ContextCompat.getColor(context, R.color.buttonbackground));

        else
            holder.childView.setBackgroundColor(ContextCompat.getColor(context, R.color.textViewBackground));

        if (displayItems.get(position).getVisible()) {
            Log.d("AIAITRUE", String.valueOf(displayItems.get(position)));
            holder.rLayoutDisable.setVisibility(View.GONE);
            holder.childView.setEnabled(true);
            holder.relativelayout.setEnabled(true);
            holder.childView.setClickable(true);

        } else {
            holder.rLayoutDisable.setVisibility(View.VISIBLE);
            holder.relativelayout.setEnabled(false);
        }

    }
Vikrant Shah
  • 547
  • 1
  • 4
  • 18
0

if you want to disable your click event for a specific row you can add

holder.childView.setClickable(false)

with respect to position, and also try to debug by putting a break point on it, so that, than you clearly came to know, if your controller is coming/executing that statement or not.

  • yeah, but when i debug the code this `holder.childView.setClickable(false)` piece of code is also invoked but still Clickable is working across this row – techDigi Sep 19 '17 at 09:24
0

If you want to disable click event for any view in adapter then you need to set click true in your layout file and then in adapter just check that view is clickable. If clickable then set on click listener.