使用自定义ArrayAdapter扩展ListView - onItemClick不起作用?

时间:2014-01-17 19:28:59

标签: android listview layout-inflater onitemclicklistener

我现在几乎尝试了所有的东西2天,但无法让它发挥作用。

从服务(也尝试过的活动)我给listview增加了一个oncreate:

      view = inflater.inflate(R.layout.my_listview, null);

      CreateItems adapter = new CreateItems(this,R.layout.my_listview_item, rowItems);
      ListView listView = (ListView)view.findViewById(R.id.my_listview);

      listView.setAdapter(adapter); 

      listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int postion, long id) {              
              Log.i("", "click list");            
           } 
          });    

      mWindowManager.addView(view, mParams); 



我也试着像这样开始听:

listView.setOnItemClickListener(this);



来自ArrayAdapter的getView()

    public class CreateItems extends ArrayAdapter<ListItem> {

    ...

    public View getView(int position, View convertView, ViewGroup parent) {

    ListItem rowItem = getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if(convertView == null) {
        convertView = mInflater.inflate(R.layout.my_listview_item, parent, false);
    }

    final View item = convertView;

    ImageView icon = (ImageView) item.findViewById(R.id.icon);
    TextView title = (TextView) item.findViewById(R.id.title);

    icon.setImageDrawable(rowItem.getImageId());
    title.setText(appLabel.get(position).toString()); 

    return convertView;



my_lsitview_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:minHeight="75dp"
android:clickable="false"   
android:focusable="false"
android:focusableInTouchMode="false">

<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="6dip"
android:layout_centerHorizontal="true"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false"
/>

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:layout_marginTop="53dip"
android:layout_marginBottom="6dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_centerHorizontal="true"
android:ellipsize="marquee"
android:layout_weight="1"
android:singleLine="true"
android:fadingEdge="horizontal"
android:focusableInTouchMode="false" 
android:clickable="false"
android:focusable="false"
android:textIsSelectable="false"
/>



listview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".listview" 
android:descendantFocusability="blocksDescendants"   
>



列表视图显示正确。我可以移动它,并且on_pressed @drawable突出显示在列表项触摸上。但是onItemClick没有开始。

当我在getView()适配器中设置onClick侦听器时,我可以获得点击次数,但on_pressed突出显示不再有效。我猜是因为在它到达列表视图之前我抓住了clickevent。

        icon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
         ....



如何才能使其正常工作?

0 个答案:

没有答案