OnItemClick未在ListView中调用

时间:2014-11-11 12:50:08

标签: android listview onitemclicklistener

我已经阅读了尽可能多的示例,我似乎正在实现这一点,但点击永远不会注册。我的OnItemClicked Listener没有被调用,虽然它似乎正确实现。

public class Playlist extends ListActivity implements OnItemClickListener{
    private int list_layout = R.layout.playlist;
    private Context context;
    private PlaylistAdapter adapter;
    private Cursor cursor = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(list_layout);
        context = this.getApplicationContext();
        adapter = new PlaylistAdapter(context, cursor, FLAGS);
        ListView list_view = getListView();
        list_view.setOnItemClickListener(this);

        ...
    }

    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
        Toast.makeText(Playlist.this, "click", Toast.LENGTH_LONG).show();
        startSong(position);
    }
}

的ListView:

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff525252"
        android:layout_weight="1"

        />

</LinearLayout>

ListView行:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="50sp"
    android:background="#ffafafaf"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:id="@+id/songname"
        android:autoText="false"
        android:textColor="#ff000000"
        android:layout_alignParentTop="true"
        android:maxLines="1"
        android:padding="1sp"
        android:textIsSelectable="false"
        android:clickable="false"/>

   <Button>
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:text="="
        android:id="@+id/options"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/artistname"
        android:textColor="#ff585858"
        android:textSize="12sp"
        android:layout_below="@+id/songname"
        android:padding="1sp"
        android:minLines="1"
        android:textIsSelectable="false"
        android:clickable="false"/>

</RelativeLayout>

编辑:抱歉。我并不是说这是一个如此糟糕的问题。我会尽力挽救它。

4 个答案:

答案 0 :(得分:1)

将完整的XML代码添加到您的问题中(如果您使用RelativeLayoutFrameLayout

并检查android:id="@+id/list"

 listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            startSong(arg2);

        }
    });

或尝试使用getView参数查看/充气列表项onclick listener

     view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {   
                 //do stuff
            }
        });

答案 1 :(得分:0)

lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                    try {

                         //  YOUR STUFF


                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });

答案 2 :(得分:0)

Listview上的Xml文件中的问题

<ListView
     android:id="@+id/list"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:background="#ff525252"
     android:clickable="true" >
</ListView>

答案 3 :(得分:0)

此类型存在许多重复问题,但与我的问题相关的问题已解决HERE

“如果列表的任何行项目包含可聚焦或可点击的视图,则OnItemClickListener将无效。”

行项必须有一个像android:descendantFocusability =“blocksDescendants”的参数。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="50sp"
    android:background="#ffafafaf"
    android:descendantFocusability="blocksDescendants"
    >