ListView中的Android WebView onclick事件问题 [英] Android WebView inside ListView onclick event issues

查看:28
本文介绍了ListView中的Android WebView onclick事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView,其中每一行都有两个并排的 webview,占据整行.我已经在我的 ListActivity 中设置了 onListItemClick(),但是当我点击其中一行时它们不会被触发(除非我碰巧点击的地方在 webview 的边界之外 - 但这不太可能是行为,用户最会可能想点击 webview 中的图像).

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).

我试过设置 setFocusable(false) 和 setFocusableInTouchMode(false),但这些都没有帮助.

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

理想情况下,这将与 Facebook 的新闻源完全一样,您可以在其中点击某人墙上帖子的个人资料图片,就像您点击了整行一样(在 FB 的情况下,墙帖的文本)

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:

我不得不使用 OnTouchListener,因为 OnClick 和 OnFocus 不起作用.我扩展了一个可重用的类:

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);
    }
}

可以覆盖 sendClick 方法以执行您的特定情况下所需的操作.用例:

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));

这篇关于ListView中的Android WebView onclick事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆