2

Scenario : I have n number of tabs each tab is having its own listview. I am using a Array adapter to generate a listview. The array which is passed to arrayadaper is having array of objects. Each object has a "menu" and "menu type" . Now based on the menu type, if the menu type is "menugroup" i will draw using a different layout and set the background to RED colour.

Question is simple.. Whenever click is made on menugroup row in listview Nothing should happen. In other words, How to ignore the click made in the menugroup row. This is need to capture only the details of clicks made in menu row at onItemClick method. Could you please point me any idea/clue to proceed ? Thanks in advance.

public class MyCustomAdapter  extends ArrayAdapter<Menu> {
private ArrayList<Menu> menuItemList; 
Context context;
LayoutInflater vi;

public MyCustomAdapter(Context context ,ArrayList<Menu> menu) {    
    super(context, 0, menu );    
    this.menuItemList = new ArrayList<Menu>();    
    this.menuItemList.addAll(menu); 
    this.context =context;
    vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    }    

private class ViewHolder {
    TextView menuItem;   
    TextView menuGroup;  
    }

public int getItemViewType(int position) {
    if (menuItemList.get(position).getMenuType().equals("menugroup")) 
        return 0;
    return 1;
}
public int getViewTypeCount() {
    return 2;
}
public View getView(final int position, View convertView, ViewGroup parent) {      
    ViewHolder holder;   
    int type = getItemViewType(position); 

    if (convertView == null) { 
        holder = new ViewHolder();  
        switch (type) {
        case 0:
            convertView = vi.inflate(R.layout.group,  null); 
            holder.menuItem = (TextView) convertView.findViewById(R.id.tvGroup);
            convertView.setBackgroundColor(Color.RED); 
            break;              
        case 1:
            convertView = vi.inflate(R.layout.item,  null); 
            holder.menuItem = (TextView) convertView.findViewById(R.id.tvItem); 
            break; 
         }  
        convertView.setTag(holder); 
    } else {
        holder = (ViewHolder) convertView.getTag();  
    }    

    switch (type) {
    case 0:         
        holder.menuItem.setText(menuItemList.get(position).getItemName()) ; 
        convertView.setBackgroundColor(Color.RED);  
        break;              
    case 1:
        holder.menuItem.setText(menuItemList.get(position).getItemName()) ;  
        break; 
     }

    return convertView; 
    }

}

user1822729
  • 825
  • 1
  • 13
  • 27

2 Answers2

2

Override isEnabled() in your adapter and return false for any position that should not be considered interactive (docs link). By default, this returns true for all elements.

devunwired
  • 62,780
  • 12
  • 127
  • 139
0

Use your adapter's getItemViewType() from the listener. If this returns 0, then don't do anything more in the listener.

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        if (parent.getAdapter().getItemViewType(position) == 0)
            return;

        // do something for non-menugroup clicks
    }
});
quietmint
  • 13,885
  • 6
  • 48
  • 73