Commit 8df2e49c authored by jyx's avatar jyx

代码优化

parent 3164499e
......@@ -6,6 +6,8 @@ import android.view.MotionEvent;
import android.view.View;
import android.widget.OverScroller;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout;
......@@ -14,7 +16,7 @@ import com.mints.goodmoney.utils.LogUtil;
import java.lang.reflect.Field;
/**
* 解决appbarLayout若干问题:
* 解决appbarLayout若干问题:
* (1)快速滑动appbarLayout会出现回弹
* (2)快速滑动appbarLayout到折叠状态下,立马下滑,会出现抖动的问题
* (3)滑动appbarLayout,无法通过手指按下让其停止滑动
......@@ -33,6 +35,14 @@ public class FlingBehavior extends AppBarLayout.Behavior {
public FlingBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
// 处理appbarlayout 无法滑动问题
setDragCallback(new DragCallback() {
@Override
public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
return true;
}
});
}
@Override
......@@ -54,6 +64,7 @@ public class FlingBehavior extends AppBarLayout.Behavior {
/**
* 反射获取私有的flingRunnable 属性,考虑support 28以后变量名修改的问题
*
* @return Field
*/
private Field getFlingRunnableField() throws NoSuchFieldException {
......@@ -70,6 +81,7 @@ public class FlingBehavior extends AppBarLayout.Behavior {
/**
* 反射获取私有的scroller 属性,考虑support 28以后变量名修改的问题
*
* @return Field
*/
private Field getScrollerField() throws NoSuchFieldException {
......@@ -86,9 +98,11 @@ public class FlingBehavior extends AppBarLayout.Behavior {
/**
* 停止appbarLayout的fling事件
*
* @param appBarLayout
*/
private void stopAppbarLayoutFling(AppBarLayout appBarLayout) {
LogUtil.d(TAG, "stopAppbarLayoutFling");
//通过反射拿到HeaderBehavior中的flingRunnable变量
try {
Field flingRunnableField = getFlingRunnableField();
......@@ -106,9 +120,7 @@ public class FlingBehavior extends AppBarLayout.Behavior {
if (overScroller != null && !overScroller.isFinished()) {
overScroller.abortAnimation();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment