0

点击[这里]本教程

嗨,我是 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> 
4

1 回答 1

1

使用自定义适配器,然后在 getview 方法中获取 imageview 的 id 并应用 onclicklistener。
参考自定义列表

于 2013-01-18T13:27:31.810 回答