首页 > 编程知识 正文

scrollview实现水平滑动,RelativeLayout

时间:2023-05-06 16:31:49 阅读:134829 作者:4255

博主声明:

转载请在开头附上正文的链接和作者信息,并标记为转载。 潇洒的百褶裙主文艺的马里奥原创,请多关照。

本文开始了文艺马里奥|博客主页: https://blog.csdn.net/smile _ running

首先,我将简要介绍名为NestedScrollView的控件。 这是从FrameLayout继承的可滚动视图控件,其位置位于我们的support v4包下,使用方法也非常简单,但规定NestedScrollView内部只包含一个视图通常,我们只包含一个View

介绍了NestedScrollView的知识点。 这与ScrollView的使用方法相同。 但是,我们对APP应用可能有这样的需求。 也就是说,让我们的滚动显示具有一定的弹性动画效果。 类似于QQ列表这样的弹性效果。 我们在NestedScrollView中添加了一定的弹性动画后,用户体验明显增加。

接下来,我们来看一下实现的NestedScrollView的灵活效果

其最终效果是这样的。 其中的粉红色背景是LinearLayout,内容存储了两个TextView用于测试。 我不太会介绍这部分的代码,但是如下所示

nd.no.xww.qqmessagedragview.elasticscrollviewandroid 3360 layout _ width=' match _ parent ' Android 3360 layout _ hid FCC ff ' Android 3360 layout _ width=' match _ parent ' Android 3360 layout _ height=' wrap _ content ' Android 3360 layout=view Android 3360 layout _ width=' match _ parent ' Android : layout _ height=' 1000 DP ' Android 3360 gravity app _ name ' name ' textview Android 3360 layout _ width=' match _ parent ' Android 3360 layout _ height=' 1000 DP ' Android 3360 gravity=' c eight # fffff '/linear layout/nd.no.xww.qqmessagedragView.elasticscrollview首先,我们的view是一个可以滚动的视图。 直接继承现有的NestedScrollView就可以了。 然后,给它添加灵活的动画就可以了。

NestedScrollView内部只有一个直接子视图,必须获取该视图才能拖动其中的内容。 代码如下所示。

@ overrideprotectedvoidonfinishinflate () { super.onFinishInflate ); //获取的是scrollview的第一个子级viewif(getchildcount(0) ) minnerview=getchildat )0); }接下来是幻灯片的监听情况,有两种可能性。 第一个,NestedScrollView移动到了顶部top。 此时,可以继续拖动。 内容的位置也发生变化。 在手指离开的刹那,我们把原来的坐标和拖动的距离进行比较,把它恢复原状就可以了。 在此期间,可以打开动画效果并设置插值器。 很有效。

另一种可能性是NestedScrollView移动到了底部的bottom。 其处理方法与上述一致,不再重复。 当然,也有普通的情况

,既不是顶部、也不是底部,那就不去处理,正常来就好了。

    这里判断是否在 top 或 bottom 处,需要根据子 View( LinearLayout) 的宽度减去控件的宽度,如果有 margin 的话,还有减去 margin 的大小,代码如下:

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = MeasureSpec.getSize(widthMeasureSpec); mHeight = MeasureSpec.getSize(heightMeasureSpec); MarginLayoutParams lp = (MarginLayoutParams) mInnerView.getLayoutParams(); //减去 margin 的值 offset = mInnerView.getMeasuredHeight() - lp.topMargin - lp.bottomMargin - mHeight; }

    判断是否处于 top 或 bottom 代码

// 判断是否处于顶部或者底部 public boolean isNeedMove() { // 0是顶部,offset是底部 if (getScrollY() == 0 || getScrollY() >= offset) { return true; } return false; }

    最后,根据我们的触摸事件拿到的偏移量,开启动画即可

public void translateAnimator() { TranslateAnimation animation = new TranslateAnimation(0, 0, mInnerView.getTop(), mRect.top); animation.setDuration(500); animation.setInterpolator(new AnticipateInterpolator(3f)); mInnerView.startAnimation(animation); // 设置回到正常的布局位置 mInnerView.layout(mRect.left, mRect.top, mRect.right, mRect.bottom); mRect.setEmpty(); }

    下面是完整的代码,运行一下就会看到上面动态图的效果。

package nd.no.xww.qqmessagedragview;import android.content.Context;import android.graphics.Rect;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.v4.widget.NestedScrollView;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.animation.AnticipateInterpolator;import android.view.animation.TranslateAnimation;/** * @author xww * @desciption : 一个具有弹性的 NestedScrollView * @date 2019/8/4 * @time 22:00 * 博主:文艺的马里奥 * 博客:https://blog.csdn.net/smile_Running */public class ElasticScrollView extends NestedScrollView { private View mInnerView;// 孩子View private float mDownY;// 点击时y坐标 private Rect mRect = new Rect(); private int offset; private boolean isCount = false;// 是否开始计算 private int mWidth; private int mHeight; public ElasticScrollView(@NonNull Context context) { this(context, null); } public ElasticScrollView(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public ElasticScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onFinishInflate() { super.onFinishInflate(); //获取的就是 scrollview 的第一个子 View if (getChildCount() > 0) { mInnerView = getChildAt(0); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = MeasureSpec.getSize(widthMeasureSpec); mHeight = MeasureSpec.getSize(heightMeasureSpec); MarginLayoutParams lp = (MarginLayoutParams) mInnerView.getLayoutParams(); //减去 margin 的值 offset = mInnerView.getMeasuredHeight() - lp.topMargin - lp.bottomMargin - mHeight; } @Override public boolean onTouchEvent(MotionEvent e) { if (mInnerView != null) { commOnTouchEvent(e); } return super.onTouchEvent(e); } public void commOnTouchEvent(MotionEvent e) { switch (e.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: final float preY = mDownY;// 按下时的y坐标 float nowY = e.getY();// 时时y坐标 int deltaY = (int) (preY - nowY);// 滑动距离 //排除出第一次移动计算无法得知y坐标 if (!isCount) { deltaY = 0; } mDownY = nowY; // 当滚动到最上或者最下时就不会再滚动,这时移动布局 if (isNeedMove()) { if (mRect.isEmpty()) { // 保存正常的布局位置 mRect.set(mInnerView.getLeft(), mInnerView.getTop(), mInnerView.getRight(), mInnerView.getBottom()); } // 移动布局 mInnerView.layout(mInnerView.getLeft(), mInnerView.getTop() - deltaY / 2, mInnerView.getRight(), mInnerView.getBottom() - deltaY / 2); } isCount = true; break; case MotionEvent.ACTION_UP: if (isNeedAnimation()) { translateAnimator(); isCount = false; } break; } } public void translateAnimator() { TranslateAnimation animation = new TranslateAnimation(0, 0, mInnerView.getTop(), mRect.top); animation.setDuration(500); animation.setInterpolator(new AnticipateInterpolator(3f)); mInnerView.startAnimation(animation); // 设置回到正常的布局位置 mInnerView.layout(mRect.left, mRect.top, mRect.right, mRect.bottom); mRect.setEmpty(); } // 是否需要开启动画 public boolean isNeedAnimation() { return !mRect.isEmpty(); } // 判断是否处于顶部或者底部 public boolean isNeedMove() { // 0是顶部,offset是底部 if (getScrollY() == 0 || getScrollY() >= offset) { return true; } return false; }}

    代码不难理解,我刚开始并没有注意这个 Margin 的值,导致的情况可能会在底部的时候,无法继续向上拉,也就是底部没有弹性效果,所以要在测量时把 margin 也处理一下就能解决问题了。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。