Android WebView界面点击无反应,WebView点击之后界面异常或者自动滑动置顶

最近遇到一个奇葩问题,RecyclerView 嵌套的 WebView ,点击 WebView 容器后,界面自动滑到顶部,该滑动不是web界面滑动,RecyclerView滑动

刚开始想到的就是焦点问题,滑动冲突,RecyclerView 设置滑动item操作,获取通过接口调用等方式,分别进行调试之后都没发现问题,

其实能发生这种现象的大概率也就是以上问题,经调试,点击其他部分没有问题,也没有后端或者h5端调用相应的方法,只点击web容器才会发生以上现象,所以更加确定是焦点问题,刚开始只在RecyclerView上设置了焦点,无效果,所以饶了老长时间圈子也没有解决,经大佬调试解决结果如下:

通过 android:descendantFocusability=“blocksDescendants” 去获取焦点即可

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center|top">
        <RecyclerView
            android:id="@+id/ShiChangRecyclerView"
            android:layout_width="match_parent"
            android:descendantFocusability="blocksDescendants"
            android:layout_height="match_parent"
            android:scrollbars="none" />
</LinearLayout>

还遇到一个问题: WebView刷新如果会导致原生界面异常可以 尝试一下用 .GoBackOnLoad();方法让网页自己刷新,而不是用 Reload(); 方法同时去刷新Web容器

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Android WebView 实现点击上传图片的功能,需要做以下几个步骤: 1. 在 WebView 启用 JavaScript: ```java webView.getSettings().setJavaScriptEnabled(true); ``` 2. 为 WebView 设置 WebChromeClient: ```java webView.setWebChromeClient(new WebChromeClient() { // For Android 5.0+ @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { if (mFilePathCallback != null) { mFilePathCallback.onReceiveValue(null); } mFilePathCallback = filePathCallback; Intent intent = fileChooserParams.createIntent(); try { startActivityForResult(intent, REQUEST_SELECT_FILE); } catch (ActivityNotFoundException e) { mFilePathCallback = null; Toast.makeText(MainActivity.this, "Cannot open file chooser", Toast.LENGTH_SHORT).show(); return false; } return true; } // For Android < 5.0 public void openFileChooser(ValueCallback<Uri> uploadMsg) { openFileChooser(uploadMsg, null); } // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult(Intent.createChooser(intent, "File Chooser"), FILECHOOSER_RESULTCODE); } }); ``` 这里的 `onShowFileChooser()` 方法是为了支持 Android 5.0 及以上版本的文件上传, `openFileChooser()` 方法是为了支持 Android 3.0 至 Android 4.4 版本的文件上传。在 `onActivityResult()` 方法处理选择的文件并返回给 WebView: ```java @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else if (requestCode == REQUEST_SELECT_FILE) { if (mFilePathCallback == null) return; Uri[] results = null; // Check that the response is a good one if (resultCode == Activity.RESULT_OK) { if (data == null) { // If there is not data, then we may have taken a photo if (mCameraPhotoPath != null) { results = new Uri[]{Uri.parse(mCameraPhotoPath)}; } } else { String dataString = data.getDataString(); ClipData clipData = data.getClipData(); if (clipData != null) { results = new Uri[clipData.getItemCount()]; for (int i = 0; i < clipData.getItemCount(); i++) { ClipData.Item item = clipData.getItemAt(i); results[i] = item.getUri(); } } if (dataString != null) { results = new Uri[]{Uri.parse(dataString)}; } } } mFilePathCallback.onReceiveValue(results); mFilePathCallback = null; } } ``` 3. 在 HTML 添加上传文件的代码: ```html <input type="file" id="fileInput" name="fileInput" multiple> ``` 这里的 `id` 和 `name` 两个属性都必须为 `fileInput`,否则在 WebView 无法触发文件上传事件。同时,为了支持多选文件,可以将 `multiple` 属性设置为 `true`。 4. 在 JavaScript 触发文件上传事件: ```javascript document.getElementById("fileInput").click(); ``` 这里的 `click()` 方法是为了模拟用户点击上传文件按钮。当用户点击上传文件按钮时,WebView 就会弹出文件选择框。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值