Set Images Multiple Clickable Zones in ListView Items?

925 Views Asked by At

Click [here] This tutorial

Hi, I am new to android, I found this tutorial for Making list with multiple clickable zones. In a ListView that have multiple interactive locations that the user can touch, instead of just one single clickable row.

I require this kind of list in my project but I am unable to set different images on each row of list.

Please help Me .

public class MyActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView list = new ListView(this);
        setContentView(list);

        String[] items = {"Tom", "Sally", "Bill", "John", "Santiago", "Isabella"};
        //Supply this adapter with either R.layout.row_button, R.layout.row_view, or R.layout.row_view_noparent
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row_view, R.id.text, items) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row =  super.getView(position, convertView, parent);

                View left = row.findViewById(R.id.left);
                left.setTag(position);
                left.setOnClickListener(MyActivity.this);
                View right = row.findViewById(R.id.right);
                right.setTag(position);
                right.setOnClickListener(MyActivity.this);

                return row;
            }
        };

        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.left:
            Toast.makeText(this, "Left Accessory "+v.getTag(), Toast.LENGTH_SHORT).show();
            break;
        case R.id.right:
            Toast.makeText(this, "Right Accessory "+v.getTag(), Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        Toast.makeText(this, "Item Click "+position, Toast.LENGTH_SHORT).show();
    }
}
 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal" >    
    <ImageView
        android:id="@+id/left"
        android:layout_width="?android:attr/listPreferredItemHeight"
        android:layout_height="fill_parent"
        android:background="@drawable/mybutton"
        android:clickable="true"
        android:scaleType="center"
        android:src="@drawable/icon" />    
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <ImageView
        android:id="@+id/right"
        android:layout_width="?android:attr/listPreferredItemHeight"
        android:layout_height="fill_parent"
        android:background="@drawable/mybutton"
        android:clickable="true"
        android:scaleType="center"
        android:src="@drawable/icon" /></LinearLayout> 
1

There are 1 best solutions below

0
On

Use custom adapter and then fetch the id of imageview in getview method and apply onclicklistener.
Refer for custom list