展开appbarlayout当recyclerview滚动/一扔顶部 [英] Expand appbarlayout when recyclerview is scrolled/fling to top
问题描述
我实现了一个recyclerview布局collapsingtoolbar中所示连接样品code。我的问题是,当我向下甩列表,它不会一路去顶。
I implemented a collapsingtoolbar layout with a recyclerview as shown in the sample code attached. My issue is that, when I fling the list downward, it does not go all the way to the top.
什么情况是,滚动就在那里AppBarLayout应该结束点停止。
What happens is that, the scrolling stops right at the point where the AppBarLayout is supposed to end.
这是我要的是在向下扔列表中的效果,该列表将所有的方式去顶,揭示/扩大AppBarLayout
The effect that I want is upon flinging the list downward, the list will go all the way to the top AND reveal/expand the AppBarLayout
我minSdk为14。任何帮助或建议是大大AP preciated。
My minSdk is 14. Any help or suggestion is greatly appreciated.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout>
<android.support.design.widget.CollapsingToolbarLayout
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
app:layout_collapseMode="parallax">
//some elements
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
app:layout_behavior="@string/appbar_scrolling_view_behavior"/> //value android.support.design.widget.AppBarLayout$ScrollingViewBehavior
<android.support.v7.widget.Toolbar
app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_collapseMode="parallax" />
推荐答案
您可以完全展开或与<崩溃的应用栏href=\"http://developer.android.com/reference/android/support/design/widget/AppBarLayout.html#setExpanded(boolean,%20boolean)\"相对=nofollow> setExpanded()
方法。一个实现可能涉及重写 dispatchTouchEvent()
在活动
类,自动折叠/扩展根据您的应用程序栏无论是过去坍塌的中间点:
You can fully expand or collapse the App Bar with the setExpanded()
method. One implementation could involve overriding dispatchTouchEvent()
in your Activity
class, and auto-collapsing/expanding your App Bar based on whether it is collapsed past the halfway point:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
float per = Math.abs(mAppBarLayout.getY()) / mAppBarLayout.getTotalScrollRange();
boolean setExpanded = (per <= 0.5F);
mAppBarLayout.setExpanded(setExpanded, true);
}
return super.dispatchTouchEvent(event);
}
在就自动滚动到上一扔的最后一个位置,我已经把在GitHub上一些code,显示了如何的编程平滑滚动到特定地点这可能会有帮助。调用滚动至则为list.size() - 对
比如可以复制的行为。顺便说一句这code的部分改编自 StylingAndroid 并 Novoda 博客:一扔
1
In respect to automatically scrolling to the last position on a fling, I have put some code on GitHub that shows how to programmatically smooth scroll to a specific location that may help. Calling a scroll to list.size() - 1
on a fling
for instance could replicate the behaviour. Parts of this code by the way are adapted from the StylingAndroid and Novoda blogs:
public class RecyclerLayoutManager extends LinearLayoutManager {
private AppBarManager mAppBarManager;
private int visibleHeightForRecyclerView;
public RecyclerLayoutManager(Context context) {
super(context);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
View firstVisibleChild = recyclerView.getChildAt(0);
final int childHeight = firstVisibleChild.getHeight();
int distanceInPixels = ((findFirstVisibleItemPosition() - position) * childHeight);
if (distanceInPixels == 0) {
distanceInPixels = (int) Math.abs(firstVisibleChild.getY());
}
//Called Once
if (visibleHeightForRecyclerView == 0) {
visibleHeightForRecyclerView = mAppBarManager.getVisibleHeightForRecyclerViewInPx();
}
//Subtract one as adapter position 0 based
final int visibleChildCount = visibleHeightForRecyclerView/childHeight - 1;
if (position <= visibleChildCount) {
//Scroll to the very top and expand the app bar
position = 0;
mAppBarManager.expandAppBar();
} else {
mAppBarManager.collapseAppBar();
}
SmoothScroller smoothScroller = new SmoothScroller(recyclerView.getContext(), Math.abs(distanceInPixels), 1000);
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
public void setAppBarManager(AppBarManager appBarManager) {
mAppBarManager = appBarManager;
}
private class SmoothScroller extends LinearSmoothScroller {
private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000;
private final float distanceInPixels;
private final float duration;
public SmoothScroller(Context context, int distanceInPixels, int duration) {
super(context);
this.distanceInPixels = distanceInPixels;
float millisecondsPerPx = calculateSpeedPerPixel(context.getResources().getDisplayMetrics());
this.duration = distanceInPixels < TARGET_SEEK_SCROLL_DISTANCE_PX ?
(int) (Math.abs(distanceInPixels) * millisecondsPerPx) : duration;
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return RecyclerLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int calculateTimeForScrolling(int dx) {
float proportion = (float) dx / distanceInPixels;
return (int) (duration * proportion);
}
}
}
编辑:
AppBarManager
在上面的code段是指接口
用于与<$ C沟通$ C> AppBarLayout 在活动
。折叠/展开应用程序栏的方法做到这一点,与动画。最后的方法来计算 RecyclerView
的行数显示在屏幕上:
AppBarManager
in the above code snippet refers to an interface
used to communicate with the AppBarLayout
in an Activity
. Collapse/expand app bar methods do just that, with animations. The final method is used to calculate the number of RecyclerView
rows visible on screen:
<一个href=\"https://github.com/PPartisan/ScrollHandleExample/blob/master/app/src/main/java/com/werd$p$pssed/partisan/scrollhandleexample/AppBarManager.java\"相对=nofollow> AppBarManager.java
public interface AppBarManager {
void collapseAppBar();
void expandAppBar();
int getVisibleHeightForRecyclerViewInPx();
}
<一个href=\"https://github.com/PPartisan/ScrollHandleExample/blob/master/app/src/main/java/com/werd$p$pssed/partisan/scrollhandleexample/MainActivity.java\"相对=nofollow> MainActivity.java
public class MainActivity extends AppCompatActivity implements AppBarManager{
@Override
public void collapseAppBar() {
mAppBarLayout.setExpanded(false, true);
}
@Override
public void expandAppBar() {
mAppBarLayout.setExpanded(true, true);
}
@Override
public int getVisibleHeightForRecyclerViewInPx() {
if (mRecyclerFragment == null) mRecyclerFragment =
(RecyclerFragment) getSupportFragmentManager().findFragmentByTag(RecyclerFragment.TAG);
int windowHeight, appBarHeight, headerViewHeight;
windowHeight = getWindow().getDecorView().getHeight();
appBarHeight = mAppBarLayout.getHeight();
headerViewHeight = mRecyclerFragment.getHeaderView().getHeight();
return windowHeight - (appBarHeight + headerViewHeight);
}
这篇关于展开appbarlayout当recyclerview滚动/一扔顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!