在ListView项目中设置图像多个可点击区域?

时间:2013-01-18 13:18:35

标签: android android-listview android-arrayadapter clickable-image

点击 [here]本教程

您好, 我是android新手, 我找到了包含多个可点击区域的制作列表的教程。在具有用户可以触摸的多个交互位置的ListView中,而不是仅一个可单击的行。

我在项目中需要这种列表,但我无法在列表的每一行上设置不同的图像。

请帮助我。

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 个答案:

答案 0 :(得分:1)

使用自定义适配器,然后在getview方法中获取imageview的id并应用onclicklistener。
请参阅custom list