Android WebView inside ListView onclick event issues

本文介绍了一种解决Android应用中WebView嵌入ListView时点击事件无法正常触发的问题的方法。通过使用OnTouchListener监听WebView的触摸事件,并自定义实现ListView的点击行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://stackoverflow.com/questions/4973228/android-webview-inside-listview-onclick-event-issues

 

I have a ListView where each row has two webviews side by side, taking up the entire row. I've set up onListItemClick() in my ListActivity, but they are not fired when I tap on one of the rows (unless the place I happen to tap is outside the webview's border - but this isn't likely behavior, users would most likely want to tap on the image inside the webview).

I've tried setting setFocusable(false) and setFocusableInTouchMode(false), but those don't help.

Ideally this would operate exactly like Facebook's newsfeed, where you can tap on the profile picture of someone's wall post and it acts as if you've tapped the entire row (in FBs case the text for the wall post)

 

 

 

 

 

 

 

Figured it out, posting my solution in case someone else wants to do something similar:

I had to use an OnTouchListener, since OnClick and OnFocus weren't working. I extended a class that is reuseable:

private class WebViewClickListener implements View.OnTouchListener { 
    private int position; 
    private ViewGroup vg; 
    private WebView wv; 
 
    public WebViewClickListener(WebView wv, ViewGroup vg, int position) { 
        this.vg = vg; 
        this.position = position; 
        this.wv = wv; 
    } 
 
    public boolean onTouch(View v, MotionEvent event) { 
        int action = event.getAction(); 
 
        switch (action) { 
            case MotionEvent.ACTION_CANCEL: 
                return true; 
            case MotionEvent.ACTION_UP: 
                sendClick(); 
                return true; 
        } 
 
        return false; 
    } 
 
    public void sendClick() { 
        ListView lv = (ListView) vg; 
        lv.performItemClick(wv, position, 0); 
    } 
} 

The sendClick method could be overridden to do what's needed in your specific case. Use case:

WebView image = (WebView) findViewById(R.id.myImage); 
image.setOnTouchListener(new WebViewClickListener(image, parent, position)); 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值