UCLASS()
class MyGAME_API UMyWidget : public UCommonUserWidget
{
GENERATED_BODY()
virtual void NativeOnInitialized() override;
void OnGetItemChildren(UObject* Item, TArray<UObject*>& OutChildren);
UPROPERTY(meta=(BindWidget))
TObjectPtr<UTreeView> TreeView;
};
void UMyWidget ::NativeOnInitialized()
{
Super::NativeOnInitialized();
TreeView->SetOnGetItemChildren(this, &ThisClass::OnGetItemChildren);
}
void UMyWidget ::OnGetItemChildren(UObject* Item, TArray<UObject*>& OutChildren)
{
if (UMyListEntry* Entry = Cast<UMyListEntry>(Item))
{
OutChildren.Empty();
OutChildren.Append(Entry->Children);
}
}
UCLASS()
class MyGAME_API UMyListEntry : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Friend")
bool bGroup = false;
UPROPERTY(BlueprintReadWrite, Category="Friend List")
int64 GroupId;
UPROPERTY(BlueprintReadWrite, Category="Friend List")
FText GroupName;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Friend")
int32 Depth = 0;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Friend")
FPlayerInfo Info;// Data
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Friend")
bool bExpanded = true;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Friend")
TArray<TObjectPtr<UMyListEntry >> Children;
};
UCLASS()
class MyGAME_API UMyListEntryWidget : public IUserObjectListEntry
{
GENERATED_BODY()
protected:
virtual void NativeOnListItemObjectSet(UObject* ListItemObject) override;
void SetEntryData(UMyListEntry * Entry);
}
void UMyListEntryWidget ::NativeOnListItemObjectSet(UObject* ListItemObject)
{
IUserObjectListEntry::NativeOnListItemObjectSet(ListItemObject);
if (UMyListEntry * ListEntry= Cast<UMyListEntry >(ListItemObject))
{
SetEntryData(ListEntry);
}
}
void UMyListEntryWidget ::SetEntryData(UMyListEntry * Entry)
{
// If it's a group, set the group name, otherwise it's a list of children under the group
}
class MyGAME_API UMySubstem : public UGameInstanceSubsystem
{
UPROPERTY(Transient)
TArray<UMyListEntry*> CachedList;
}
Set Data To UMyWidget
TreeView->SetListItems(MySubsystem->GetCachedListEntrys());
I have a maximum of 2 levels, one is a group and one is a sublist
At present, there is no problem, but when I click on the group widget and then expand the list, that is to say, the sublist widget under the group is in the expanded state, at this time I update the data of the list item, and then call the method, the content displayed by the expanded item under the group does not change, until I click on the group, collapse and expand again, the list widget under the group will use the new data, how to solve this problem
TreeView->RequestRefresh();
TreeView->ExpandAll();
This means that even if the data changes and the TreeView is refreshed, as long as the list of child widgets under the group is expanded, the child widget will not update to display the new data, and you must manually click on the group to retract and then expand
Is there an elegant way to solve this problem?