0

點擊 [here]本教程在ListView項目中設置圖像多個可點擊區域?

嗨, 我是新到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> 
+0

你從哪裏得到的圖像?來自網址或本地。 –

回答

1

使用自定義適配器,然後取出在getview方法的ImageView的ID和應用onclicklistener。
參考custom list

相關問題