- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 Scrollview 中嵌入了每一个看起来像卡片类型的布局。当我滚动时,如何像 Spring 式移动卡片然后回到原来的位置。
就像卡片在弹跳。在我的布局中,最上面是ScrollView,下面集成了5到6个布局。
当我滚动时如何将那些添加的布局向上移动一点并到达它的位置?你可以在这里看到一个例子。看看单独滚动的地方卡片从它的位置移动。看看 screenshot注意上下滚动时卡片像 Spring 类型一样移动的滚动,即滚动时卡片之间的间距会减小。如何实现?
在目前应用的CustomScrollView中,它有overscroll但我无法实现 Spring 式可移动卡片。
这是我应用的代码。
<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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LaunchActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mobile.ui.MyImageView
android:id="@+id/backgroundImage_timeline"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_dot_line"
android:scaleType="centerCrop" />
<com.mobile.ui.view.ObservableScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LaunchActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/commonModules"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/anotherModule"
android:orientation="vertical">
<com.mobile.ui.FirstModule //this is one layout
android:id="@+id/myFirstModule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
<---second layout-----> and so on..
</RelativeLayout>
</com.mobile.ui.view.ObservableScrollView>
</RelativeLayout>
public class ObservableScrollView extends ScrollView {
private View inner;
private float y;
private Rect normal = new Rect();
private boolean isCount = false;
public interface OnOverScrolledListener {
void onOverScrolled(android.widget.ScrollView scrollView,
int deltaX, int deltaY, boolean clampedX, boolean clampedY);
}
private OnOverScrolledListener mOnOverScrolledListener;
private int mOverScrollByDeltaX;
private int mOverScrollByDeltaY;
@Override protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
this.mOverScrollByDeltaX = deltaX;
this.mOverScrollByDeltaY = deltaY;
final boolean result = super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
return result;
};
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
if (mOnOverScrolledListener != null && (clampedX || clampedY)) {
mOnOverScrolledListener.onOverScrolled(this, mOverScrollByDeltaX, mOverScrollByDeltaY, clampedX, clampedY);
}
}
public OnOverScrolledListener getOnOverScrolledListener() {
return mOnOverScrolledListener;
}
public void setOnOverScrolledListener(OnOverScrolledListener onOverScrolledListener) {
this.mOnOverScrolledListener = onOverScrolledListener;
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@SuppressLint("MissingSuperCall")
@Override
protected void onFinishInflate() {
if (getChildCount() > 0) {
inner = getChildAt(0);
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (inner != null) {
commOnTouchEvent(ev);
}
return super.onTouchEvent(ev);
}
public void commOnTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
if (isNeedAnimation()) {
animation();
isCount = false;
}
break;
case MotionEvent.ACTION_MOVE:
final float preY = y;
float nowY = ev.getY();
int deltaY = (int) (preY - nowY);
if (!isCount) {
deltaY = 0;
}
y = nowY;
if (isNeedMove()) {
if (normal.isEmpty()) {
normal.set(inner.getLeft(), inner.getTop(),
inner.getRight(), inner.getBottom());
}
inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2,
inner.getRight(), inner.getBottom() - deltaY / 2);
}
isCount = true;
break;
default:
break;
}
}
public void animation() {
TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(),
normal.top);
ta.setDuration(200);
inner.startAnimation(ta);
inner.layout(normal.left, normal.top, normal.right, normal.bottom);
normal.setEmpty();
}
public boolean isNeedAnimation() {
return !normal.isEmpty();
}
public boolean isNeedMove() {
int offset = inner.getMeasuredHeight() - getHeight();
int scrollY = getScrollY();
if (scrollY == 0 || scrollY == offset) {
return true;
}
return false;
}
我试过的替代方法是,
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
View view = (View) scrollView.getChildAt(scrollView.getChildCount() - 1);
int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));
if(view.getTop()==scrollY){
// reaches the top end
Log.e("scrolled top","scrolling top");
}
// if diff is zero, then the bottom has been reached
if (diff == 0) {
// do stuff L
Log.e("bottom reached","bottom reached");
}
else {
TranslateAnimation animation = new TranslateAnimation(0f, 0f, 0f, -100f); // might need to review the docs
animation.setDuration(500); // set how long you want the animation
animation.setFillAfter(true);
myFirstModule.startAnimation(animation);
mySecondModule.startAnimation(animation);
//till 10..
}
}
});
}
但这在滚动期间没有得到应用。滚动和 View 上升后动画有一些延迟,而不是完美的动画。
最佳答案
我找到了一个解决方案:
public class ObservableScrollView extends ScrollView {
private ScrollCallbacks mCallbacks;
private static final int MAX_Y_OVERSCROLL_DISTANCE = 150;
private Context mContext;
private int mMaxYOverscrollDistance;
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
initBounceScrollView();
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mCallbacks != null) {
mCallbacks.onScrollChanged(l, t, oldl, oldt);
}
}
@Override
public int computeVerticalScrollRange() {
return super.computeVerticalScrollRange();
}
public void setCallbacks(ScrollCallbacks listener) {
mCallbacks = listener;
}
private void initBounceScrollView() {
// get the density of the screen and do some maths with it on the max
// overscroll distance
// variable so that you get similar behaviors no matter what the screen
// size
final DisplayMetrics metrics = mContext.getResources()
.getDisplayMetrics();
final float density = metrics.density;
mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
}
public interface ScrollCallbacks {
void onScrollChanged(int l, int t, int oldl, int oldt);
}
@SuppressLint("NewApi")
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
int scrollY, int scrollRangeX, int scrollRangeY,
int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
// This is where the magic happens, we have replaced the incoming
// maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,
scrollRangeX, scrollRangeY, maxOverScrollX,
mMaxYOverscrollDistance, isTouchEvent);
}
}
主要 Activity :
scrollView.setCallbacks(new ObservableScrollView.ScrollCallbacks() {
@Override
public void onScrollChanged(int l, int t, int oldl, int oldt) {
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (myFirstModule.getLocalVisibleRect(scrollBounds) ){
if(!myFirstModule.isAnim) {
if (myFirstModule.getY() < scrollBounds.top) {
myFirstModule.setAnimationView(2);
} else {
myFirstModule.setAnimationView(1);
}
}
}else{
myFirstModule.isAnim = false; //myFirstModule is layout. In my case, I used layout in separate class.
}
在第一个模块中:
// in firstModule i integrated layout which i skipped.. I am focusing only on main thing..
public boolean isAnim = false;
private Animation animSlideUp = null;
private Animation animSlideDown = null;
private void loadAnimation(){
animSlideUp = AnimationUtils.loadAnimation(getContext(), R.anim.bottom_to_top);
animSlideDown = AnimationUtils.loadAnimation(getContext(), R.anim.top_to_bottom);
}
//this i will use in mainactivity.
public void setAnimationView(int animType){
if(isVisible()) {
isAnim = true;
if (animType == 1) {
startAnimation(animSlideUp);
} else {
startAnimation(animSlideDown);
}
}
}
bottom_to_top:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="@android:anim/decelerate_interpolator">
<translate
android:fromXDelta="0%" android:toXDelta="0%"
android:fromYDelta="300" android:toYDelta="0"
android:duration="400" />
</set>
从上到下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="@android:anim/decelerate_interpolator">
<translate
android:fromXDelta="0%" android:toXDelta="0%"
android:fromYDelta="-300" android:toYDelta="0"
android:duration="400" />
</set>
就是这样。 :-) 无需任何库即可实现。
关于android - android滚动时如何拉动类似于elastic type的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40797810/
是否有一种 STL 算法允许我将一个函数应用于一个范围内的每个元素,转换元素,并将之前转换的元素作为输入? 我在想这样的事情(显然行不通,因为第二个迭代器将无效): struct Input {
我有一个字典列表,例如: l =[{country:'Italy',sales:100,cost:50}{country:'Italy',sales:130,cost:60} {co
考虑以下几点: $var = 'Now is the time' if ($var -like 'Now*') { 'true' } else { 'false' } 输出:真 现在交换 -like
我认为这是一个简单的问题,但尚未得到解决方案。我只想从此处解释的列中获取有效数字。 假设我们有一个包含以下值的 varchar 列 ABC Italy Apple 234.62 2:234:43:22
这个问题已经有答案了: MySQL LIKE IN()? (12 个回答) 已关闭 4 年前。 是否可以使用 IN 子句扩展 LIKE 表达式? 此时我得到以下 SQL: select * from
这个问题在这里已经有了答案: How to postpone/defer the evaluation of f-strings? (14 个答案) 关闭 3 年前。 考虑字符串 string_0
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
我刚刚阅读了以下关于同一主题的帖子: Facebook like notifications tracking (DB Design)和 Database design to store notifi
我如何在 javascript 中创建一个新事件/像在 c# 中一样? private event EventHandler asdCompleted; private void SetEventHa
我经常访问一个名为 GOOD 的网站我特别喜欢一种审美风格;导航栏如何在网站背景中扩展其颜色。如果您访问该网站,就会明白我的意思。 在 CSS 中,我怎样才能以最简单的方式复制它?我已经用 z-ind
我对 LINQ 比较陌生,不知道如何执行 Like 条件。我有一个 myObject 的 IEnumerable 列表,想要做一些类似 myObject.Description 的事情,比如“Help
我正在尝试在 Sharepoint 2013 提供商托管的应用程序中构建一个类似人员选择器的工具。最初,我使用的是 Utility.ResolvePrincipal,它让我可以访问 Sharepoin
过去几个月我一直在研究微服务架构应用程序,我仍在努力适应分布式特性。我多次注意到一种模式,但我不确定处理它的首选方式是什么。 假设我们有服务 A、服务 B 和服务 C。服务 A 公开了一个 API,其
这个问题在这里已经有了答案: Equivalent to unix "less" command within R console (5 个回答) 6年前关闭。 R 控制台中是否有任何命令与 Linu
是否可以在 Xcode 中为类似于 emacs 中的“标记”功能的行添加书签?还有我可以用来跳转到行号的快捷方式吗?我的源代码变得很长且难以导航。 最佳答案 是的;如果您将文本插入符号放在要添加书签的
在使用 vi 15 年的大部分时间后,我在使用 Go 时一时兴起尝试了 Rob Pike 的 Acme。我真的很喜欢它的小巧轻便。现代 unix 风格的东西在 Acme 中表现不佳,而 Ruby 开发
我正在寻找可以打印矩阵[1:5, 1:5] 的任何包中的函数。 head() 适用于列数较少但矩阵较大的用户。我知道我可以为它创建自己的函数,但我想知道是否已经有函数了。 最佳答案 在名为futile
我正在用 C++ 构建一个聚类算法,但我不能很好地处理 OOP 和发生变化的变量(成员数据)的状态。对于某种复杂的算法,我发现这是我发展的障碍。 因此,我正在考虑将编程语言更改为一种功能语言:Ocam
我有一个这样的日志: Jun 21 06:25:07 172.25.1.1 kernel: DROP IN=ppp0 OUT= MAC= SRC=206.221.177.2 DST=185.79.95
我需要一些帮助来制作类似于 Accordion 的东西。目标是,如果您单击导航中的链接,一个部分会消失,而您单击的部分会出现(在相同位置且不明显)。 如果可能,它还应该自动滚动到该部分的开头(导航的结
我是一名优秀的程序员,十分优秀!