0

我用onItemClickListener创建了自定义列表,只有下半部分是可点击的。该列表使用扩展ArrayAdapter的ImageAdapter。 每行有两个ImageViews和使用以下XML参数自定义列表:上半部分不可点击

android:focusable="false" 
android:focusableInTouchMode="false" 
android:clickable="false" 

我跟着this教程,甚至试图删除的TextView和ImageView的一个TextView。删除它们后,该列表完全无法点击。

这是我的mainActivity

public class ClickTestActivity extends ListActivity 
{ 
String[] listItems={"alpha", "beta", "gamma", "delta", "epsilon"}; 
boolean[] listImages={true, false, true, false, true}; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    setListAdapter(new ImageAdapter(this, R.layout.main, R.id.text1, R.id.image1, listItems, listImages)); 
    getListView().setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {   

      Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show(); 


     } 
    }); 
} 
} 

和我的ImageAdapter

public class ImageAdapter extends ArrayAdapter 
{ 
Activity context; 
String[] items; 
boolean[] arrows; 
int layoutId; 
int textId; 
int imageId; 

ImageAdapter(Activity context, int layoutId, int imageId, String[] items, boolean[] arrows) 
{ 
    super(context, layoutId, items); 

    this.context = context; 
    this.items = items; 
    this.arrows = arrows; 
    this.layoutId = layoutId; 
    this.textId = textId; 
    this.imageId = imageId; 
} 

public View getView(int pos, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater=context.getLayoutInflater(); 
    View row=inflater.inflate(layoutId, null); 
    TextView label=(TextView)row.findViewById(textId); 

    label.setText(items[pos]); 

    if (arrows[pos]) 
    { 
    ImageView icon=(ImageView)row.findViewById(imageId); 
     icon.setImageResource(R.drawable.ic_launcher); 
    }  

    return(row); 
} 
} 

和main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layercontainer" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffff">  
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    <TextView 
     android:id="@+id/text1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:padding="10dp" 
     android:textSize="16sp" 
     android:textColor="#000000"  
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:clickable="false"/> 
    <ImageView 
     android:id="@+id/image1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:clickable="false"/> 
</RelativeLayout> 

回答

0

ListView的布局被充气应该是不同的布局。尝试使用另一个XML为TextView的和列表视图内的ImageView说list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/layercontainer" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#ffffff">  

<TextView 
    android:id="@+id/text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:padding="10dp" 
    android:textSize="16sp" 
    android:textColor="#000000"  
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false"/> 
<ImageView 
    android:id="@+id/image1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false"/> 
</RelativeLayout> 

main.xml中只有列表视图。更改

setListAdapter(new ImageAdapter(this, R.layout.main, R.id.text1, R.id.image1, listItems, listImages)); 

setListAdapter(new ImageAdapter(this, R.layout.list_item, R.id.text1, R.id.image1, listItems, listImages)); 
+0

完美。之前尝试过:/ 非常感谢 – Seb 2012-03-28 08:45:10