caTea 블로그

AppBarLayout 부드럽게 스크롤 본문

android

AppBarLayout 부드럽게 스크롤

ZaRas 2017. 4. 14. 15:44
반응형
어느 양키성님께서 만든 클래스

아주 부드럽게 작동한다


/**
* Created by achee7059 on 2017. 4. 14..
*
* 앱바 스크롤 부드럽게 하는 클래스
*/

public class FlingBehavior extends AppBarLayout.Behavior {

private static final String TAG = FlingBehavior.class.getName();
private static final int TOP_CHILD_FLING_THRESHOLD = 1;
private static final float OPTIMAL_FLING_VELOCITY = 3500;
private static final float MIN_FLING_VELOCITY = 20;

boolean shouldFling = false;
float flingVelocityY = 0;

public FlingBehavior() {
}

public FlingBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
int velocityX, int velocityY, int[] consumed) {

super.onNestedPreScroll(coordinatorLayout, child, target, velocityX, velocityY, consumed);

if (velocityY > MIN_FLING_VELOCITY) {
shouldFling = true;
flingVelocityY = velocityY;
} else {
shouldFling = false;
}
}

@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target) {
super.onStopNestedScroll(coordinatorLayout, abl, target);
if (shouldFling) {
Log.d(TAG, "onNestedPreScroll: running nested fling, velocityY is " + flingVelocityY);
onNestedFling(coordinatorLayout, abl, target, 0, flingVelocityY, true);
}
}

@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
float velocityX, float velocityY, boolean consumed) {

if (target instanceof RecyclerView && velocityY < 0) {
Log.d(TAG, "onNestedFling: target is recyclerView");
final RecyclerView recyclerView = (RecyclerView) target;
final View firstChild = recyclerView.getChildAt(0);
final int childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild);
consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD;
}

// prevent fling flickering when going up
if (target instanceof NestedScrollView && velocityY > 0) {
consumed = true;
}

if (Math.abs(velocityY) < OPTIMAL_FLING_VELOCITY) {
velocityY = OPTIMAL_FLING_VELOCITY * (velocityY < 0 ? -1 : 1);
}
Log.d(TAG, "onNestedFling: velocityY - " + velocityY + ", consumed - " + consumed);

return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}
}

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/abl_recommend_detail"
app:layout_behavior="com.ubist.tourtainment.flag.utils.FlingBehavior">

mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (!shouldScroll)
mNestedScrollView.fullScroll(ScrollView.FOCUS_UP);
}
});

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

Log.e(TAG, "verticalOffset = " + Math.abs(verticalOffset));
Log.e(TAG, "appBarLayout.getTotalScrollRange = " + appBarLayout.getTotalScrollRange());

shouldScroll = Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange();

}


728x90

'android' 카테고리의 다른 글

android crash dump  (0) 2017.12.20
android google map 좌표들을 이용해 스크린에 꽉차게  (0) 2017.05.25
Magnify Anim View Pager  (0) 2017.03.13
android frame animation class  (0) 2017.02.22
android permission activity  (0) 2017.02.22