fix wrong page slided index

This commit is contained in:
王劲鹏 2021-07-05 12:05:35 +08:00 committed by osborn
parent 84ca53c3b7
commit 666998d3a6
3 changed files with 143 additions and 9 deletions

View File

@ -81,8 +81,14 @@ public class SliderNode extends SuperNode<RecyclerView> {
if (slideAdapter.loop) { if (slideAdapter.loop) {
if (position == 0) { if (position == 0) {
recyclerView.scrollToPosition(slideAdapter.itemCount); recyclerView.scrollToPosition(slideAdapter.itemCount);
position = slideAdapter.itemCount - 1;
} else if (position == slideAdapter.itemCount + 1) { } else if (position == slideAdapter.itemCount + 1) {
recyclerView.scrollToPosition(1); recyclerView.scrollToPosition(1);
position = 0;
} else {
position = position - 1;
} }
} }
@ -202,10 +208,19 @@ public class SliderNode extends SuperNode<RecyclerView> {
int page = params.getProperty("page").asNumber().toInt(); int page = params.getProperty("page").asNumber().toInt();
boolean smooth = params.getProperty("smooth").asBoolean().value(); boolean smooth = params.getProperty("smooth").asBoolean().value();
if (smooth) { if (smooth) {
if (slideAdapter.loop) {
mView.smoothScrollToPosition(page + 1);
} else {
mView.smoothScrollToPosition(page); mView.smoothScrollToPosition(page);
}
} else {
if (slideAdapter.loop) {
mView.scrollToPosition(page + 1);
} else { } else {
mView.scrollToPosition(page); mView.scrollToPosition(page);
} }
}
if (!TextUtils.isEmpty(onPageSlidedFuncId)) { if (!TextUtils.isEmpty(onPageSlidedFuncId)) {
callJSResponse(onPageSlidedFuncId, page); callJSResponse(onPageSlidedFuncId, page);
lastPosition = page; lastPosition = page;
@ -216,6 +231,11 @@ public class SliderNode extends SuperNode<RecyclerView> {
@DoricMethod @DoricMethod
public int getSlidedPage() { public int getSlidedPage() {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mView.getLayoutManager(); LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mView.getLayoutManager();
return linearLayoutManager != null ? linearLayoutManager.findFirstVisibleItemPosition() : 0; int pageIndex = linearLayoutManager != null ? linearLayoutManager.findFirstVisibleItemPosition() : 0;
if (slideAdapter.loop) {
return pageIndex - 1;
} else {
return pageIndex;
}
} }
} }

View File

@ -1,4 +1,4 @@
import { Group, Panel, text, gravity, Gravity, Color, LayoutSpec, vlayout, slider, slideItem, image, layoutConfig, ScaleType, switchView, hlayout } from "doric"; import { Group, Panel, text, gravity, Gravity, Color, LayoutSpec, vlayout, slider, slideItem, image, layoutConfig, ScaleType, switchView, hlayout, modal, input, stack, InputType } from "doric";
import { colors } from "./utils"; import { colors } from "./utils";
const imageUrls = [ const imageUrls = [
@ -11,6 +11,13 @@ const imageUrls = [
'http://calonye.com/wp-content/uploads/2015/08/0-wx_fmtgiftpwebpwxfrom5wx_lazy1-9.gif', 'http://calonye.com/wp-content/uploads/2015/08/0-wx_fmtgiftpwebpwxfrom5wx_lazy1-9.gif',
'http://hbimg.b0.upaiyun.com/ca29ea125b7f2d580f573e48eb594b1ef509757f34a08-m0hK45_fw658', 'http://hbimg.b0.upaiyun.com/ca29ea125b7f2d580f573e48eb594b1ef509757f34a08-m0hK45_fw658',
'https://misc.aotu.io/ONE-SUNDAY/SteamEngine.png', 'https://misc.aotu.io/ONE-SUNDAY/SteamEngine.png',
'http://b.hiphotos.baidu.com/image/pic/item/908fa0ec08fa513db777cf78376d55fbb3fbd9b3.jpg',
'http://f.hiphotos.baidu.com/image/pic/item/0e2442a7d933c8956c0e8eeadb1373f08202002a.jpg',
'http://f.hiphotos.baidu.com/image/pic/item/b151f8198618367aa7f3cc7424738bd4b31ce525.jpg',
'http://b.hiphotos.baidu.com/image/pic/item/0eb30f2442a7d9337119f7dba74bd11372f001e0.jpg',
'http://a.hiphotos.baidu.com/image/h%3D300/sign=b38f3fc35b0fd9f9bf175369152cd42b/9a504fc2d5628535bdaac29e9aef76c6a6ef63c2.jpg',
'http://h.hiphotos.baidu.com/image/pic/item/810a19d8bc3eb1354c94a704ac1ea8d3fd1f4439.jpg',
'http://calonye.com/wp-content/uploads/2015/08/0-wx_fmtgiftpwebpwxfrom5wx_lazy1-9.gif',
] ]
@Entry @Entry
class SliderPanel extends Panel { class SliderPanel extends Panel {
@ -29,11 +36,30 @@ class SliderPanel extends Panel {
} }
}) })
}, },
loop: true,
layoutConfig: { layoutConfig: {
widthSpec: LayoutSpec.MOST, widthSpec: LayoutSpec.MOST,
heightSpec: LayoutSpec.MOST, heightSpec: LayoutSpec.MOST,
weight: 1, weight: 1,
}, },
onPageSlided: (index) => {
modal(context).toast(index.toString())
}
})
let pageIndexInput = input({
backgroundColor: Color.WHITE,
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.FIT,
},
hintText: "Page index",
inputType: InputType.Number,
textAlignment: Gravity.Center,
border: {
width: 1,
color: Color.GRAY,
},
}) })
rootView.addChild(vlayout([ rootView.addChild(vlayout([
@ -62,7 +88,7 @@ class SliderPanel extends Panel {
height: 50, height: 50,
}), }),
switchView({ switchView({
state: false, state: true,
onSwitch: (state) => { onSwitch: (state) => {
pager.loop = state pager.loop = state
}, },
@ -97,6 +123,67 @@ class SliderPanel extends Panel {
space: 10, space: 10,
backgroundColor: Color.RED, backgroundColor: Color.RED,
}), }),
hlayout([
text({
text: "Slide to page",
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
textSize: 20,
textColor: Color.BLACK,
textAlignment: gravity().center(),
height: 50,
}),
pageIndexInput,
text({
text: "Go!",
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
padding: {
left: 10,
right: 10
},
textSize: 20,
textColor: Color.BLACK,
backgroundColor: Color.WHITE,
textAlignment: gravity().center(),
height: 40,
onClick: async () => {
let index = await pageIndexInput.getText(context);
await pager.slidePage(context, parseInt(index), true)
}
}),
], {
layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT),
gravity: gravity().center(),
space: 10,
backgroundColor: Color.RED,
}),
hlayout([
text({
text: "getSlidedPage",
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.JUST,
},
textSize: 20,
textColor: Color.BLACK,
textAlignment: gravity().center(),
height: 50,
onClick: async () => {
let index = await pager.getSlidedPage(context);
modal(context).toast(index.toString())
}
}),
], {
layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT),
gravity: gravity().center(),
space: 10,
backgroundColor: Color.RED,
}),
pager, pager,
]).also(it => { ]).also(it => {
it.layoutConfig = { it.layoutConfig = {

View File

@ -96,9 +96,22 @@ - (void)blendView:(UICollectionView *)view forPropName:(NSString *)name propValu
self.onPageSelectedFuncId = prop; self.onPageSelectedFuncId = prop;
} else if ([@"loop" isEqualToString:name]) { } else if ([@"loop" isEqualToString:name]) {
self.loop = [prop boolValue]; self.loop = [prop boolValue];
__weak typeof(self) _self = self;
if (self.loop) { if (self.loop) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(_self) self = _self;
[self.view reloadData]; [self.view reloadData];
[self.view setContentOffset:CGPointMake(1 * self.view.width, self.view.contentOffset.y) animated:false]; [self.view setContentOffset:CGPointMake(1 * self.view.width, self.view.contentOffset.y) animated:false];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(_self) self = _self;
[self.view reloadData];
[self.view setContentOffset:CGPointMake(0, self.view.contentOffset.y) animated:false];
});
} }
} else if ([@"bounces" isEqualToString:name]) { } else if ([@"bounces" isEqualToString:name]) {
self.view.bounces = [prop boolValue]; self.view.bounces = [prop boolValue];
@ -226,8 +239,12 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (self.loop) { if (self.loop) {
if (pageIndex == 0) { if (pageIndex == 0) {
[self.view setContentOffset:CGPointMake(self.itemCount * self.view.width, self.view.contentOffset.y) animated:false]; [self.view setContentOffset:CGPointMake(self.itemCount * self.view.width, self.view.contentOffset.y) animated:false];
pageIndex = self.itemCount - 1;
} else if (pageIndex == self.itemCount + 1) { } else if (pageIndex == self.itemCount + 1) {
[self.view setContentOffset:CGPointMake(1 * self.view.width, self.view.contentOffset.y) animated:false]; [self.view setContentOffset:CGPointMake(1 * self.view.width, self.view.contentOffset.y) animated:false];
pageIndex = 0;
} else {
pageIndex = pageIndex - 1;
} }
} }
@ -242,7 +259,13 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- (void)slidePage:(NSDictionary *)params withPromise:(DoricPromise *)promise { - (void)slidePage:(NSDictionary *)params withPromise:(DoricPromise *)promise {
NSUInteger pageIndex = [params[@"page"] unsignedIntegerValue]; NSUInteger pageIndex = [params[@"page"] unsignedIntegerValue];
BOOL smooth = [params[@"smooth"] boolValue]; BOOL smooth = [params[@"smooth"] boolValue];
if (self.loop) {
[self.view setContentOffset:CGPointMake((pageIndex + 1) * self.view.width, self.view.contentOffset.y) animated:smooth];
} else {
[self.view setContentOffset:CGPointMake(pageIndex * self.view.width, self.view.contentOffset.y) animated:smooth]; [self.view setContentOffset:CGPointMake(pageIndex * self.view.width, self.view.contentOffset.y) animated:smooth];
}
[promise resolve:nil]; [promise resolve:nil];
self.lastPosition = pageIndex; self.lastPosition = pageIndex;
if (self.onPageSelectedFuncId && self.onPageSelectedFuncId.length > 0) { if (self.onPageSelectedFuncId && self.onPageSelectedFuncId.length > 0) {
@ -252,7 +275,11 @@ - (void)slidePage:(NSDictionary *)params withPromise:(DoricPromise *)promise {
- (NSNumber *)getSlidedPage { - (NSNumber *)getSlidedPage {
NSUInteger pageIndex = (NSUInteger) (self.view.contentOffset.x / self.view.width); NSUInteger pageIndex = (NSUInteger) (self.view.contentOffset.x / self.view.width);
if (self.loop) {
return @(pageIndex - 1);
} else {
return @(pageIndex); return @(pageIndex);
} }
}
@end @end