要在两个 webview 之间滑动,您需要自定义 webview,因为默认情况下 webview 没有滑动功能,并且在它们两者之间进行更改。下面的代码很好地做到了这一点。版本更改,还添加了顶部和底部滑动功能的代码。
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Context;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends AppCompatActivity {
MyWebView webView1;
MyWebView webView2;
LinearLayout ll1;
LinearLayout ll2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConstraintLayout parentLayout = (ConstraintLayout) findViewById(R.id.parentlayout);
Button b1 = new Button(this);
b1.setText("Mobile");
b1.setWidth(140);
Button b2 = new Button(this);
b2.setText("Web");
b2.setWidth(140);
LinearLayout linearlayout1 = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 140);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
450, 140);
linearlayout1.setLayoutParams(layoutParams);
linearlayout1.setOrientation(LinearLayout.HORIZONTAL);
linearlayout1.setGravity(Gravity.CENTER | Gravity.TOP);
linearlayout1.addView(b1, buttonParams);
linearlayout1.addView(b2, buttonParams);
ll1 = new LinearLayout(this);
ll2 = new LinearLayout(this);
webView1 = new MyWebView(this);
webView2 = new MyWebView(this);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
parentLayout.addView(linearlayout1);
parentLayout.addView(ll1, layoutParams1);
parentLayout.addView(ll2, layoutParams1);
ll1.setVisibility(View.VISIBLE);
ll2.setVisibility(View.INVISIBLE);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams2.setMargins(30, 150, 30, 0);
ll1.addView(webView1, layoutParams2);
ll2.addView(webView2, layoutParams2);
webView1.setInitialScale(1);
webView1.getSettings().setJavaScriptEnabled(true);
webView1.getSettings().setLoadWithOverviewMode(true);
webView1.getSettings().setUseWideViewPort(true);
webView1.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView1.setScrollbarFadingEnabled(false);
webView1.getSettings().setBuiltInZoomControls(true);
webView1.getSettings().setDomStorageEnabled(true);
webView1.setWebViewClient(new WebViewClient());
webView1.loadUrl("http://example.com");
webView2.setInitialScale(1);
webView2.getSettings().setJavaScriptEnabled(true);
webView2.getSettings().setLoadWithOverviewMode(true);
webView2.getSettings().setUseWideViewPort(true);
webView2.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView2.setScrollbarFadingEnabled(false);
webView2.getSettings().setBuiltInZoomControls(true);
webView2.getSettings().setDomStorageEnabled(true);
webView2.setWebViewClient(new WebViewClient());
webView2.loadUrl("https://bestinternetsearch.com");
getSupportActionBar().hide();
// mobile view
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
ll1.setVisibility(View.VISIBLE);
ll2.setVisibility(View.INVISIBLE);
show_toast("button1");
}
});
// web view
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
ll1.setVisibility(View.INVISIBLE);
ll2.setVisibility(View.VISIBLE);
show_toast("button2");
}
});
}
void show_toast(final String text) {
Toast t = Toast.makeText(this, text, Toast.LENGTH_SHORT);
t.show();
}
class MyWebView extends WebView {
Context context;
GestureDetector gd;
MyWebView mywebview;
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
public MyWebView(Context context) {
super(context);
this.context = context;
gd = new GestureDetector(context, sogl);
mywebview = this;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gd.onTouchEvent(event);
return super.onTouchEvent(event);
}
GestureDetector.SimpleOnGestureListener sogl = new SimpleOnGestureListener() {
public boolean onDown(MotionEvent event) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY)) {
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0) {
//mywebview.goForward();
//show_toast("swipe right");
ll1.setVisibility(View.VISIBLE);
ll2.setVisibility(View.INVISIBLE);
} else {
//mywebview.goBack();
//show_toast("swipe left");
ll1.setVisibility(View.INVISIBLE);
ll2.setVisibility(View.VISIBLE);
}
return true;
}
}
else if (Math.abs(distanceY) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceY > 0) {
show_toast("swipe bottom");
//onSwipeBottom();
} else {
show_toast("swipe top");
//onSwipeTop();
}
return true;
}
return false;
}
};
void show_toast(final String text) {
Toast t = Toast.makeText(context, text, Toast.LENGTH_SHORT);
t.show();
}
}
//@Override
//public void onBackPressed() { // navigation bar
//webview.goBack();
//super.onBackPressed();
//}
}