我创建了一个WebView,在我的WebView中我有一个按钮,它在单击时更改了WebView中的页面。我还在WebView上设置了一个触摸监听器。但在设置触摸侦听器后,按钮仅响应QUICK双击。我的意思是你应该点击两次,点击之间没有连续的间隔。这是我的代码。任何帮助将受到高度赞赏。
@SuppressLint("SetJavaScriptEnabled")
public class DashboardFragment extends Fragment implements SolutionHandler {
private static final String SAVE_DASHBOARD_PAGE = "saveDashboardPage";
private static final String SAVE_DASHBOARD_WEB_STATE = "saveDashboardWebState";
private static final String DASHBOARD_URL = "https://api.myApp.com/api/dashboard/";
private static final String LOGOUT_URL = "myapp://logout/";
private static final String SOLUTION_URL = "mytapp://solution/open?solution_id";
private static final String MY_APP = "myapp://";
private View dashboardView;
private WebView dashboardWebView;
private String currentUrl;
private User user;
private LoadingDialog loadingDialog;
private Bundle webState;
private int supportUrlAcessCount = 0;
public DashboardFragment() {
setRetainInstance(true);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
dashboardView = inflater.inflate(R.layout.fragment_dashboard, container, false);
user = User.sharedUser(getActivity());
if (savedInstanceState != null) {
currentUrl = savedInstanceState.getString(SAVE_DASHBOARD_PAGE);
webState = savedInstanceState.getBundle(SAVE_DASHBOARD_WEB_STATE);
}
if (currentUrl == null || currentUrl.length() == 0) {
currentUrl = DASHBOARD_URL + user.hash();
}
loadingDialog = new LoadingDialog(getActivity(), R.string.loading_dashboard, R.string.icon_arrows_cw, true);
WebChromeClient webClient = new WebChromeClient();
if (dashboardWebView == null) {
dashboardWebView = new WebView(getActivity());
dashboardWebView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
dashboardWebView.setVerticalScrollBarEnabled(true);
dashboardWebView.setHorizontalScrollBarEnabled(true);
dashboardWebView.requestFocusFromTouch();
dashboardWebView.getSettings().setAppCachePath(getActivity().getCacheDir().getAbsolutePath());
dashboardWebView.getSettings().setAppCacheEnabled(true);
dashboardWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
// dashboardWebView.setOnTouchListener(webViewTouchListener);
dashboardWebView.setWebChromeClient(webClient);
dashboardWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if (url.startsWith(DASHBOARD_URL)) {
view.clearHistory();
loadingDialog.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith(OHITAPP)) {
if (url.startsWith(SOLUTION_URL)) {
loadingDialog.setStringResource(R.string.loading_solution);
loadingDialog.show();
Locker.lock(getActivity());
Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher(url);
String solutionId = "";
if (matcher.find()) {
solutionId = matcher.group();
}
APITalker.sharedTalker().getSolution(user.hash(), Integer.valueOf(solutionId), false,
APITalker.CALL_TYPES.CALL_TYPE_DEEP_LINK, DashboardFragment.this);
} else if (url.startsWith(LOGOUT_URL)) {
loadingDialog.dismiss();
ErrorHelper.showError(TalkersConstants.HASH_INVALID, (SuperActivity) getActivity());
}
return true;
} else if (url.startsWith("http://support")) {
supportUrlAcessCount++;
if (supportUrlAcessCount == 2) {
((MenuActivity) getActivity()).changeFragment(6, false, false, false, null, null);
return true;
}
return false;
} else {
return false;
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
showNoInternetError();
}
});
if (webState == null) {
if (CheckConnectionHelper.isNetworkAvailable(getActivity())) {
loadingDialog.show();
dashboardWebView.loadUrl(currentUrl);
} else {
ErrorHelper.showError(TalkersConstants.JUST_FAILURE, (SuperActivity) getActivity());
dashboardWebView.loadUrl("about:blank");
}
dashboardWebView.getSettings().setSupportZoom(true);
dashboardWebView.getSettings().setBuiltInZoomControls(true);
dashboardWebView.getSettings().setDisplayZoomControls(false);
dashboardWebView.getSettings().setLoadWithOverviewMode(true);
dashboardWebView.getSettings().setUseWideViewPort(true);
dashboardWebView.getSettings().setJavaScriptEnabled(true);
} else {
dashboardWebView.restoreState(webState);
}
} else {
if (webState == null) {
loadingDialog.show();
dashboardWebView.loadUrl(currentUrl);
dashboardWebView.getSettings().setSupportZoom(true);
dashboardWebView.getSettings().setBuiltInZoomControls(true);
dashboardWebView.getSettings().setDisplayZoomControls(false);
dashboardWebView.getSettings().setLoadWithOverviewMode(true);
dashboardWebView.getSettings().setUseWideViewPort(true);
dashboardWebView.getSettings().setJavaScriptEnabled(true);
} else {
dashboardWebView.restoreState(webState);
}
}
LinearLayout dashboardContainer = (LinearLayout) dashboardView.findViewById(R.id.dashboard_container);
dashboardContainer.addView(dashboardWebView);
getActivity().getWindow().getDecorView().getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
dashboardView.requestLayout();
}
});
// SuperActivity.savedLastClickTime = System.currentTimeMillis();
return dashboardView;
}
@Override
public void onDestroyView() {
LinearLayout dashboardContainer = (LinearLayout) dashboardView.findViewById(R.id.dashboard_container);
dashboardContainer.removeView(dashboardWebView);
super.onDestroyView();
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(SAVE_DASHBOARD_PAGE, getCurrentPage());
outState.putBundle(SAVE_DASHBOARD_WEB_STATE, getCurrentWebViewState());
super.onSaveInstanceState(outState);
}
public String getCurrentPage() {
return dashboardWebView.getUrl();
}
public Bundle getCurrentWebViewState() {
Bundle outState = new Bundle();
dashboardWebView.saveState(outState);
return outState;
}
@Override
public void solutionSuccess(int solutionId, boolean isVoice, String title, String html, String speech) {
loadingDialog.dismiss();
Solution solution = new Solution(solutionId, title);
ArrayList<Solution> solutions = new ArrayList<>();
solutions.add(solution);
ActivityOptions options = ActivityOptions.makeCustomAnimation(getActivity(), R.anim.flip_right_in, R.anim.flip_right_out);
Intent solutionIntent = new Intent(getActivity(), SolutionActivity.class);
solutionIntent.putExtra(SolutionActivity.ACCESS_METHOD_EXTRA, APITalker.ACCESS_METHODS.ACCESS_METHOD_DEEPLINKSOLUTION);
solutionIntent.putExtra(SolutionActivity.HTML_EXTRA, html);
solutionIntent.putExtra(SolutionActivity.SPEECH_EXTRA, speech);
solutionIntent.putParcelableArrayListExtra(SolutionActivity.SOLUTIONS_EXTRA, solutions);
solutionIntent.putExtra(SolutionActivity.POSITION_EXTRA, 0);
startActivityForResult(solutionIntent, SolutionActivity.REQUEST_CODE, options.toBundle());
Locker.unlock(getActivity());
}
@Override
public void solutionFailure(String error) {
loadingDialog.dismiss();
if (error.equals(TalkersConstants.HASH_INVALID)) {
((MenuActivity) getActivity()).logout(null);
}
ErrorHelper.showError(error, (SuperActivity) getActivity());
Locker.unlock(getActivity());
}
public boolean navigatesBack() {
if (dashboardWebView != null && dashboardWebView.canGoBack()) {
dashboardWebView.goBack();
return true;
}
return false;
}
public void showNoInternetError() {
loadingDialog.dismiss();
ErrorHelper.showError(TalkersConstants.JUST_FAILURE, (SuperActivity) getActivity());
dashboardWebView.loadUrl("about:blank");
}
public void reloadWebView() {
if (dashboardWebView.getUrl() == null
|| dashboardWebView.getUrl().isEmpty()
|| dashboardWebView.getUrl().equals("about:blank")) {
dashboardWebView.loadUrl(currentUrl);
}
}
OnTouchListener webViewTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (System.currentTimeMillis() - SuperActivity.savedLastClickTime < 2000) {
Log.d("I AM IN A FALSE", "FALSE");
return false;
}
SuperActivity.savedLastClickTime = System.currentTimeMillis();
Log.d("I AM IN A TOUCH LISTENER", "TOUCH LISTENER");
view.performClick();
return true;
}
};
}
这是DashboardFragment代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dashboard_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
编辑:我还编写了日志来确定onTouch监听器的行为。发布已编辑的代码和日志
@Override
public boolean onTouch(View view, MotionEvent event) {
if (System.currentTimeMillis() - SuperActivity.savedLastClickTime < 2000) {
Log.d("I AM IN A FALSE", "FALSE");
return false;
}
SuperActivity.savedLastClickTime = System.currentTimeMillis();
Log.d("I AM IN A TOUCH LISTENER", "TOUCH LISTENER");
view.performClick();
return true;
}
};
和日志
11-26 15:01:21.747: D/I AM IN A TOUCH LISTENER(16231): TOUCH LISTENER
11-26 15:01:21.817: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:21.837: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:36.747: D/I AM IN A TOUCH LISTENER(16231): TOUCH LISTENER
11-26 15:01:36.837: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:36.847: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:39.387: E/NMSP(16231): [bz] Session Idle for too long, longer than [50] (initiated from XMode.parseBcp)
11-26 15:01:39.597: E/NMSP(16231): [bz] XMode.parseXModeMsg() unknown protocol: 0
11-26 15:01:39.597: I/SpeechKit(16231): Disconnected
11-26 15:01:42.367: D/I AM IN A TOUCH LISTENER(16231): TOUCH LISTENER
11-26 15:01:42.407: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:42.457: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:42.537: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:49.407: D/I AM IN A TOUCH LISTENER(16231): TOUCH LISTENER
11-26 15:01:49.457: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:49.527: D/I AM IN A FALSE(16231): FALSE
11-26 15:01:49.597: D/I AM IN A FALSE(16231): FALSE
因此,在每个点击应用程序上输入触摸侦听器3次:第一次只是触摸侦听器,并在“假”部分中进行2,3次。但为什么呢?