Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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:

privateclassWebViewClickListenerimplementsView.OnTouchListener {
    privateint position;
    private ViewGroup vg;
    private WebView wv;

    publicWebViewClickListener(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:
                returntrue;
            case MotionEvent.ACTION_UP:
                sendClick();
                returntrue;
        }

        returnfalse;
    }

    publicvoidsendClick() {
        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:

WebViewimage= (WebView) findViewById(R.id.myImage);
image.setOnTouchListener(newWebViewClickListener(image, parent, position));

Solution 2:

I managed to get this working with the following:

class NoClickWebView extends WebView {
    public NoClickWebView(Context context) {
        super(context);
        setClickable(false);
        setLongClickable(false);
        setFocusable(false);
        setFocusableInTouchMode(false);
    }
}

You can use this class or just set these properties on a standard WebView.

Solution 3:

Got it working in Android 4.4 using the same approach as bjdodson above but also overriding dispatchTouchEvent

publicclassNonClickableWebviewextendsWebView {
    publicNonClickableWebview(Context context, AttributeSet attrs) {
        super(context, attrs);
        setClickable(false);
        setLongClickable(false);
        setFocusable(false);
        setFocusableInTouchMode(false);
    }
    @OverridepublicbooleandispatchTouchEvent(MotionEvent ev) {
        returnfalse;
    }
}

Solution 4:

Setup to parent layout of each WebView:

android:descendantFocusability="blocksDescendants"android:layout_width="match_parent"android:layout_height="wrap_content"

Solution 5:

I just tried what is suggested in this post WebView inside the Custom ListView: Click Listener is not Working, I dunno why but if you set these features in the XML they don't seem to work. Doing this dynamically does allow you to click on the listview item =)

Post a Comment for "Android Webview Inside Listview Onclick Event Issues"