这对我很合适
-
我的自定义手势检测器类。复制并粘贴到适当的包中。
package com.cse.stackoverflow.gesture;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public abstract class CustomGestureDetector extends android.view.GestureDetector.SimpleOnGestureListener {
private static final String TAG = CustomGestureDetector.class.getSimpleName();
private View view;
private boolean selectionStart;
public CustomGestureDetector(View view) {
this.view = view;
}
//FOR GESTURE
@Override
public boolean onFling(MotionEvent motionEventOne, MotionEvent motionEventTwo, float velocityX, float velocityY) {
if (motionEventOne == null || motionEventTwo == null) {
return false;
} else if (motionEventOne.getPointerCount() > 1 || motionEventTwo.getPointerCount() > 1) {
return false;
} else {
if (isSelectionStart()) {
Log.d(TAG, "ME 1 : X - " + motionEventOne.getX());
Log.d(TAG, "ME 1 : Y - " + motionEventOne.getY());
Log.d(TAG, "ME 2 : X - " + motionEventTwo.getX());
Log.d(TAG, "ME 2 : Y - " + motionEventTwo.getY());
Log.d(TAG, "Velocity Of X - " + velocityX);
Log.d(TAG, "Velocity Of Y - " + velocityY);
} else {
try {
///////////////////////////////////////////////////////////////////////////////
Log.d(TAG, "ME 1 : X - " + motionEventOne.getX());
Log.d(TAG, "ME 1 : Y - " + motionEventOne.getY());
Log.d(TAG, "ME 2 : X - " + motionEventTwo.getX());
Log.d(TAG, "ME 2 : Y - " + motionEventTwo.getY());
Log.d(TAG, "Velocity Of X - " + velocityX);
Log.d(TAG, "Velocity Of Y - " + velocityY);
float mRightToLeftCover = motionEventOne.getX() - motionEventTwo.getX();
float mTopToBottomCover = motionEventTwo.getY() - motionEventOne.getY();
float mVelocityX = velocityX;
float mVelocityY = velocityY;
Log.i(TAG, "mRightToLeftCover : " + mRightToLeftCover);
Log.i(TAG, "mTopToBottomCover : " + mTopToBottomCover);
Log.i(TAG, "mVelocityX : " + mVelocityX);
Log.i(TAG, "mVelocityY : " + mVelocityY);
if (mRightToLeftCover >= 0) {
if (mTopToBottomCover >= 0) {
if (mTopToBottomCover < 100) {
if (mRightToLeftCover > 100) {
Log.d(TAG, "1. R =>> L");
onRightToLeftSwap();
}
} else {
if (mRightToLeftCover < 100) {
Log.d(TAG, "9. T ==>> B");
onTopToBottomSwap();
} else {
Log.d(TAG, "2. T ==>> B, R =>> L");
}
}
} else {
if (mTopToBottomCover > -100) {
if (mRightToLeftCover > 100) {
Log.d(TAG, "3. R =>> L");
onRightToLeftSwap();
}
} else {
if (mRightToLeftCover < 100) {
Log.d(TAG, "10. B ==>> T");
onBottomToTopSwap();
} else {
Log.d(TAG, "4. B ==>> T, R =>> L");
}
}
}
} else if (mRightToLeftCover < 0) {
if (mTopToBottomCover >= 0) {
if (mTopToBottomCover < 100) {
if (mRightToLeftCover > -100) {
Log.d(TAG, "5. L =>> R");
onLeftToRightSwap();
}
} else {
if (mRightToLeftCover > -100) {
Log.d(TAG, "11. T ==>> B");
onTopToBottomSwap();
} else {
Log.d(TAG, "6. T ==>> B, L =>> R");
}
}
} else {
if (mTopToBottomCover > -100) {
if (mRightToLeftCover < -100) {
Log.d(TAG, "7. L =>> R");
onLeftToRightSwap();
}
} else {
if (mRightToLeftCover < -100) {
Log.d(TAG, "12. B ==>> T");
onBottomToTopSwap();
} else {
Log.d(TAG, "8. B ==>> T, L =>> R");
}
}
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
//////////////////////////////////////////////////////////////////////////////
return false;
}
}
//EXPERIMENTAL PURPOSE
public abstract void onLeftToRightSwap();
public abstract void onRightToLeftSwap();
public abstract void onTopToBottomSwap();
public abstract void onBottomToTopSwap();
public abstract void onLeftToRightTopToBottomDiagonalSwap();
public abstract void onLeftToRightBottomToTopDiagonalSwap();
public abstract void onRightToLeftTopToBottomDiagonalSwap();
public abstract void onRightToLeftBottomToTopDiagonalSwap();
//SINGLE AND DOUBLE TABS
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d(TAG, "On Single Tap");
Log.d(TAG, "Selection Start : " + selectionStart);
Log.d(TAG, "ME 1 : X - " + e.getX());
Log.d(TAG, "ME 1 : Y - " + e.getY());
onSingleTap();
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG, "On Double Tap");
onDoubleTap();
return super.onDoubleTap(e);
}
public abstract void onSingleTap();
public abstract void onDoubleTap();
public boolean isSelectionStart() {
return selectionStart;
}
public void setSelectionStart(boolean selectionStart) {
this.selectionStart = selectionStart;
}
@Override
public void onLongPress(MotionEvent e) {
onLongPressPerformed(e);
super.onLongPress(e);
}
public abstract void onLongPressPerformed(MotionEvent e);
}
-
活动_main。xml只是一个小修改,即我将id“webView”设置为您的webView。(将此xml代码复制并粘贴到xml文件中)。
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="@+id/rL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C6FF00">
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8">
<EditText
android:id="@+id/fromDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="From Date" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="@+id/linear"
android:layout_toRightOf="@+id/linear"
android:background="#defec8"
android:orientation="horizontal">
<EditText
android:id="@+id/todate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="To Date" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="@+id/linear1"
android:layout_toRightOf="@+id/linear1"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="@+id/timespinner"
android:layout_width="80dp"
android:layout_height="42dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linear"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="40dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/linear4"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="@+id/nametype"
android:layout_width="80dp"
android:layout_height="40dp">
</Spinner>
</LinearLayout>
<LinearLayout
android:id="@+id/linear3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linear4"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="@id/linear5"
android:layout_toRightOf="@id/linear5"
android:background="#defec8"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<Spinner
android:id="@+id/digitspinner"
android:layout_width="80dp"
android:layout_height="40dp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/linearweb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/rL"
android:layout_alignParentBottom="true">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
</RelativeLayout>
-
MainActivity代码(复制并粘贴所有方法,并在onCreate()中调用initialiseView()方法)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialiseView();
}
RelativeLayout upperLayout;
LinearLayout lowerLayout;
WebView mWebView;
private void initialiseView() {
upperLayout = (RelativeLayout) findViewById(R.id.rL);
lowerLayout = (LinearLayout) findViewById(R.id.linearweb);
mWebView = (WebView) findViewById(R.id.webView);
CustomGestureDetector mCustomGestureDetectorForUpperLayout = new CustomGestureDetector(upperLayout) {
@Override
public void onLeftToRightSwap() {
}
@Override
public void onRightToLeftSwap() {
}
@Override
public void onTopToBottomSwap() {
//Toast.makeText(MainActivity.this, "onTopToBottomSwap", Toast.LENGTH_SHORT).show();
showUpperLayout();
}
@Override
public void onBottomToTopSwap() {
//Toast.makeText(MainActivity.this, "onBottomToTopSwap", Toast.LENGTH_SHORT).show();
hideUpperLayout();
}
@Override
public void onLeftToRightTopToBottomDiagonalSwap() {
}
@Override
public void onLeftToRightBottomToTopDiagonalSwap() {
}
@Override
public void onRightToLeftTopToBottomDiagonalSwap() {
}
@Override
public void onRightToLeftBottomToTopDiagonalSwap() {
}
@Override
public void onSingleTap() {
}
@Override
public void onDoubleTap() {
}
@Override
public void onLongPressPerformed(MotionEvent e) {
}
};
final GestureDetector mGestureDetectorUpperLayout = new GestureDetector(this, mCustomGestureDetectorForUpperLayout);
CustomGestureDetector mCustomGestureDetectorForLowerLayout = new CustomGestureDetector(lowerLayout) {
@Override
public void onLeftToRightSwap() {
}
@Override
public void onRightToLeftSwap() {
}
@Override
public void onTopToBottomSwap() {
showUpperLayout();
}
@Override
public void onBottomToTopSwap() {
}
@Override
public void onLeftToRightTopToBottomDiagonalSwap() {
}
@Override
public void onLeftToRightBottomToTopDiagonalSwap() {
}
@Override
public void onRightToLeftTopToBottomDiagonalSwap() {
}
@Override
public void onRightToLeftBottomToTopDiagonalSwap() {
}
@Override
public void onSingleTap() {
}
@Override
public void onDoubleTap() {
}
@Override
public void onLongPressPerformed(MotionEvent e) {
}
};
final GestureDetector mGestureDetectorLowerLayout = new GestureDetector(this, mCustomGestureDetectorForLowerLayout);
upperLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mGestureDetectorUpperLayout.onTouchEvent(motionEvent);
return true;
}
});
lowerLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mGestureDetectorLowerLayout.onTouchEvent(motionEvent);
return true;
}
});
mWebView.loadUrl("https://www.google.co.in/");
CustomGestureDetector mCustomGestureDetectorForWebView = new CustomGestureDetector(mWebView) {
@Override
public void onLeftToRightSwap() {
}
@Override
public void onRightToLeftSwap() {
}
@Override
public void onTopToBottomSwap() {
showUpperLayout();
}
@Override
public void onBottomToTopSwap() {
hideUpperLayout();
}
@Override
public void onLeftToRightTopToBottomDiagonalSwap() {
}
@Override
public void onLeftToRightBottomToTopDiagonalSwap() {
}
@Override
public void onRightToLeftTopToBottomDiagonalSwap() {
}
@Override
public void onRightToLeftBottomToTopDiagonalSwap() {
}
@Override
public void onSingleTap() {
}
@Override
public void onDoubleTap() {
}
@Override
public void onLongPressPerformed(MotionEvent e) {
}
};
final GestureDetector mGestureDetectorForWebView = new GestureDetector(this, mCustomGestureDetectorForWebView);
mWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mGestureDetectorForWebView.onTouchEvent(motionEvent);
return true;
}
});
}
public void hideUpperLayout() {
upperLayout.setVisibility(View.GONE);
}
public void showUpperLayout() {
upperLayout.setVisibility(View.VISIBLE);
}
public void toggleUpperLayout() {
if (upperLayout.getVisibility() == View.VISIBLE) {
hideUpperLayout();
} else {
showUpperLayout();
}
}
}
-
这是可选的(在您的网络视图上查看谷歌主页)。
<uses-permission android:name="android.permission.INTERNET" />
根据注释更新以实现平滑滚动
-
首先是你的风格。xml创建如下主题条目,或者只是复制并粘贴它。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
-
因为色彩需要创造色彩。xml(如果您创建了应用程序,则其具有默认条目)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
-
现在,您的activity_main。xml如下所示。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.cse.scrolltoolbar.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:id="@+id/rL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#C6FF00"
android:paddingTop="100dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay">
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8">
<EditText
android:id="@+id/fromDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="From Date" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="@+id/linear"
android:layout_toRightOf="@+id/linear"
android:background="#defec8"
android:orientation="horizontal">
<EditText
android:id="@+id/todate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="To Date" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="@+id/linear1"
android:layout_toRightOf="@+id/linear1"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="@+id/timespinner"
android:layout_width="80dp"
android:layout_height="42dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linear"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="40dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/linear4"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="@+id/nametype"
android:layout_width="80dp"
android:layout_height="40dp">
</Spinner>
</LinearLayout>
<LinearLayout
android:id="@+id/linear3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linear4"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:layout_toEndOf="@id/linear5"
android:layout_toRightOf="@id/linear5"
android:background="#defec8"
android:orientation="horizontal">
<Spinner
android:id="@+id/digitspinner"
android:layout_width="80dp"
android:layout_height="40dp" />
</LinearLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!--<include layout="@layout/content_scrolling" />-->
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</WebView>
</android.support.design.widget.CoordinatorLayout>
-
在清单文件中添加internet权限,并添加以下方法,将google主页加载到您的web视图中,并在setContentView之后从onCreate调用此方法。
private void initialiseView() {
WebView mWebView = (WebView) findViewById(R.id.webView);
mWebView.loadUrl("https://www.google.co.in/");
}