㈠ android的这种动画效果的listview是怎么做出来的
可以试试listview的smoothScrollByOffset和smoothScrollToPosition,我试了一下,不是直接跳过去,而是滚过去,你可以试试看能不能符合你的要求。
㈡ android 为什么初始界面scrollView.scrollTo执行无效
翻阅查找ScrollView的文档并搜索了一下没有发现直接设置的属性和方法,这里通过继承来达到这一目的。 /** * 快/慢滑动ScrollView * @author 农民伯伯 * */ public class SlowScrollView extends ScrollView { public SlowScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public SlowScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public SlowScrollView(Context context) { super(context); } /** * 滑动事件 */ @Override public void fling(int velocityY) { super.fling(velocityY / 4); } } 代码说明: 重点在"velocityY / 4",这里意思是滑动速度减慢到原来四分之一的速度,这里大家可以根据自己的需求加快或减慢滑动速度。 查看原帖>>
㈢ android中ScrollView的smoothScrollTo为什么会不执行呢
系统故障?
㈣ scrollView的scrollTo如何设置动画时间
翻阅查找ScrollView的文档并搜索了一下没有发现直接设置的属性和方法,这里通过继承来达
㈤ Android scrollBy和scrollTo的区别
scrollTo(int x,int y): 如果偏移位置发生了改变,就会给mScrollX和mScrollY赋新值,改变当前位置。 注意:x,y代表的不是坐标点,而是偏移量。 scrollBy(int x,int y): 它实际上是调用了scrollTo(mScrollX + x, mScrollY + y); mScrollX + x和mScrollY + y,即表示在原先偏移的基础上在发生偏移,通俗的说就是相对当前位置偏移。 blog#csdn#net/vipzjyno1/article/details/24577023
㈥ scrollto和scrollby的区别
1、区别
1)scrollTo()是一步到位;
2)scrollBy()是逐步累加。
2、例子:
1)假设有一个View,如果想把SView从(0, 0)移动到(100, 100)。注意,这里说的(0, 0)和(100, 100),指的是SView左上角的坐标。那么偏移量就是原点(0, 0)到目标点(100, 100)的距离,即(0 , 0) - (100, 100) = (-100, -100)。
只需要调用SView.scrollTo(-100, -100)就可以了。scrollTo(int x, int y)的两个参数x和y,代表的是偏移量,这时的参照物是(0, 0)点。
然而,scrollBy()是有一定的区别的。scrollBy()的参照物是(0, 0)点加上偏移量之后的坐标。
2)假设SView调用了scrollTo(-100, -100),此时SView左上角的坐标是(100, 100),这时再调用scrollBy(-20, -20),此时SView的左上角就被绘制到了(120, 120)这个位置。
㈦ android 如何实现边缘拖动侧滑
据需求需要自己自定义一个ListView来实现该功能,接下来先贴出代码再讲解具体的实现
package com.example.slidecutlistview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Scroller;
/**
* @blog http://blog.csdn.net/xiaanming
*
* @author xiaanming
*
*/
public class SlideCutListView extends ListView {
/**
* 当前滑动的ListViewposition
*/
private int slidePosition;
/**
* 手指按下X的坐标
*/
private int downY;
/**
* 手指按下Y的坐标
*/
private int downX;
/**
* 屏幕宽度
*/
private int screenWidth;
/**
* ListView的item
*/
private View itemView;
/**
* 滑动类
*/
private Scroller scroller;
private static final int SNAP_VELOCITY = 600;
/**
* 速度追踪对象
*/
private VelocityTracker velocityTracker;
/**
* 是否响应滑动,默认为不响应
*/
private boolean isSlide = false;
/**
* 认为是用户滑动的最小距离
*/
private int mTouchSlop;
/**
* 移除item后的回调接口
*/
private RemoveListener mRemoveListener;
/**
* 用来指示item滑出屏幕的方向,向左或者向右,用一个枚举值来标记
*/
private RemoveDirection removeDirection;
// 滑动删除方向的枚举值
public enum RemoveDirection {
RIGHT, LEFT;
}
public SlideCutListView(Context context) {
this(context, null);
}
public SlideCutListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SlideCutListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
screenWidth = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
scroller = new Scroller(context);
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
}
/**
* 设置滑动删除的回调接口
* @param removeListener
*/
public void setRemoveListener(RemoveListener removeListener) {
this.mRemoveListener = removeListener;
}
/**
* 分发事件,主要做的是判断点击的是那个item, 以及通过postDelayed来设置响应左右滑动事件
*/
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
addVelocityTracker(event);
// 假如scroller滚动还没有结束,我们直接返回
if (!scroller.isFinished()) {
return super.dispatchTouchEvent(event);
}
downX = (int) event.getX();
downY = (int) event.getY();
slidePosition = pointToPosition(downX, downY);
// 无效的position, 不做任何处理
if (slidePosition == AdapterView.INVALID_POSITION) {
return super.dispatchTouchEvent(event);
}
// 获取我们点击的item view
itemView = getChildAt(slidePosition - getFirstVisiblePosition());
break;
}
case MotionEvent.ACTION_MOVE: {
if (Math.abs(getScrollVelocity()) > SNAP_VELOCITY
|| (Math.abs(event.getX() - downX) > mTouchSlop && Math
.abs(event.getY() - downY) < mTouchSlop)) {
isSlide = true;
}
break;
}
case MotionEvent.ACTION_UP:
recycleVelocityTracker();
break;
}
return super.dispatchTouchEvent(event);
}
/**
* 往右滑动,getScrollX()返回的是左边缘的距离,就是以View左边缘为原点到开始滑动的距离,所以向右边滑动为负值
*/
private void scrollRight() {
removeDirection = RemoveDirection.RIGHT;
final int delta = (screenWidth + itemView.getScrollX());
// 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item
scroller.startScroll(itemView.getScrollX(), 0, -delta, 0,
Math.abs(delta));
postInvalidate(); // 刷新itemView
}
/**
* 向左滑动,根据上面我们知道向左滑动为正值
*/
private void scrollLeft() {
removeDirection = RemoveDirection.LEFT;
final int delta = (screenWidth - itemView.getScrollX());
// 调用startScroll方法来设置一些滚动的参数,我们在computeScroll()方法中调用scrollTo来滚动item
scroller.startScroll(itemView.getScrollX(), 0, delta, 0,
Math.abs(delta));
postInvalidate(); // 刷新itemView
}
/**
* 根据手指滚动itemView的距离来判断是滚动到开始位置还是向左或者向右滚动
*/
private void scrollByDistanceX() {
// 如果向左滚动的距离大于屏幕的三分之一,就让其删除
if (itemView.getScrollX() >= screenWidth / 3) {
scrollLeft();
} else if (itemView.getScrollX() <= -screenWidth / 3) {
scrollRight();
} else {
// 滚回到原始位置,为了偷下懒这里是直接调用scrollTo滚动
itemView.scrollTo(0, 0);
}
}
/**
* 处理我们拖动ListView item的逻辑
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isSlide && slidePosition != AdapterView.INVALID_POSITION) {
addVelocityTracker(ev);
final int action = ev.getAction();
int x = (int) ev.getX();
switch (action) {
case MotionEvent.ACTION_MOVE:
int deltaX = downX - x;
downX = x;
// 手指拖动itemView滚动, deltaX大于0向左滚动,小于0向右滚
itemView.scrollBy(deltaX, 0);
break;
case MotionEvent.ACTION_UP:
int velocityX = getScrollVelocity();
if (velocityX > SNAP_VELOCITY) {
scrollRight();
} else if (velocityX < -SNAP_VELOCITY) {
scrollLeft();
} else {
scrollByDistanceX();
}
recycleVelocityTracker();
// 手指离开的时候就不响应左右滚动
isSlide = false;
break;
}
return true; // 拖动的时候ListView不滚动
}
//否则直接交给ListView来处理onTouchEvent事件
return super.onTouchEvent(ev);
}
@Override
public void computeScroll() {
// 调用startScroll的时候scroller.computeScrollOffset()返回true,
if (scroller.computeScrollOffset()) {
// 让ListView item根据当前的滚动偏移量进行滚动
itemView.scrollTo(scroller.getCurrX(), scroller.getCurrY());
postInvalidate();
// 滚动动画结束的时候调用回调接口
if (scroller.isFinished()) {
if (mRemoveListener == null) {
throw new NullPointerException(RemoveListener is null, we should called setRemoveListener());
}
itemView.scrollTo(0, 0);
mRemoveListener.removeItem(removeDirection, slidePosition);
}
}
}
/**
* 添加用户的速度跟踪器
*
* @param event
*/
private void addVelocityTracker(MotionEvent event) {
if (velocityTracker == null) {
velocityTracker = VelocityTracker.obtain();
}
velocityTracker.addMovement(event);
}
/**
* 移除用户速度跟踪器
*/
private void recycleVelocityTracker() {
if (velocityTracker != null) {
velocityTracker.recycle();
velocityTracker = null;
}
}
/**
* 获取X方向的滑动速度,大于0向右滑动,反之向左
*
* @return
*/
private int getScrollVelocity() {
velocityTracker.computeCurrentVelocity(1000);
int velocity = (int) velocityTracker.getXVelocity();
return velocity;
}
/**
*
* 当ListView item滑出屏幕,回调这个接口
* 我们需要在回调方法removeItem()中移除该Item,然后刷新ListView
*
* @author xiaanming
*
*/
public interface RemoveListener {
public void removeItem(RemoveDirection direction, int position);
}
}
㈧ android recyclerview 是否能上滑动
使用RecyclerView时,调用smoothScrollToPostion()方法滑动到指定位置,但是条目很多时滑动的很慢,本篇文章就是实现RecyclerView的快速滑动。
先介绍如何实现,然后再介绍原理。
实现代码创建FastScrollLinearLayoutManager,继承LinearLayoutManager复moothScrollToPosition()方法,主要复写LinearSmoothScroller中方法
代码如下,解释全在注释中:
public class FastScrollLinearLayoutManager extends LinearLayoutManager {
public FastScrollLinearLayoutManager(Context context) { super(context);
} @Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) { @Override
public PointF (int targetPosition) { return FastScrollLinearLayoutManager.this.(targetPosition);
} //该方法控制速度。
//if returned value is 2 ms, it means scrolling 1000 pixels with LinearInterpolation should take 2 seconds.
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { /*
mdpi上, 1英寸有160个像素点, 25/160,
xxhdpi,1英寸有480个像素点, 25/480,
*/
//return 10F / displayMetrics.densityDpi;//可以减少时间,默认25F
return super.calculateSpeedPerPixel(displayMetrics);
//Calculates the time it should take to scroll the given distance (in pixels)
protected int calculateTimeForScrolling(int dx) { /*
控制距离, 然后根据上面那个方(calculateSpeedPerPixel())提供的速度算出时间,默认一次 滚动 TARGET_SEEK_SCROLL_DISTANCE_PX = 10000个像素, 在此处可以减少该值来达到减少滚动时间的目的.
*/
//间接计算时提高速度,也可以直接在calculateSpeedPerPixel提高
if (dx > 3000) {
dx = 3000;
} int time = super.calculateTimeForScrolling(dx);
LogUtil.d(time);//打印时间看下
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
从复写的两个方法可以看出,都是为了提高滑动速度。一种是直接修改速度,另外一种是通过减少距离来减少所需时间,间接提高滑动速度。
这两种方法都可以,看自己所需。接下来就讲讲实现的原理。这块我只是梳理的大致的流程,不过至此你已经可以实现快速滑动了。
2. RecyclerView 滑动过程梳理
1.调用RecyclerView.smoothScrollToPosition(position)时public void moothScrollToPosition(int position) { //...直接调用了LayoutManage的该方法
}1234
2.LinearLayoutManager中@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) { //...
}; //设置终点位置
linearSmoothScroller.setTargetPosition(position); //开始滚动,使用LinearSmoothScroller(一直匀速滑动,当targetPosition出现在屏幕上时再减速滑动),startSmoothScroll()是LayoutManager中的方法
startSmoothScroll(linearSmoothScroller);
}1234567891011
3.LayoutManager中public void startSmoothScroll(SmoothScroller smoothScroller) { //...
mSmoothScroller = smoothScroller; //调用SmoothScroller.start()方法开始滚动,this参数指当前LayoutManager
mSmoothScroller.start(mRecyclerView, this);
}123456
4.SmoothScroller中
void start(RecyclerView recyclerView, LayoutManager layoutManager) { //...
//使用ViewFlinger进行动画,ViewFlinger实现了Runnable接口,并且内部使用了Scroller,这样就可以post自己进而对RecyclerView不断layout就可以实现滑动
mRecyclerView.mViewFlinger.postOnAnimation();
}12345
5.ViewFlinger中,这是实现滑动的重点,省略了很多代码逻辑
private class ViewFlinger implements Runnable {
@Override
public void run() { if (scroller.computeScrollOffset()) { //调用SmoothScroller的onAnimation方法
smoothScroller.onAnimation(dx - overscrollX, dy - overscrollY);
}
}
}123456789
6.SmoothScroller中private void onAnimation(int dx, int dy) { //...
if (mTargetView != null) { // verify target position
if (getChildPosition(mTargetView) == mTargetPosition) { //要滑动到的位置已经显示在屏幕上,onTargetFound()方法里update了差值器,由线性差值器变成了减速的差值器。
onTargetFound(mTargetView, recyclerView.mState, mRecyclingAction);
mRecyclingAction.runIfNecessary(recyclerView);
} //...
if (mRunning) { //再下一次滑动
onSeekTargetStep(dx, dy, recyclerView.mState, mRecyclingAction); //调用内部类Action的runIfNecessary方法
mRecyclingAction.runIfNecessary(recyclerView);
}
}12345678910111213141516171819
7.Action中 private void runIfNecessary(RecyclerView recyclerView) { //调用了ViewFlinger.smoothScrollBy()方法,并传入了mDuration,mDuration是在SmoothScroller中upDate()时传入的,就是由前文讲的两个方法共同决定的
recyclerView.mViewFlinger.smoothScrollBy(mDx, mDy, mDuration, mInterpolator);
}1234
8.ViewFlinger中开始滚动 public void smoothScrollBy(int dx, int dy, int ration, Interpolator interpolator) { if (mInterpolator != interpolator) {
mInterpolator = interpolator;
mScroller = ScrollerCompat.create(getContext(), interpolator);
}
setScrollState(SCROLL_STATE_SETTLING);
mLastFlingX = mLastFlingY = 0; //调用Scroller开始滚动,此处即ration
mScroller.startScroll(0, 0, dx, dy, ration);
postOnAnimation();
}1234567891011
这块粗略的按照流程说了一下滚动过程,涉及的类比较多,最终通过Scroller来进行滚动。
结语:本篇文章实现了RecyclerView的快速滚动,但需要注意一个问题:如果你的Item比较复杂,滚动起来会卡顿。 这个在看源码时的一个注释里面有提到,后来实践时确实也发现。不得不说微信朋友圈滑动起来的真的快,它用的是ListView,貌似开启了FastEnable属性。 同时也可以仿照知乎,先使用RecyclerView.scrollToPosition(position)直接滑动到某一个位置后再使用smoothScrollToPosition(0)滑动到顶部。这块在RecyclerView里的Action类中jumpTo()的注释里有提到,如果很远的话可以先到一个位置后再滑动。
㈨ 如何以编程方式滚动的Android的WebView-android,scrolling
事实证明,在window.scrollTo()不工作,你就不能添加您的scrollTo()。对于原因,我自己的scrollTo()方法被调用时,我打电话window.scrollTo()。 因此,简言之,以滚动的WebView到特定的DOM写一个javaScript函数来完成滚动:
function scrollToElement(id) {
var elem = document.getElementById(id);
var x = 0;
var y = 0;
while (elem != null) {
x += elem.offsetLeft;
y += elem.offsetTop;
elem = elem.offsetParent;
}
window.scrollTo(x, y);
}
然后从你的Android应用程序(Java代码),告诉你的WebView加载一个URL:
webView.loadUrl("javascript:scrollToElement('" + elemId + "')");
有这种方法的问题,例如在滚动不会很好地进行动画处理,但该工程。 DOM的window对象是否正确报告的WebView的当前滚动位置(见window.pageXOffset,window.pageYOffset或window.scrollX,window.scrollY)。如果你只是想知道的WebView的当前滚动位置,编写JavaScript来调用Java代码,并沿X / Y偏移量通过。
㈩ android listview 当选中一个item的时候就把这个item滚动到顶端。
listview.smoothScrollToPositonFromTop(position, 0, 60); 三个参数分别是选择的position,滚动后到顶部的距离,用时多少ms。