gpt4 book ai didi

android - 如何在 android 布局中使用向上/向下滑动方法?

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:49 26 4
gpt4 key购买 nike

enter image description here

我正在使用两个相对布局。一个相对布局由编辑框和微调器(Lime 颜色布局)组成。另一个相对布局仅由 Web View 组成。我希望使用向上/向下滑动的方法来进行相对布局(青柠色)。如果用户向上滑动石灰色布局。其他布局将全屏显示。我不知道如何实现。我需要任何引用资料或文章才能完成。

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<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_marginBottom="10dp">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent">

</WebView>

</LinearLayout>

</RelativeLayout>

最佳答案

这非常适合我

  1. My custom Gesture Detector Class. Copy and paste in appropriate package.
  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);
}
  1. activity_main.xml just small modification i.e. I set id "webView" to your webView. (Copy and paste this xml code into your xml file).
<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>
  1. MainActivity code (Copy and paste all methods and call initialiseView() method in onCreate())
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();
}
}
}
  1. This is optional(to see google home page on your webview). //Add internet permission in AndroidMenifest.xml
<uses-permission android:name="android.permission.INTERNET" />

根据平滑滚动的评论更新

To Achieve Smooth Scrolling you need need to use AppBarLayout inside parent layout(Parent layout may be anything for easy use Coordinator layout).

  1. First in your style.xml create themes entry like below or just copy and paste it.
<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>
  1. For colours need to create colors.xml (If you created app its have default entries)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
  1. Now your activity_main.xml is like below.
<?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>
  1. Add internet permission in Manifest file and add below method to load google home page to your web view and call this method from onCreate after setContentView.
    private void initialiseView() {

WebView mWebView = (WebView) findViewById(R.id.webView);

mWebView.loadUrl("https://www.google.co.in/");


}

关于android - 如何在 android 布局中使用向上/向下滑动方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46402557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com