提交 9f60e283 编写于 作者: L liqiang

fix ui_picker set value bug

Change-Id: Ia1775527cc2ee389eba61fa7b63fbe352a7932a8
Signed-off-by: Nliqiang <liqiang121@huawei.com>
上级 4de9343f
......@@ -20,113 +20,72 @@
#include "themes/theme_manager.h"
namespace OHOS {
class PickerListScrollListener : public ListScrollListener {
public:
PickerListScrollListener(UIPicker* picker, UIList* list)
: listView_(list),
pickerView_(picker),
selectView_(nullptr),
lastSelectView_(nullptr),
selectIndex_(0),
isInitted_(false)
{
}
virtual ~PickerListScrollListener() {}
void OnItemSelected(int16_t index, UIView* view) override
{
if (!isInitted_) {
return;
}
if ((lastSelectView_ != nullptr) && (listView_ != nullptr) && (pickerView_ != nullptr) && (view != nullptr)) {
lastSelectView_->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetBackgroundTextColor().full);
if (pickerView_->backgroundFontName_ == nullptr) {
static_cast<UILabel*>(lastSelectView_)->SetFontId(pickerView_->backgroundFontId_);
} else {
static_cast<UILabel*>(lastSelectView_)
->SetFont(pickerView_->backgroundFontName_, pickerView_->backgroundFontSize_);
}
view->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetHighlightTextColor().full);
if (pickerView_->highlightFontName_ == nullptr) {
static_cast<UILabel*>(view)->SetFontId(pickerView_->highlightFontId_);
} else {
static_cast<UILabel*>(view)->SetFont(pickerView_->highlightFontName_, pickerView_->highlightFontSize_);
}
lastSelectView_ = view;
selectIndex_ = index;
listView_->Invalidate();
}
PickerListScrollListener::PickerListScrollListener(UIPicker* picker, UIList* list)
: listView_(list),
pickerView_(picker),
selectView_(nullptr),
lastSelectView_(nullptr),
selectIndex_(0),
isInitted_(false) {}
void PickerListScrollListener::OnItemSelected(int16_t index, UIView* view)
{
if (!isInitted_) {
return;
}
void OnScrollEnd(int16_t index, UIView* view) override
{
if ((view == nullptr) || (listView_ == nullptr) || (pickerView_ == nullptr)) {
return;
}
if (lastSelectView_ != nullptr) {
lastSelectView_->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetBackgroundTextColor().full);
if (pickerView_->backgroundFontName_ == nullptr) {
static_cast<UILabel*>(lastSelectView_)->SetFontId(pickerView_->backgroundFontId_);
} else {
static_cast<UILabel*>(lastSelectView_)
->SetFont(pickerView_->backgroundFontName_, pickerView_->backgroundFontSize_);
}
lastSelectView_ = view;
if ((lastSelectView_ != nullptr) && (listView_ != nullptr) && (pickerView_ != nullptr) && (view != nullptr)) {
lastSelectView_->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetBackgroundTextColor().full);
if (pickerView_->backgroundFontName_ == nullptr) {
static_cast<UILabel*>(lastSelectView_)->SetFontId(pickerView_->backgroundFontId_);
} else {
static_cast<UILabel*>(lastSelectView_)
->SetFont(pickerView_->backgroundFontName_, pickerView_->backgroundFontSize_);
}
view->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetHighlightTextColor().full);
if (pickerView_->highlightFontName_ == nullptr) {
static_cast<UILabel*>(view)->SetFontId(pickerView_->highlightFontId_);
} else {
static_cast<UILabel*>(view)->SetFont(pickerView_->highlightFontName_, pickerView_->highlightFontSize_);
}
listView_->Invalidate();
selectView_ = view;
lastSelectView_ = view;
selectIndex_ = index;
if (pickerView_->pickerListener_) {
pickerView_->pickerListener_->OnPickerStoped(*pickerView_);
}
listView_->Invalidate();
}
}
void SetSelectView(UIView* view)
{
selectView_ = view;
lastSelectView_ = view;
void PickerListScrollListener::OnScrollEnd(int16_t index, UIView* view)
{
if ((view == nullptr) || (listView_ == nullptr) || (pickerView_ == nullptr)) {
return;
}
const UIView* GetSelectView() const
{
return selectView_;
if (lastSelectView_ != nullptr) {
lastSelectView_->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetBackgroundTextColor().full);
if (pickerView_->backgroundFontName_ == nullptr) {
static_cast<UILabel*>(lastSelectView_)->SetFontId(pickerView_->backgroundFontId_);
} else {
static_cast<UILabel*>(lastSelectView_)
->SetFont(pickerView_->backgroundFontName_, pickerView_->backgroundFontSize_);
}
lastSelectView_ = view;
}
void SetSelectIndex(uint16_t index)
{
selectIndex_ = index;
view->SetStyle(STYLE_TEXT_COLOR, pickerView_->GetHighlightTextColor().full);
if (pickerView_->highlightFontName_ == nullptr) {
static_cast<UILabel*>(view)->SetFontId(pickerView_->highlightFontId_);
} else {
static_cast<UILabel*>(view)->SetFont(pickerView_->highlightFontName_, pickerView_->highlightFontSize_);
}
uint16_t GetSelectIndex() const
{
return selectIndex_;
}
listView_->Invalidate();
selectView_ = view;
selectIndex_ = index;
void SetInitStatus(bool status)
{
isInitted_ = status;
if (pickerView_->pickerListener_) {
pickerView_->pickerListener_->OnPickerStoped(*pickerView_);
}
private:
UIList* listView_;
UIPicker* pickerView_;
UIView* selectView_;
UIView* lastSelectView_;
uint16_t selectIndex_;
bool isInitted_;
};
}
UIPicker::UIPicker()
: isWidthSet_(false),
......@@ -177,9 +136,8 @@ UIPicker::UIPicker()
#endif
list_.SetLoopState(false);
list_.EnableAutoAlign(true);
PickerListScrollListener* listener = new PickerListScrollListener(this, &list_);
list_.SetScrollStateListener(listener);
listListener_ = static_cast<void*>(listener);
listListener_ = new PickerListScrollListener(this, &list_);
list_.SetScrollStateListener(listListener_);
Add(&list_);
}
......@@ -188,7 +146,7 @@ UIPicker::~UIPicker()
ClearValues();
Remove(&list_);
if (listListener_ != nullptr) {
delete static_cast<PickerListScrollListener*>(listListener_);
delete listListener_;
listListener_ = nullptr;
}
......@@ -244,17 +202,13 @@ bool UIPicker::RefreshValues(int16_t start, int16_t end)
if (!isWidthSet_ || !isHeightSet_ || !itemsHeight_ || ((start == 0) && (end == 0))) {
return false;
}
uint16_t userSelectInndex = static_cast<PickerListScrollListener*>(listListener_)->GetSelectIndex();
uint16_t userSelectIndex = listListener_->GetSelectIndex();
ClearList();
InitTextAdapter();
textAdapter_->SetData(start, end);
maxCount_ = end - start + 1;
RefreshList();
if (userSelectInndex) {
RefreshSelected(userSelectInndex);
} else if (setSelectedIndex_) {
RefreshSelected(setSelectedIndex_);
}
RefreshSelected(userSelectIndex);
return true;
}
......@@ -263,7 +217,7 @@ bool UIPicker::RefreshValues(const char* value[], uint16_t count)
if (value == nullptr || !isWidthSet_ || !isHeightSet_ || !itemsHeight_) {
return false;
}
uint16_t userSelectInndex = static_cast<PickerListScrollListener*>(listListener_)->GetSelectIndex();
uint16_t userSelectIndex = listListener_->GetSelectIndex();
ClearList();
for (uint16_t i = 0; i < count; i++) {
dataList_.PushBack(value[i]);
......@@ -272,11 +226,7 @@ bool UIPicker::RefreshValues(const char* value[], uint16_t count)
textAdapter_->SetData(&dataList_);
maxCount_ = count;
RefreshList();
if (userSelectInndex != 0) {
RefreshSelected(userSelectInndex);
} else if (setSelectedIndex_ != 0) {
RefreshSelected(setSelectedIndex_);
}
RefreshSelected(userSelectIndex);
return true;
}
......@@ -326,10 +276,9 @@ void UIPicker::ClearList()
maxCount_ = 0;
itemsWidth_ = 0;
if (listListener_) {
PickerListScrollListener* listListener = static_cast<PickerListScrollListener*>(listListener_);
listListener->SetSelectView(nullptr);
listListener->SetSelectIndex(0);
listListener->SetInitStatus(false);
listListener_->SetSelectView(nullptr);
listListener_->SetSelectIndex(0);
listListener_->SetInitStatus(false);
}
dataList_.Clear();
}
......@@ -342,18 +291,22 @@ bool UIPicker::SetSelected(uint16_t index)
bool UIPicker::RefreshSelected(uint16_t index)
{
if (itemsHeight_ && (maxCount_ > index) && (list_.GetChildrenHead() != nullptr) && isWidthSet_ && isHeightSet_) {
PickerListScrollListener* listListener = static_cast<PickerListScrollListener*>(listListener_);
listListener->SetInitStatus(false);
if (maxCount_ <= index) {
GRAPHIC_LOGW("Failed to refresh selected since index is beyond range!");
return false;
}
listListener_->SetSelectIndex(index);
if (itemsHeight_ && (list_.GetChildrenHead() != nullptr) && isWidthSet_ && isHeightSet_) {
listListener_->SetInitStatus(false);
// 2: half
int16_t yOffset = (list_.GetHeight() - itemsHeight_) / 2 -
itemsHeight_ * (index - list_.GetChildrenHead()->GetViewIndex());
list_.SetScrollStateListener(nullptr);
list_.ScrollBy(yOffset - list_.GetChildrenHead()->GetY());
list_.SetScrollStateListener(listListener);
listListener->SetScrollState(ListScrollListener::SCROLL_STATE_STOP);
list_.SetScrollStateListener(listListener_);
listListener_->SetScrollState(ListScrollListener::SCROLL_STATE_STOP);
UIView* childView = static_cast<UIView*>(list_.GetChildrenHead());
uint16_t lastSelectIndex = listListener->GetSelectIndex();
uint16_t lastSelectIndex = listListener_->GetSelectIndex();
int16_t viewIndex;
while (childView != nullptr) {
......@@ -373,9 +326,8 @@ bool UIPicker::RefreshSelected(uint16_t index)
} else {
static_cast<UILabel*>(childView)->SetFont(highlightFontName_, highlightFontSize_);
}
listListener->SetSelectView(childView);
listListener->SetSelectIndex(index);
listListener->SetInitStatus(true);
listListener_->SetSelectView(childView);
listListener_->SetInitStatus(true);
}
childView = childView->GetNextSibling();
}
......@@ -387,8 +339,7 @@ bool UIPicker::RefreshSelected(uint16_t index)
uint16_t UIPicker::GetSelected() const
{
PickerListScrollListener* listListener = static_cast<PickerListScrollListener*>(listListener_);
return listListener->GetSelectIndex();
return listListener_->GetSelectIndex();
}
void UIPicker::SetFontId(uint8_t backgroundFontId, uint8_t highlightFontId)
......
......@@ -39,6 +39,52 @@
#include "components/ui_list.h"
namespace OHOS {
class UIPicker;
class PickerListScrollListener : public ListScrollListener {
public:
PickerListScrollListener(UIPicker* picker, UIList* list);
virtual ~PickerListScrollListener() {}
void OnItemSelected(int16_t index, UIView* view) override;
void OnScrollEnd(int16_t index, UIView* view) override;
void SetSelectView(UIView* view)
{
selectView_ = view;
lastSelectView_ = view;
}
const UIView* GetSelectView() const
{
return selectView_;
}
void SetSelectIndex(uint16_t index)
{
selectIndex_ = index;
}
uint16_t GetSelectIndex() const
{
return selectIndex_;
}
void SetInitStatus(bool status)
{
isInitted_ = status;
}
private:
UIList* listView_;
UIPicker* pickerView_;
UIView* selectView_;
UIView* lastSelectView_;
uint16_t selectIndex_;
bool isInitted_;
};
/**
* @brief Defines a picker. Multiple texts or numbers can be put into a sliding list for selection.
* The selected text or numbers are highlighted.
......@@ -489,8 +535,8 @@ protected:
bool isHeightSet_ : 1;
TextAdapter* textAdapter_;
uint16_t maxCount_;
PickerListScrollListener* listListener_;
uint16_t setSelectedIndex_;
private:
friend class PickerListScrollListener;
bool RefreshValues(const char* value[], uint16_t count);
......@@ -517,7 +563,7 @@ private:
List<const char*> dataList_;
bool isSetAdaptered_ : 1;
UIList list_;
void* listListener_;
SelectedListener* pickerListener_;
UITextLanguageDirect direct_;
};
......
......@@ -112,6 +112,7 @@ void UITestUIPicker::UIKit_Picker_Test_Base_001()
picker1_->SetTextColor(Color::Gray(), Color::Red());
picker1_->SetDirect(UITextLanguageDirect::TEXT_DIRECT_RTL);
picker1_->SetValues(g_pickerRange, sizeof(g_pickerRange) / sizeof(g_pickerRange[0]));
picker1_->SetSelected(3); // 3 : default selected value
picker1_->RegisterSelectedListener(this);
positionX_ = picker1_->GetWidth();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册