问题描述
我试图模仿谷歌Plus应用程序在我的项目,因为它似乎是现在的参考。
I am trying to mimic the Google Plus application in my project, as it seems to be the reference now.
滚动时,列表视图效果是非常好的,我希望做类似的事情。
The listview effect when scrolling is really nice and I would like to do something similar.
我已经开始与LayoutAnimationControllerhttp://android-er.blogspot.be/2009/10/listview-and-listactivity-layout.html
I have started with the LayoutAnimationControllerhttp://android-er.blogspot.be/2009/10/listview-and-listactivity-layout.html
LayoutAnimationController controller
= AnimationUtils.loadLayoutAnimation(
this, R.anim.list_layout_controller);
getListView().setLayoutAnimation(controller);
这似乎不好,因为不是所有的元素都动画:
and that seems bad as not all the elements are animated:
所以,我结束了使用适配器的getView和使用这样的:
So I ended up by using the getView of the Adapter and using this:
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(800);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(600);
set.addAnimation(animation);
row.startAnimation(set);
其结果是真棒,我真的很高兴呢!
The result is awesome and I am really happy with it!
不幸的是,只有当我从上滚动到列表的底部作品!
Unfortunately, it only works when I scroll from top to bottom of the list!
二要使其在另一边滚动时的工作,我需要改变一点点的TranslateAnimation
I I want to make it work when scrolling in the other side, I need to change a little bit the TranslateAnimation
所以我的问题,是有办法来检测,如果我向上滚动在我的适配器向下?
SO my question, is there a way to detect if I scroll upwards on downwards in my Adapter?
谢谢!
推荐答案
指定的<$c$c>OnScrollListener$c$c>你的的ListView
。创建一个标志,指示用户是否向上或向下滚动。通过检查设置一个适当的值标志,如果当前的第一的可见项目位置等于低于previous的第一的可见项目的位置,以更多或更少。把里面的 onScrollStateChanged,办理入住手续()
。
Assign an OnScrollListener
to your ListView
. Create a flag which indicates whether the user is scrolling up or down. Set an appropriate value to the flag by checking if the current first visible item position equals to more or less than the previous first visible item position. Put that check inside onScrollStateChanged()
.
样品code:
private int mLastFirstVisibleItem;
private boolean mIsScrollingUp;
public void onScrollStateChanged(AbsListView view, int scrollState) {
final ListView lw = getListView();
if (view.getId() == lw.getId()) {
final int currentFirstVisibleItem = lw.getFirstVisiblePosition();
if (currentFirstVisibleItem > mLastFirstVisibleItem) {
mIsScrollingUp = false;
} else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
mIsScrollingUp = true;
}
mLastFirstVisibleItem = currentFirstVisibleItem;
}
}
检查 mIsScrollingUp
是 true或false getView()
,并相应地分配动画。
Check if mIsScrollingUp
is true or false in getView()
, and assign the animations accordingly.
这篇关于在适配器检测滚动方向(上/下)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!