Making a custom Android ListView partially clickable

684 Views Asked by At

I have an Android ListView with a custom item layout:

|              ...              |
+-----------------+-------------+
|  Labels & Info  |  ImageView  |
+-----------------+-------------+
|              ...              |

I want two different actions to occur when either the left side (Labels & Info), or the right side (ImageView) of the list item are clicked. Also, the part of the item which is pressed should show accurate touch feedback (as it would highlight a ListView item by clicking on it).

I know how to implement OnClickListeners for specific views, but what I really want is to split the list item itself into two clickable areas.

How can I achieve this?

EDIT: Here is an image that should describe what I want more accurately:

enter image description here

The red and green areas in the item on the bottom illustrate the two areas that should be clickable and highlighted when clicked.

2

There are 2 best solutions below

1
On

To achieve this thing, you will have to edit the getView() function of your adapter. Add the onClickListeners to respective ImageView and TextView inside it. For example:

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if (rowView == null) {
      LayoutInflater inflater = context.getLayoutInflater();
      rowView = inflater.inflate(R.layout.rowlayout, null);
      ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
      viewHolder.image = (ImageView) rowView
          .findViewById(R.id.ImageView01);
      rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();
    //Add your onClickListeners here
    //holder.text.setOnClickListner(new OnClickListner...
    //holder.iamge.setOnClickListner(new OnClickListner...

    return rowView;
  }
1
On

you can put two TextView in your layout, that match with green area and red area, and set click listener for those,and set text to "". that mean you have 5 different part, 3 part that you want to show to user and 2 part for click listener,i hope you understand my word.