[英]Custom CoordinatorLayout.Behavior and RecyclerView scroll issue
我有一個帶有兩個孩子的CoordinatorLayout
,一個作為標題的View
和一個RecyclerView
:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
app:layout_behavior="some.package.AlphaBehavior">
<ImageView
android:id="@+id/header_iv"
style="@style/some_style"/>
<TextView
android:id="@+id/header_retails_tv"
style="@style/some_style_tv"
android:text="@string/some_text"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false" />
</android.support.design.widget.CoordinatorLayout>
我使用標題大小動態設置填充到RecyclerView
,並將clipToPadding
設置為false
,因此RecyclerView
顯示在標題下方,當用戶向上滾動時, RecyclerView
顯示在標題視圖上方。
我做了一個自定義的CoordinatorLayout.Behavior
,以便在用戶向上滾動list
時完成淡出視圖,並在標題必須再次可見時淡入淡出, AlphaBehavior
:
public class AlphaBehavior extends CoordinatorLayout.Behavior {
private float alpha = 1.0f;
private float scrolly = 0.f;
private int headerSize = 0;
private Animation defaultFadeInAnimation;
public AlphaBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
defaultFadeInAnimation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
}
public void setHeaderSize(int headerSize) {
this.headerSize = headerSize;
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof RecyclerView;
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
scrolly += dyConsumed;
Log.d(Constants.TAG, dyConsumed + "/" + dyUnconsumed + "/" + scrolly);
float totalScrollY = ((RecyclerView)target).computeVerticalScrollOffset();
Log.d(Constants.TAG, "totalScrollY:" + totalScrollY);
alpha = (headerSize - totalScrollY) / headerSize;
if (alpha < 0.f) alpha = 0.f;
if (alpha > 1.0f) alpha = 1.f;
if (dyConsumed < 0 && totalScrollY > headerSize) {
alpha = 0.f;
}
Log.d(Constants.TAG, "alpha:" + alpha);
child.setAlpha(alpha);
}
@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target) {
int pos = ((LinearLayoutManager)((RecyclerView)target).getLayoutManager()).findFirstCompletelyVisibleItemPosition();
Log.d(Constants.TAG, "pos:" + pos);
if (pos == 0 && child.getAlpha() == 0.f) {
child.startAnimation(defaultFadeInAnimation);
}
}
// overriding this in case we don't the other events are not called
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
}
但是我遇到了一個問題:如果用戶滾動非常快,則行為事件的調用不正確。 scrollY
成員與總滾動不完全相關,而且totalScrollY
成員(從RecyclerView
計算滾動獲得)不正確。 即使我試圖在onStopNestedScroll
事件中找到firstCompletelyVisibleItem
,但當recyclerView
實現列表的開始時它返回位置2或3。
最后,我使用OnScrollListener
而不是使用CoordinatorLayout.Behavior
解決了它,它就像一個魅力。 我把代碼,也許對某人有用:
用於隱藏視圖的自定義onScrollListener:
public class HideViewOnScrollListener extends RecyclerView.OnScrollListener {
private float alpha = 1.f;
private float scrolly = 0.f;
private int heightViewToHide;
private final View viewToHide;
public HideViewOnScrollListener(View viewToHide) {
this.viewToHide = viewToHide;
heightViewToHide = viewToHide.getHeight();
if (heightViewToHide == 0) {
ViewTreeObserver viewTreeObserver = viewToHide.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
heightViewToHide = viewToHide.getHeight();
if (heightViewToHide > 0)
viewToHide.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
scrolly += dy;
alpha = (heightViewToHide - scrolly) / heightViewToHide;
if (alpha < 0.f) alpha = 0.f;
if (alpha > 1.0f) alpha = 1.f;
if (dy < 0 && scrolly > heightViewToHide) {
alpha = 0.f;
}
viewToHide.setAlpha(alpha);
}
}
您可以通過以下方式添加到RecyclerView:
recyclerView.addOnScrollListener(new HideViewOnScrollListener(viewToHide));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.