diff --git a/docs/api/hippy-react/components.md b/docs/api/hippy-react/components.md index 59e903df54b..3c61fc7a103 100644 --- a/docs/api/hippy-react/components.md +++ b/docs/api/hippy-react/components.md @@ -166,6 +166,7 @@ import icon from './qb_icon_new.png'; > * `xIndex`: number - 滑动到 X 方向的第 xIndex 个 item > * `yIndex`: number - 滑动到 Y 方向的 yIndex 个 item > * `animated`: boolean - 滑动过程是否使用动画 +> * `scrollAlign`: enum(start,center,end,auto) - 滑动对齐类型 只有`Ohos`支持 ### collapsePullHeader diff --git a/docs/api/hippy-vue/components.md b/docs/api/hippy-vue/components.md index 21bb0090935..85ee6fd1fff 100644 --- a/docs/api/hippy-vue/components.md +++ b/docs/api/hippy-vue/components.md @@ -446,6 +446,7 @@ Hippy 的重点功能,高性能的可复用列表组件,在终端侧会被 > * `xIndex`: number - 滑动到 X 方向的第 xIndex 个 item > * `yIndex`: number - 滑动到 Y 方向的 yIndex 个 item > * `animated`: boolean - 滑动过程是否使用动画 +> * `scrollAlign`: enum(start,center,end,auto) - 滑动对齐类型 只有`Ohos`支持 --- diff --git a/framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/arkui/list_node.h b/framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/arkui/list_node.h index a98eb1a7d6a..dbc3066a71d 100644 --- a/framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/arkui/list_node.h +++ b/framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/arkui/list_node.h @@ -67,7 +67,7 @@ class ListNode : public ArkUINode { HRPoint GetScrollOffset(); void ScrollTo(float offsetX, float offsetY, bool animated); - void ScrollToIndex(int32_t index, bool animated, bool isScrollAlignStart); + void ScrollToIndex(int32_t index, bool animated, ArkUI_ScrollAlignment align); void SetListDirection(bool isVertical); void SetListInitialIndex(int32_t index); void SetScrollEdgeEffect(bool hasEffect); diff --git a/framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/utils/hr_convert_utils.h b/framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/utils/hr_convert_utils.h index bf1e0103045..ed9adb1ed7d 100644 --- a/framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/utils/hr_convert_utils.h +++ b/framework/ohos/src/main/cpp/impl/renderer/native/include/renderer/utils/hr_convert_utils.h @@ -41,6 +41,7 @@ class HRConvertUtils { static ArkUI_ImageSize BackgroundImageSizeToArk(std::string &str); static float ToDegrees(const HippyValue &value); static bool TransformToArk(HippyValueArrayType &valueArray, HRTransform &transform); + static ArkUI_ScrollAlignment ScrollAlignmentToArk(const HippyValue &value); }; } // namespace native diff --git a/framework/ohos/src/main/cpp/impl/renderer/native/src/arkui/list_node.cc b/framework/ohos/src/main/cpp/impl/renderer/native/src/arkui/list_node.cc index 7c6f86ffe59..6afc59ff1eb 100644 --- a/framework/ohos/src/main/cpp/impl/renderer/native/src/arkui/list_node.cc +++ b/framework/ohos/src/main/cpp/impl/renderer/native/src/arkui/list_node.cc @@ -85,10 +85,10 @@ void ListNode::ScrollTo(float offsetX, float offsetY, bool animated) { SetSubAttributeFlag((uint32_t)AttributeFlag::SCROLL_OFFSET); } -void ListNode::ScrollToIndex(int32_t index, bool animated, bool isScrollAlignStart) { +void ListNode::ScrollToIndex(int32_t index, bool animated, ArkUI_ScrollAlignment align) { ArkUI_NumberValue value[] = {{.i32 = index}, {.i32 = animated ? 1 : 0}, - {.i32 = isScrollAlignStart ? ARKUI_SCROLL_ALIGNMENT_START : ARKUI_SCROLL_ALIGNMENT_END}}; + {.i32 = align}}; ArkUI_AttributeItem item = {value, sizeof(value) / sizeof(ArkUI_NumberValue), nullptr, nullptr}; MaybeThrow(NativeNodeApi::GetInstance()->setAttribute(nodeHandle_, NODE_LIST_SCROLL_TO_INDEX, &item)); SetSubAttributeFlag((uint32_t)AttributeFlag::LIST_SCROLL_TO_INDEX); diff --git a/framework/ohos/src/main/cpp/impl/renderer/native/src/components/list_view.cc b/framework/ohos/src/main/cpp/impl/renderer/native/src/components/list_view.cc index 52f936b6f33..ee0f0252f01 100644 --- a/framework/ohos/src/main/cpp/impl/renderer/native/src/components/list_view.cc +++ b/framework/ohos/src/main/cpp/impl/renderer/native/src/components/list_view.cc @@ -23,6 +23,7 @@ #include "renderer/components/list_view.h" #include "renderer/components/list_item_view.h" #include "renderer/components/refresh_wrapper_view.h" +#include "renderer/utils/hr_convert_utils.h" #include "renderer/utils/hr_event_utils.h" #include "renderer/utils/hr_pixel_utils.h" #include "renderer/utils/hr_value_utils.h" @@ -167,8 +168,12 @@ void ListView::CallImpl(const std::string &method, const std::vector auto xIndex = HRValueUtils::GetInt32(params[0]); auto yIndex = HRValueUtils::GetInt32(params[1]); auto animated = HRValueUtils::GetBool(params[2], false); + ArkUI_ScrollAlignment align = ARKUI_SCROLL_ALIGNMENT_START; + if (params.size() >= 4) { + align = HRConvertUtils::ScrollAlignmentToArk(params[3]); + } auto index = isVertical_ ? yIndex : xIndex; - listNode_->ScrollToIndex(hasPullHeader_ ? index + 1 : index, animated, true); + listNode_->ScrollToIndex(hasPullHeader_ ? index + 1 : index, animated, align); } else if (method == "scrollToContentOffset") { auto xOffset = HRValueUtils::GetFloat(params[0]); auto yOffset = HRValueUtils::GetFloat(params[1]); @@ -180,7 +185,7 @@ void ListView::CallImpl(const std::string &method, const std::vector } listNode_->ScrollTo(xOffset, yOffset, animated); } else if (method == "scrollToTop") { - listNode_->ScrollToIndex(hasPullHeader_ ? 1 : 0, true, true); + listNode_->ScrollToIndex(hasPullHeader_ ? 1 : 0, true, ARKUI_SCROLL_ALIGNMENT_START); } else { BaseView::CallImpl(method, params, callback); } @@ -241,7 +246,7 @@ void ListView::UpdateRenderViewFrameImpl(const HRRect &frame, const HRPadding &p } void ListView::ScrollToIndex(int32_t index, bool animated) { - listNode_->ScrollToIndex(index, animated, true); + listNode_->ScrollToIndex(index, animated, ARKUI_SCROLL_ALIGNMENT_START); } void ListView::SetScrollNestedMode(ArkUI_ScrollNestedMode scrollForward, ArkUI_ScrollNestedMode scrollBackward) { @@ -467,7 +472,7 @@ void ListView::CheckEndDrag() { HREventUtils::SendComponentEvent(headerView_->GetCtx(), headerView_->GetTag(), HREventUtils::EVENT_PULL_HEADER_RELEASED, nullptr); } else { - listNode_->ScrollToIndex(1, true, true); + listNode_->ScrollToIndex(1, true, ARKUI_SCROLL_ALIGNMENT_START); } pullAction_ = ScrollAction::None; } else if (footerView_ && pullAction_ == ScrollAction::PullFooter) { @@ -476,7 +481,7 @@ void ListView::CheckEndDrag() { HREventUtils::EVENT_PULL_FOOTER_RELEASED, nullptr); } else { auto lastIndex = static_cast(children_.size()) - 1; - listNode_->ScrollToIndex(lastIndex - 1, true, false); + listNode_->ScrollToIndex(lastIndex - 1, true, ARKUI_SCROLL_ALIGNMENT_END); } pullAction_ = ScrollAction::None; } @@ -495,7 +500,7 @@ void ListView::CheckPullOnItemVisibleAreaChange(int32_t index, bool isVisible, f headerViewFullVisible_ = false; } } else { - listNode_->ScrollToIndex(1, true, true); + listNode_->ScrollToIndex(1, true, ARKUI_SCROLL_ALIGNMENT_START); } } else { headerViewFullVisible_ = false; @@ -513,7 +518,7 @@ void ListView::CheckPullOnItemVisibleAreaChange(int32_t index, bool isVisible, f footerViewFullVisible_ = false; } } else { - listNode_->ScrollToIndex(lastIndex - 1, true, false); + listNode_->ScrollToIndex(lastIndex - 1, true, ARKUI_SCROLL_ALIGNMENT_END); } } else { footerViewFullVisible_ = false; diff --git a/framework/ohos/src/main/cpp/impl/renderer/native/src/components/waterfall_view.cc b/framework/ohos/src/main/cpp/impl/renderer/native/src/components/waterfall_view.cc index 36117b6d616..dbfbd72d58c 100644 --- a/framework/ohos/src/main/cpp/impl/renderer/native/src/components/waterfall_view.cc +++ b/framework/ohos/src/main/cpp/impl/renderer/native/src/components/waterfall_view.cc @@ -168,11 +168,11 @@ void WaterfallView::HandleOnChildrenUpdated() { listNode_->InsertChild(bannerListNode_.get(), 1); headerView->GetLocalRootArkUINode()->SetNodeDelegate(this); headerView->GetLocalRootArkUINode()->SetItemIndex(0); - listNode_->ScrollToIndex(1, true, true); + listNode_->ScrollToIndex(1, true, ARKUI_SCROLL_ALIGNMENT_START); } else{ listNode_->InsertChild(bannerListNode_.get(), 0); - listNode_->ScrollToIndex(0, true, true); + listNode_->ScrollToIndex(0, true, ARKUI_SCROLL_ALIGNMENT_START); } listNode_->AddChild(flowListNode_.get()); @@ -231,7 +231,7 @@ void WaterfallView::CallImpl(const std::string &method, const std::vectorScrollToIndex(1, true,true); + listNode_->ScrollToIndex(1, true, ARKUI_SCROLL_ALIGNMENT_START); } else { BaseView::CallImpl(method, params, callback); } @@ -330,7 +330,7 @@ void WaterfallView::OnHeadRefreshFinish(int32_t delay){ FOOTSTONE_DLOG(INFO)<<__FUNCTION__<<" delay = "< 0 ){ //TODO setTimeout(delay) - listNode_->ScrollToIndex(1, true, true); + listNode_->ScrollToIndex(1, true, ARKUI_SCROLL_ALIGNMENT_START); } } diff --git a/framework/ohos/src/main/cpp/impl/renderer/native/src/utils/hr_convert_utils.cc b/framework/ohos/src/main/cpp/impl/renderer/native/src/utils/hr_convert_utils.cc index eec14aee258..48c283857d3 100644 --- a/framework/ohos/src/main/cpp/impl/renderer/native/src/utils/hr_convert_utils.cc +++ b/framework/ohos/src/main/cpp/impl/renderer/native/src/utils/hr_convert_utils.cc @@ -22,6 +22,7 @@ #include "renderer/utils/hr_convert_utils.h" #include "footstone/logging.h" +#include "renderer/utils/hr_value_utils.h" namespace hippy { inline namespace render { @@ -211,6 +212,27 @@ bool HRConvertUtils::TransformToArk(HippyValueArrayType &valueArray, HRTransform return true; } +ArkUI_ScrollAlignment HRConvertUtils::ScrollAlignmentToArk(const HippyValue &value) { + if (value.IsNumber()) { + auto ret = HRValueUtils::GetInt32(value); + if (ret >= ARKUI_SCROLL_ALIGNMENT_START && ret <= ARKUI_SCROLL_ALIGNMENT_AUTO) { + return (ArkUI_ScrollAlignment)ret; + } + } else if (value.IsString()) { + std::string str = value.ToStringChecked(); + if (str == "start") { + return ARKUI_SCROLL_ALIGNMENT_START; + } else if (str == "center") { + return ARKUI_SCROLL_ALIGNMENT_CENTER; + } else if (str == "end") { + return ARKUI_SCROLL_ALIGNMENT_END; + } else if (str == "auto") { + return ARKUI_SCROLL_ALIGNMENT_AUTO; + } + } + return ARKUI_SCROLL_ALIGNMENT_START; +} + } // namespace native } // namespace render } // namespace hippy