//MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//タイトルバーを削除する
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
MyWebView view= (MyWebView) findViewById(R.id.view);
view.setWebViewClient(new WebViewClient());
//javascriptを許可する
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("http://www.yahoo.co.jp/");
}
}//MyWebView.java
public class MyWebView extends WebView {
private Context mContext;
private GestureDetector mGestureDetector;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mGestureDetector = new GestureDetector(mContext, mSimpleOnGestureListener);
}
public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
mGestureDetector = new GestureDetector(mContext, mSimpleOnGestureListener);
}
public MyWebView(Context context) {
super(context);
mContext = context;
mGestureDetector = new GestureDetector(mContext, mSimpleOnGestureListener);
}
private final GestureDetector.SimpleOnGestureListener mSimpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
// 縦の移動距離が大きすぎる場合は無視
return super.onFling(e1, e2, velocityX, velocityY);
}
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// 開始位置から終了位置の移動距離が指定値より大きい
// X軸の移動速度が指定値より大きい
MoveRightToLeft();
Toast.makeText(mContext, "右から左", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// 終了位置から開始位置の移動距離が指定値より大きい
// X軸の移動速度が指定値より大きい
MoveLeftToRight();
Toast.makeText(mContext, "左から右", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return super.onFling(e1, e2, velocityX, velocityY);
}
};
@Override
public boolean onTouchEvent(MotionEvent event) {
return (mGestureDetector.onTouchEvent(event) | super.onTouchEvent(event));
}
private void MoveLeftToRight() {//左から右へ
goForward();//進む
}
private void MoveRightToLeft() {//右から左
goBack();//戻る
}
}//main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="jp.co.webview.MainActivity">
<jp.co.webview.MyWebView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
※MebViewの継承をした時コンストラクタは
public MyWebView(Context context) {
super(context);
mContext = context;
mGestureDetector = new GestureDetector(mContext, mSimpleOnGestureListener);
}
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mGestureDetector = new GestureDetector(mContext, mSimpleOnGestureListener);
}
public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
mGestureDetector = new GestureDetector(mContext, mSimpleOnGestureListener);
}
の3つオーバーライドしないとコンパイル時にエラーが出ます