Set particular item unclickable ListView android

687 Views Asked by At

I am having custom adapter which extends ArrayAdapter. I am having different layouts for different items. I like to make items that are from particular type unclickable. Here is my code:

package android.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Switch;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import android.items.ItemProperties;



public class CustomAdapter extends ArrayAdapter<ItemProperties> {

private static final int TYPE_ITEM_AMOUNTS = 0;
private static final int TYPE_ITEM_LIMITS = 1;
private static final int TYPE_MAX_COUNT = 2;


private LayoutInflater mInflater;
private ArrayList<Integer> positions = new ArrayList<Integer>();
private List<ItemProperties> items = new ArrayList<ItemProperties>();

public CustomAdapter(Context context, int resource, List<ItemProperties> itemCards, ArrayList positions) {
    super(context, resource, items);
    this.positions = positions;
    this.items = items;
}

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

@Override
public ItemProperties getItem(int position) {
    return items.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflaterRow = (LayoutInflater) getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = null;
    int type = getItemViewType(position);
    switch (type) {
        case TYPE_ITEM_AMOUNTS:
            rowView = inflaterRow.inflate(R.layout.item_details, parent, false);
            rowView.setEnabled(false);
            rowView.setOnClickListener(null);
            TextView txtName = (TextView) rowView.findViewById(R.id.text_name);
            txtName.setText(itemCards.get(position).getCardLabelDesc());
            TextView txtValue = (TextView) rowView.findViewById(R.id.text_value);
            txtValue.setText(itemCards.get(position).getCardLabelValue());
            break;
        case TYPE_ITEM_LIMITS:
            rowView = inflaterRow.inflate(R.layout.item_limit_settings, parent, false);
            rowView.setEnabled(true);
            TextView txtLimitDesc = (TextView) rowView.findViewById(R.id.text_item_description);
            txtLimitDesc.setText(itemCards.get(position).getCardLabelDesc());
            Switch limitEnable = (Switch) rowView.findViewById(R.id.enable_limit);
            limitEnable.setText("");
            TextView txtLimit = (TextView) rowView.findViewById(R.id.txt_limit);
            txtLimit.setText(itemCards.get(position).getCardLabelValue());
            break;
    }
    return rowView;
}

@Override
public int getViewTypeCount() {
    return TYPE_MAX_COUNT;
}

@Override
public int getItemViewType(int position) {
    if (positions.contains(position))
        return TYPE_ITEM_LIMITS;
    else
        return TYPE_ITEM_AMOUNTS;
     }
  }

However, this does not work for me, nothing is clickable.

In the activity in which I populate the list I have this:

   propertiesAdapter=new    
     CustomAdapter(this,R.layout.item_details,itemPropertiesList,positions);
    listDetails.setAdapter(propertiesAdapter);
    listDetails.setOnItemClickListener(this);

and I have onItemClick event handler.

Does anybody knows what is the problem?

3

There are 3 best solutions below

0
On BEST ANSWER

In the type of TYPE_ITEM_AMOUNTS,you set the view to be disabled,and OnClickListener null,so it's not clickable,I guess this is exactly what you want;

In the type of TYPE_ITEM_LIMITS,you have a Switch here,but you don't set a OnCheckedChangeListener for the switch. When you using a OnItemClickListener for a ListView,remember this:If there is anything that can be clicked in your list item(a button,or in your case, a switch),then the onItemClick() method will never work.This is why nothing is clickable in your listView.

You should set all the views in your list not focusable so that onItemClick() can work normally.Or another way ,do not use OnItemClickListener ,just set a OnCheckedChangeListener for your switch to handle click event.

0
On

Your code listed below does not make sense to me. This would set the whole layout view to be disabled. item_details is your layout with items for the ListView, I believe.

rowView = inflaterRow.inflate(R.layout.item_details, parent, false);
rowView.setEnabled(false);

Suggested code, from getView() case TYPE_ITEM_AMOUNTS:

TextView txtName = (TextView) rowView.findViewById(R.id.text_name);
txtName.setEnabled(false);

Notes:

  • I suspect code rowView = inflaterRow.inflate(R.layout.item_details is the view for the whole layout. Thus rowView is misleading, common name for it is convertView.
  • The code, txtName.setEnabled(false), will disable a certain row position in the ListView. The main code difference is txtName.setEnabled()
  • Remove code rowView.setEnabled(false);
  • I think the answer by Ai Hao is correct too but is less clear and may conflict with code I suggest.
0
On

You need to override two additional methods in your adapter:

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return (position >= 0) && (position < getCount()) && !isSeparator(position);
}

there are some issues that may cause click listener run even on disabled items (for some complex gestures, drag etc.), so add a check to your click listener to avoid unintended reaction on unclickable items.