实现 OnItemClickListener,但 onItemClick 不起作用



在MainActivity中有onItemClick,并且该活动实现了OnItemClickListener。一切都很简单,这个监听器的结果是当用户触摸ListView的一个项目时,在屏幕上打印一条消息。但是onItemClick函数不起作用,为什么?[logCat无错误]提前谢谢。

这是我的主要活动代码:

package com.prendonota;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.prendonota.activeRecord.Nota;
import com.prendonota.crud.NotaCrud;
public class MainActivity extends Activity implements OnItemClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //*** identifico la ListView  che ho usato nell'xml
        ListView listView = (ListView)findViewById(R.id.listaNote);
        //*** accedo al DB per prendere le note
        NotaCrud crud = new NotaCrud(this);
        List<Nota> listaNote = crud.getListNotes();
        //*** tramite il custom adapter inserisco ogni nota nel relativo item
        ListaNotaAdapter adattatore = new ListaNotaAdapter(this, R.layout.row, listaNote);
        //*** inserisco l'adapter personalizzato appena popolato nella ListView iniziale
        listView.setAdapter(adattatore);
        listView.setOnItemClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void addNote(View view){
        Log.i(this.getClass().toString(), "addNote()");
        Intent intent = new Intent( this, InsertNoteActivity.class );
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Log.i(this.getClass().toString(), "onItemClick: +++ log +++");
        Toast.makeText( this, "Hello world", Toast.LENGTH_SHORT ).show();
    }

}
我activity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/add_nota"
        android:onClick="addNote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/add_nota_label" />
    <ListView
        android:id="@+id/listaNote"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@id/add_nota" >
    </ListView>
</RelativeLayout>

和我的row.xml用于适配器的每个项:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <!--*** PARTE SUPERIORE ***-->
        <RelativeLayout
            android:id="@+id/button_row_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFEC9">
            <ImageButton
                android:id="@+id/button_row_delete"
                android:onClick="crudClick"
                android:layout_width="35dp"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:contentDescription="@string/icon_delete"
                android:src="@android:drawable/ic_menu_delete"/>
            <ImageButton
                android:id="@+id/button_row_edit"
                android:onClick="crudClick"
                android:layout_width="35dp"
                android:layout_height="30dp"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/button_row_delete"
                android:contentDescription="@string/icon_edit"
                android:src="@android:drawable/ic_menu_edit" />
            <TextView
                android:id="@+id/item_data"
                android:layout_width="20dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:textIsSelectable="true"
                android:layout_toLeftOf="@+id/button_row_edit"
                android:padding="10dip"
                android:textSize="12sp" />
        </RelativeLayout>
        <!--*** /PARTE SUPERIORE ***-->

        <!--*** PARTE INFERIORE ***-->
        <RelativeLayout 
            android:id="@+id/oggetto_row_item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FCFBB5">
            <TextView
                android:id="@+id/item_oggetto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dip"
                android:textSize="18sp"
                android:paddingTop="5dp"/>
        </RelativeLayout>
        <!--*** /PARTE INFERIORE ***-->

</LinearLayout>

如果列表视图中的视图是可聚焦的,则OnItemClick方法不会触发

使文本可选(android:textIsSelectable)允许TextView集中,并防止任何(短)点击事件进入OnItemClickListenerOnItemLongClickListener仍然可以工作。

这表示在Android库(如果不是故意的)或Android文档(如果是故意的)中存在一个严重的错误。在任何情况下,只要将android:textIsSelectable更改为false,一切都会很好。

我知道这有点晚了,但是,我有同样的问题,并通过编程设置onFocusable=false来解决,即,在我的自定义ListViewAdapter中,在getView(...)上,我检索ImageButton调用findViewById,然后通过调用setFocusable(false)setFocusableInTouchMode(false)来改变可聚焦值:

public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = LayoutInflater.from(context).inflate(R.layout.alvappliances, parent, false);
    ImageButton delete = (ImageButton)convertView.findViewById(R.id.alvaDeleteIB);
    delete.setFocusable(false);
    delete.setFocusableInTouchMode(false);
    return convertView;
}

在row.xml中添加线性布局标签

android: descendantFocusability = " blocksDescendants "

试试下面的代码,看看是否有帮助

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

相关内容

  • 没有找到相关文章

最新更新