feat:fix shaking when drag

This commit is contained in:
pengfei.zhou 2019-11-25 18:54:27 +08:00
parent d3b2d4c8bd
commit a47b3682db
4 changed files with 35 additions and 15 deletions

View File

@ -34,6 +34,7 @@ import java.util.List;
import pub.doric.DoricActivity; import pub.doric.DoricActivity;
import pub.doric.devkit.ui.DemoDebugActivity; import pub.doric.devkit.ui.DemoDebugActivity;
import pub.doric.pullable.DoricSwipeLayout;
import pub.doric.utils.DoricUtils; import pub.doric.utils.DoricUtils;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@ -43,6 +44,13 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
final DoricSwipeLayout swipeLayout = findViewById(R.id.swipe_layout);
swipeLayout.setOnRefreshListener(new DoricSwipeLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeLayout.setRefreshing(false);
}
});
RecyclerView recyclerView = findViewById(R.id.root); RecyclerView recyclerView = findViewById(R.id.root);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setLayoutManager(new LinearLayoutManager(this));
try { try {

View File

@ -1,8 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<pub.doric.pullable.DoricSwipeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/root" android:id="@+id/root"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" /> tools:context=".MainActivity" />
</pub.doric.pullable.DoricSwipeLayout>

View File

@ -26,8 +26,8 @@ import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape; import android.graphics.drawable.shapes.OvalShape;
import android.view.View; import android.view.View;
import android.view.animation.Animation; import android.view.animation.Animation;
import android.widget.ImageView;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
@ -36,7 +36,7 @@ import androidx.core.view.ViewCompat;
* called before the animation is actually complete and support shadows on older * called before the animation is actually complete and support shadows on older
* platforms. * platforms.
*/ */
class CircleImageView extends ImageView { class CircleImageView extends AppCompatImageView {
private static final int KEY_SHADOW_COLOR = 0x1E000000; private static final int KEY_SHADOW_COLOR = 0x1E000000;
private static final int FILL_SHADOW_COLOR = 0x3D000000; private static final int FILL_SHADOW_COLOR = 0x3D000000;

View File

@ -51,7 +51,7 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
@VisibleForTesting @VisibleForTesting
static final int CIRCLE_DIAMETER_LARGE = 56; static final int CIRCLE_DIAMETER_LARGE = 56;
private static final String LOG_TAG = androidx.swiperefreshlayout.widget.SwipeRefreshLayout.class.getSimpleName(); private static final String LOG_TAG = DoricSwipeLayout.class.getSimpleName();
private static final int MAX_ALPHA = 255; private static final int MAX_ALPHA = 255;
private static final int STARTING_PROGRESS_ALPHA = (int) (.3f * MAX_ALPHA); private static final int STARTING_PROGRESS_ALPHA = (int) (.3f * MAX_ALPHA);
@ -587,16 +587,19 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
if (mTarget == null) { if (mTarget == null) {
return; return;
} }
int circleWidth = mCircleView.getMeasuredWidth();
int circleHeight = mCircleView.getMeasuredHeight();
mCircleView.layout((width / 2 - circleWidth / 2), mCurrentTargetOffsetTop,
(width / 2 + circleWidth / 2), mCurrentTargetOffsetTop + circleHeight);
final View child = mTarget; final View child = mTarget;
final int childLeft = getPaddingLeft(); final int childLeft = getPaddingLeft();
final int childTop = getPaddingTop(); final int childTop = getPaddingTop() + mCircleView.getBottom();
final int childWidth = width - getPaddingLeft() - getPaddingRight(); final int childWidth = width - getPaddingLeft() - getPaddingRight();
final int childHeight = height - getPaddingTop() - getPaddingBottom(); final int childHeight = height - getPaddingTop() - getPaddingBottom();
child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
int circleWidth = mCircleView.getMeasuredWidth();
int circleHeight = mCircleView.getMeasuredHeight();
mCircleView.layout((width / 2 - circleWidth / 2), mCurrentTargetOffsetTop,
(width / 2 + circleWidth / 2), mCurrentTargetOffsetTop + circleHeight);
} }
@Override @Override
@ -756,9 +759,11 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
consumed[1] = dy - (int) mTotalUnconsumed; consumed[1] = dy - (int) mTotalUnconsumed;
mTotalUnconsumed = 0; mTotalUnconsumed = 0;
} else { } else {
if (dy > 3) {
mTotalUnconsumed -= dy; mTotalUnconsumed -= dy;
consumed[1] = dy; consumed[1] = dy;
} }
}
moveSpinner(mTotalUnconsumed); moveSpinner(mTotalUnconsumed);
} }
@ -1179,4 +1184,6 @@ public class DoricSwipeLayout extends ViewGroup implements NestedScrollingParent
*/ */
boolean canChildScrollUp(@NonNull DoricSwipeLayout parent, @Nullable View child); boolean canChildScrollUp(@NonNull DoricSwipeLayout parent, @Nullable View child);
} }
} }