Android WebView inside ListView onclick event issues

前端 未结 6 1175
太阳男子
太阳男子 2020-12-25 09:03

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 ta

相关标签:
6条回答
  • 2020-12-25 09:12

    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.

    0 讨论(0)
  • 2020-12-25 09:12

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

    public class NonClickableWebview extends WebView {
        public NonClickableWebview(Context context, AttributeSet attrs) {
            super(context, attrs);
            setClickable(false);
            setLongClickable(false);
            setFocusable(false);
            setFocusableInTouchMode(false);
        }
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-12-25 09:13

    Setup to parent layout of each WebView:

    android:descendantFocusability="blocksDescendants" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    
    0 讨论(0)
  • 2020-12-25 09:24

    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));
    
    0 讨论(0)
  • 2020-12-25 09:25

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

    0 讨论(0)
  • 2020-12-25 09:33

    I was using the following layout in ListView and it was working perfectly, ie its clicking, scrolling and so on.

    TextView1 
    ImageView1 TextView2 ImageView2
    TextView3
    

    Then I have changed the layout with following, ie I have added with WebView control in left most corner in place of ImageView1

    TextView1 
    WebView TextView2 ImageView2
    TextView3
    

    After doing this, clicking was not working for me.

    I solve this problem by adding this to the Webview:

    webView.setFocusable(false);
    webView.setClickable(false);
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题