Android 向上滚动隐藏视图并向下滚动显示像推特一样的视图效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21824508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android scroll up hide view and scroll down show view effect like twitter
提问by NaiveBz
How to do scrolling effect like twitter when scroll Up hide viewpager tab (Home, Discover, activity). Or effect like facebook scrolling, while scroll up hide option view(status, photo, checkin) when scroll down show option view. Any example link will do please help.
向上滚动隐藏viewpager选项卡(主页,发现,活动)时如何像推特一样滚动效果。或者像 facebook 滚动一样,向上滚动时隐藏选项视图(状态、照片、签入),向下滚动时显示选项视图。任何示例链接都可以,请帮忙。
回答by Vansuita Jr.
Easy solution:
简单的解决方案:
public abstract class OnScrollObserver implements AbsListView.OnScrollListener {
public abstract void onScrollUp();
public abstract void onScrollDown();
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
int last = 0;
boolean control = true;
@Override
public void onScroll(AbsListView view, int current, int visibles, int total) {
if (current < last && !control) {
onScrollUp();
control = true;
} else if (current > last && control) {
onScrollDown();
control = false;
}
last = current;
}
Usage:
用法:
listView.setOnScrollListener(new OnScrollObserver() {
@Override
public void onScrollUp() {
}
@Override
public void onScrollDown() {
}
});
回答by Kevin Robatel
EDIT:better, you have this library https://github.com/ksoichiro/Android-ObservableScrollView
编辑:更好,你有这个库https://github.com/ksoichiro/Android-ObservableScrollView
You can look at this https://github.com/LarsWerkman/QuickReturnListView
你可以看看这个https://github.com/LarsWerkman/QuickReturnListView
回答by przemek19980102
That's my own implementation: notice:
那是我自己的实现:注意:
- View to be hidden should be fixed height
- We are not hiding the view by Visiblity.GONE
- We are setting the final height to 0px
- 要隐藏的视图应为固定高度
- 我们没有通过 Visiblity.GONE 隐藏视图
- 我们将最终高度设置为 0px
Here is the code:
这是代码:
//Your view which you would like to animate
final RelativeLayout yourViewToHide = (yourViewToHideativeLayout) findViewById(R.id.topWrapper);
//The initial height of that view
final int initialViewHeight = yourViewToHide.getLayoutParams().height;
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//Try catch block for NullPointerExceptions
try{
//Here is a simple delay. If user scrolls ListView from the top of the screen to the bottom then continue
if(firstVisibleItem % visibleItemCount == 0) {
//Here we initialize the animator, doesn't matter what values You will type in
ValueAnimator animator = ValueAnimator.ofInt(0, 1);
//if Scrolling up
if (fastScrollSB.getProgress() > view.getFirstVisiblePosition()){
//Getting actual yourViewToHide params
ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams();
if (!animator.isRunning()) {
//Setting animation from actual value to the initial yourViewToHide height)
animator.setIntValues(params.height, initialViewHeight);
//Animation duration
animator.setDuration(500);
//In this listener we update the view
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams();
params.height = (int) animation.getAnimatedValue();
yourViewToHide.setLayoutParams(params);
}
});
//Starting the animation
animator.start();
}
System.out.println("Scrolling up!");
//If not scrolling
} else if (fastScrollSB.getProgress() == view.getFirstVisiblePosition()) {
System.out.println("Not Scrolling!");
//If scrolling down
} else if (fastScrollSB.getProgress() < view.getFirstVisiblePosition()){
//Getting actual yourViewToHide params
ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams();
if (!animator.isRunning()) {
//Setting animation from actual value to the target value (here 0, because we're hiding the view)
animator.setIntValues(params.height, 0);
//Animation duration
animator.setDuration(500);
//In this listener we update the view
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams();
params.height = (int) animation.getAnimatedValue();
yourViewToHide.setLayoutParams(params);
}
});
//Starting the animation
animator.start();
}
System.out.println("Scrolling down!");
}
}
}catch (Exception e){
e.printStackTrace();
}
}
});`
Hope it fits your needs :)
希望它适合您的需求:)
回答by QVDev
there is no quick example for this. But what you can do is keep track which way you are scrolling and show or hide the view accordingly
没有简单的例子。但是您可以做的是跟踪滚动的方式并相应地显示或隐藏视图
For example get first visible position of the ListView keep track of this and if it is smaller then before you know you are scrolling up this way you can show the view. In case it is bigger then hide the view.
例如,获取 ListView 的第一个可见位置,跟踪它,如果它较小,那么在您知道以这种方式向上滚动之前,您可以显示视图。如果它更大,则隐藏视图。
This is a simple approach in case you want to have more precise you need to work with onTouchListeners and y coordinates of the movement.
这是一种简单的方法,以防您想要更精确地使用 onTouchListeners 和移动的 y 坐标。
http://developer.android.com/reference/android/widget/ListView.html
http://developer.android.com/reference/android/widget/ListView.html