Derbi Senda
Derbi Senda

Reputation: 15

Disable ListView item click and then Enable it

I've got a custom Adapter, because I have two Textviews in a row. First of all I want only the left sides TextViews clickable and disable click event for the right side.

layout_list_xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView  style="@style/TextDesign"
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/left" />

    <TextView  style="@style/TextDesign"
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/right" />
</RelativeLayout>
</LinearLayout>

Custom Adapter:

    public class CustomAdapter extends BaseAdapter {

    Context context;
    List<Occurence> occurenceList;
    TextView left, right;

    public CustomAdapter(Context context, List<Occurence> occurenceList) {
        this.context = context;
        this.occurenceList = occurenceList;
    }

    @Override
    public int getCount() {
        return occurenceList.size();
    }

    @Override
    public Object getItem(int position) {
        return occurenceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=LayoutInflater.from(context);
        View v=inflater.inflate(R.layout.layout_list,parent,false);
        left=(TextView) v.findViewById(R.id.left);
        right=(TextView) v.findViewById(R.id.right);
        left.setText(occurenceList.get(position).singleNumber);
        right.setText(occurenceList.get(position).occurence+" occurence");
        v.setTag(occurencekList.get(position).getId());
        return v;
    }



    @Override
    public boolean areAllItemsEnabled() {
        return super.areAllItemsEnabled();
    }

    @Override
    public boolean isEnabled(int position) {
        return super.isEnabled(position);
    }



}

So when the User clicks on the TextView I want to disable the click for that TextView on the List. The value of the clicked TV will moved to another TextView as a SpannableString where can I perform another action. That means if the User clicks on that SpannableString that will remove that SpannableString and reenable the click action for that ListItem which is disabled.

I attached a picture below to see the result.

image

I've read about that I should put an if statement in the Override method called isEnabled in the adapter class but I need the concrete solution.

Upvotes: 0

Views: 3048

Answers (3)

Derbi Senda
Derbi Senda

Reputation: 15

I solved how can you disable a listItem click and enable it when needed. Add boolean variable to every item in your custom list and make a boolean instance in your adapter class.

public class CustomAdapter extends BaseAdapter {

Context context;
List<Object> objectList;
public boolean clicked=false;

public CustomAdapter(Context context, List<Object> objectList) {
    this.context = context;
    this.objectList = objectList;
}

@Override
public int getCount() {
    return objectList.size();
}

@Override
public Object getItem(int position) {
    return objectList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //your custom code....
    return View;
}


@Override
public boolean areAllItemsEnabled() {
    return super.areAllItemsEnabled();
}


//this is what you need
@Override
public boolean isEnabled(int position) {
    if(objectList.get(position).yourBoolean()==false){
        return false;
    }
    if(objectList.get(position).yourBoolean()==true && clicked==true){
        return true;
    }
    return super.isEnabled(position);
}

In your main_activity.java you invoke your list.onItemClickListener

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {

//Then the isEnabled method will return false for this position and
//this item wont be clickable
objectList.get(position).setYourBooleanValue(false);
                            customAdapter.setclicked(true);

}
                });

If you want to set back this position to clickable just set your boolean back to true and then the isEnabled method will return true for that position.

I hope it will be useful for somebody in the future.

Upvotes: 0

Marcelo Ferracin
Marcelo Ferracin

Reputation: 175

You can set a click listener to your left TextView, do what you have to do, and then remove the listener until needed.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater=LayoutInflater.from(context);
    View v=inflater.inflate(R.layout.layout_list,parent,false);
    left=(TextView) v.findViewById(R.id.left);

    left.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //In here your data treatment

            left.setOnClickListener(null);
        }
    });

    right=(TextView) v.findViewById(R.id.right);
    left.setText(occurenceList.get(position).singleNumber);
    right.setText(occurenceList.get(position).occurence+" occurence");
    v.setTag(occurencekList.get(position).getId());
    return v;
}

Remember that if you using Java 8 you can change the listener to a lambda expression and save some lines.

Upvotes: 0

Grisgram
Grisgram

Reputation: 3243

Set a ClickListener directly on the TextView. As the ClickListener gets the clicked View as parameter when triggered, so you can disable the listener right when it happens.

In your getView method put

left.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         v.setOnClickListener(null); // This disables the click right away
    }
});

Upvotes: 1

Related Questions