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:39

If you want to use both the simple click and long click on list view items better way to implement that would be to use context menu for long click. Avoid using setItemLongClickListener especially if you have multiple row layouts for your listview.

查看更多
忆尘夕之涩
3楼-- · 2018-12-31 14:40

if you have textviews, buttons or stg clickable or selectable in your row view only

android:descendantFocusability="blocksDescendants"

is not enough. You have to set

android:textIsSelectable="false"

to your textviews and

android:focusable="false"

to your buttons and other focusable items.

查看更多
闭嘴吧你
4楼-- · 2018-12-31 14:45

I had the same issue, I was using a style for my texts in the row layout that had the "focusable" attribute. It worked after I removed it.

查看更多
泛滥B
5楼-- · 2018-12-31 14:46

Faced same problem, tried for hours. If you have tried all of the above than try changing layout_width of Listview and list item to match_parent from wrap_content.

查看更多
路过你的时光
6楼-- · 2018-12-31 14:47
  1. Add this in main Layout

    android:descendantFocusability="blocksDescendants"
    
  2. Write this code into every button,Textview,ImageView etc which have onClick

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

Hope it will work.

查看更多
荒废的爱情
7楼-- · 2018-12-31 14:49

Android:autoText attribute also make TextView auto focusable.

查看更多
登录 后发表回答