Skip to content

Commit

Permalink
feat(ohos): list scrollToIndex support scrollAlign for vl
Browse files Browse the repository at this point in the history
  • Loading branch information
etkmao committed Feb 27, 2025
1 parent a42a01a commit 2d2f403
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/api/hippy-react/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docs/api/hippy-vue/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ Hippy 的重点功能,高性能的可复用列表组件,在终端侧会被
> * `xIndex`: number - 滑动到 X 方向的第 xIndex 个 item
> * `yIndex`: number - 滑动到 Y 方向的 yIndex 个 item
> * `animated`: boolean - 滑动过程是否使用动画
> * `scrollAlign`: enum(start,center,end,auto) - 滑动对齐类型 只有`Ohos`支持

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -167,8 +168,12 @@ void ListView::CallImpl(const std::string &method, const std::vector<HippyValue>
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]);
Expand All @@ -180,7 +185,7 @@ void ListView::CallImpl(const std::string &method, const std::vector<HippyValue>
}
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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -476,7 +481,7 @@ void ListView::CheckEndDrag() {
HREventUtils::EVENT_PULL_FOOTER_RELEASED, nullptr);
} else {
auto lastIndex = static_cast<int32_t>(children_.size()) - 1;
listNode_->ScrollToIndex(lastIndex - 1, true, false);
listNode_->ScrollToIndex(lastIndex - 1, true, ARKUI_SCROLL_ALIGNMENT_END);
}
pullAction_ = ScrollAction::None;
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -231,7 +231,7 @@ void WaterfallView::CallImpl(const std::string &method, const std::vector<HippyV
} else if (method == "scrollToContentOffset") {

} else if (method == "scrollToTop"){
listNode_->ScrollToIndex(1, true,true);
listNode_->ScrollToIndex(1, true, ARKUI_SCROLL_ALIGNMENT_START);
} else {
BaseView::CallImpl(method, params, callback);
}
Expand Down Expand Up @@ -330,7 +330,7 @@ void WaterfallView::OnHeadRefreshFinish(int32_t delay){
FOOTSTONE_DLOG(INFO)<<__FUNCTION__<<" delay = "<<delay;
if(delay > 0 ){
//TODO setTimeout(delay)
listNode_->ScrollToIndex(1, true, true);
listNode_->ScrollToIndex(1, true, ARKUI_SCROLL_ALIGNMENT_START);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

0 comments on commit 2d2f403

Please sign in to comment.