带有OnItemClickListener android的ListView

我ListView与RatingBar和使用自定义ImageButton。这是我的问题:单击我的时ListView,我OnItemClickListener无法正常工作。请任何人能帮助我。码:


ListView lv = getListView();

setContentView(lv);

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override

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

        Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();

    }

});

提前致谢!


12345678_0001
浏览 451回答 3
3回答

皈依舞

尽管这是一个非常古老的问题,但我仍在发布答案,以便对您有所帮助。如果要在列表视图中使用任何布局,请使用...android:descendantFocusability="blocksDescendants"&nbsp; &nbsp;&nbsp;...在列表内的第一个父布局上。这就像魔术一样,单击将不会被列表中的任何元素占用,而是将直接转到列表项。

慕妹3242003

嘿检查一下,对我有用...希望它对你也有用如果列表项包含 ImageButton问题:OnItemClickListener根本什么都没有回应!原因:不知道解决方案:在代码中,将ImageButton的焦点设置为“false”1: ImageButton button = (ImageButton) convertView.findViewById(R.id.imageButton);2: button.setFocusable(false);

牧羊人nacy

我有一个扩展ListActivity的活动。我尝试在onCreate中执行以下操作:ListView listView = getListView();listView.setOnItemClickListener(new OnItemClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; Log.i("Hello!", "Y u no see me?");&nbsp; &nbsp; }});但这没有用。相反,我只需要重写onListItemClick:@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {&nbsp; &nbsp; Log.i("Hello!", "Clicked! YAY!");}
打开App,查看更多内容
随时随地看视频慕课网APP