OnItemCLickListener not working in listview

2018-12-31 14:34发布

Activity class code:

conversationList = (ListView)findViewById(android.R.id.list);
ConversationArrayAdapter conversationArrayAdapter=new  ConversationArrayAdapter(this, R.layout.conversation_list_item_format_left, conversationDetails);
conversationList.setAdapter(conversationArrayAdapter);
conversationList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Log.d("test","clicked");
    }
});

The getView function in the Adapter class:

if (v == null) {                                
    LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(leftSideMessageNumber.equals(m.getTo())) {
        v = vi.inflate(R.layout.conversation_list_item_format_left, null);
    } else {
        v = vi.inflate(R.layout.conversation_list_item_format_right, null);
    }
}

Is there a problem with using two xmls while inflating?

21条回答
浮光初槿花落
2楼-- · 2018-12-31 14:50

I had the same problem and I just saw I had accidentally set:

@Override
public boolean isEnabled(int position)
{
    return false;
}

on my CustomListViewAdapter class.

By changing this to:

return true;

I've managed to fix the problem. Just in case if someone has done the same mistake...

查看更多
何处买醉
3楼-- · 2018-12-31 14:53
private AdapterView.OnItemClickListener onItemClickListener;

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
 .......

final View view = convertView;
convertView.setOnClickListener(new View.OnClickListener() {
    @Override
     public void onClick(View v) {
         if (onItemClickListener != null) {
             onItemClickListener.onItemClick(null, view, position, -1);
         }
      }
 });

return convertView;
}

public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
}

Then in your activity, use adapter.setOnItemClickListener() before attaching it to the listview.

Copied from github its worked for me

查看更多
唯独是你
4楼-- · 2018-12-31 14:57

Even I was having the same problem, I am having checkbox, did the following to masker itemClickListener work,

Added the following properties to the checkbox,

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

and ItemClickListner started working.

For detailed example you can go through the link,

http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/

Hope it helps Cheers!!

查看更多
君临天下
5楼-- · 2018-12-31 14:58

I just found solution from here.. but by deep clicking...

If any row item of list contains focusable or clickable view then OnItemClickListener won't work.

The row item must have a param like android:descendantFocusability="blocksDescendants".

here you can see example, how your list item should look like. Your list item xml should be... row_item.xml (your_xml_file.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >

// your other widgets here

</LinearLayout>
查看更多
栀子花@的思念
6楼-- · 2018-12-31 14:58

For my lists, my rows have other things that can be clicked, like buttons, so doing a blanket blocksDescendants doesn't work. Instead I add a line in the button's xml:

    android:focusable="false"

That keeps the buttons from blocking the clicks on the rows, but still lets the buttons take the clicks, too.

查看更多
与风俱净
7楼-- · 2018-12-31 14:58

Use android:descendantFocusability

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dip"
    android:background="@color/light_green"
    android:descendantFocusability="blocksDescendants" >

Add above in root layout

查看更多
登录 后发表回答