fix scrollable false prevent scrollToPosition

This commit is contained in:
王劲鹏 2022-06-29 15:06:18 +08:00 committed by osborn
parent e6cce1b60f
commit 59788fd658
4 changed files with 132 additions and 0 deletions

View File

@ -15,11 +15,13 @@
*/ */
package pub.doric.shader.flowlayout; package pub.doric.shader.flowlayout;
import android.content.Context;
import android.graphics.Rect; import android.graphics.Rect;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager; import androidx.recyclerview.widget.StaggeredGridLayoutManager;
@ -82,6 +84,17 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
} }
return super.canScrollVertically(); return super.canScrollVertically();
} }
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
if (scrollable) {
super.smoothScrollToPosition(recyclerView, state, position);
} else {
DoricLinearSmoothScroller linearSmoothScroller = new DoricLinearSmoothScroller(recyclerView.getContext());
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
}; };
private int columnSpace = 0; private int columnSpace = 0;
private int rowSpace = 0; private int rowSpace = 0;
@ -395,4 +408,26 @@ public class FlowLayoutNode extends SuperNode<RecyclerView> implements IDoricScr
onScrollEndFuncId = null; onScrollEndFuncId = null;
flowAdapter.renderItemFuncId = null; flowAdapter.renderItemFuncId = null;
} }
private static class DoricLinearSmoothScroller extends LinearSmoothScroller {
public DoricLinearSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDyToMakeVisible(View view, int snapPreference) {
final RecyclerView.LayoutManager layoutManager = getLayoutManager();
if (layoutManager == null) {
return 0;
}
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
final int top = layoutManager.getDecoratedTop(view) - params.topMargin;
final int bottom = layoutManager.getDecoratedBottom(view) + params.bottomMargin;
final int start = layoutManager.getPaddingTop();
final int end = layoutManager.getHeight() - layoutManager.getPaddingBottom();
return calculateDtToFit(top, bottom, start, end, snapPreference);
}
}
} }

View File

@ -16,6 +16,7 @@
package pub.doric.shader.list; package pub.doric.shader.list;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.GestureDetector; import android.view.GestureDetector;
@ -26,6 +27,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
@ -136,6 +138,17 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
} }
return super.canScrollVertically(); return super.canScrollVertically();
} }
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
if (scrollable) {
super.smoothScrollToPosition(recyclerView, state, position);
} else {
DoricLinearSmoothScroller linearSmoothScroller = new DoricLinearSmoothScroller(recyclerView.getContext());
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
}); });
recyclerView.setAdapter(this.listAdapter); recyclerView.setAdapter(this.listAdapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@ -537,4 +550,25 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
void onMoved(int fromPos, int toPos); void onMoved(int fromPos, int toPos);
} }
private static class DoricLinearSmoothScroller extends LinearSmoothScroller {
public DoricLinearSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDyToMakeVisible(View view, int snapPreference) {
final RecyclerView.LayoutManager layoutManager = getLayoutManager();
if (layoutManager == null) {
return 0;
}
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
final int top = layoutManager.getDecoratedTop(view) - params.topMargin;
final int bottom = layoutManager.getDecoratedBottom(view) + params.bottomMargin;
final int start = layoutManager.getPaddingTop();
final int end = layoutManager.getHeight() - layoutManager.getPaddingBottom();
return calculateDtToFit(top, bottom, start, end, snapPreference);
}
}
} }

View File

@ -15,11 +15,14 @@
*/ */
package pub.doric.shader.slider; package pub.doric.shader.slider;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.PagerSnapHelper; import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -58,6 +61,7 @@ public class SliderNode extends SuperNode<RecyclerView> {
protected RecyclerView build() { protected RecyclerView build() {
RecyclerView recyclerView = new RecyclerView(getContext()); RecyclerView recyclerView = new RecyclerView(getContext());
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()) { final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()) {
@Override @Override
public boolean canScrollHorizontally() { public boolean canScrollHorizontally() {
@ -66,7 +70,19 @@ public class SliderNode extends SuperNode<RecyclerView> {
} }
return super.canScrollHorizontally(); return super.canScrollHorizontally();
} }
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
if (scrollable) {
super.smoothScrollToPosition(recyclerView, state, position);
} else {
DoricLinearSmoothScroller linearSmoothScroller = new DoricLinearSmoothScroller(recyclerView.getContext());
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
}; };
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager); recyclerView.setLayoutManager(layoutManager);
final PagerSnapHelper snapHelper = new PagerSnapHelper(); final PagerSnapHelper snapHelper = new PagerSnapHelper();
@ -165,6 +181,7 @@ public class SliderNode extends SuperNode<RecyclerView> {
if (jsObject.propertySet().size() > 1 || !jsObject.propertySet().contains("subviews")) { if (jsObject.propertySet().size() > 1 || !jsObject.propertySet().contains("subviews")) {
if (mView != null) { if (mView != null) {
mView.post(new Runnable() { mView.post(new Runnable() {
@SuppressLint("NotifyDataSetChanged")
@Override @Override
public void run() { public void run() {
slideAdapter.itemCount = itemCount; slideAdapter.itemCount = itemCount;
@ -257,4 +274,26 @@ public class SliderNode extends SuperNode<RecyclerView> {
onPageSlidedFuncId = null; onPageSlidedFuncId = null;
renderPageFuncId = null; renderPageFuncId = null;
} }
private static class DoricLinearSmoothScroller extends LinearSmoothScroller {
public DoricLinearSmoothScroller(Context context) {
super(context);
}
@Override
public int calculateDxToMakeVisible(View view, int snapPreference) {
final RecyclerView.LayoutManager layoutManager = getLayoutManager();
if (layoutManager == null) {
return 0;
}
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
view.getLayoutParams();
final int left = layoutManager.getDecoratedLeft(view) - params.leftMargin;
final int right = layoutManager.getDecoratedRight(view) + params.rightMargin;
final int start = layoutManager.getPaddingLeft();
final int end = layoutManager.getWidth() - layoutManager.getPaddingRight();
return calculateDtToFit(left, right, start, end, snapPreference);
}
}
} }

View File

@ -99,6 +99,30 @@ class SliderPanel extends Panel {
space: 10, space: 10,
backgroundColor: Color.RED, backgroundColor: Color.RED,
}), }),
hlayout([
text({
text: "Scrollable",
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
textSize: 20,
textColor: Color.BLACK,
textAlignment: gravity().center(),
height: 50,
}),
switchView({
state: true,
onSwitch: (state) => {
pager.scrollable = state
},
}),
], {
layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT),
gravity: gravity().center(),
space: 10,
backgroundColor: Color.RED,
}),
hlayout([ hlayout([
text({ text({
text: "Bounces", text: "Bounces",