use asynclayoutinflater instead of new thread

This commit is contained in:
Tungstend 2022-11-26 04:36:00 +08:00
parent 37ced479ac
commit 68531a5a95
3 changed files with 14 additions and 7 deletions

View File

@ -32,6 +32,7 @@ dependencies {
implementation 'commons-io:commons-io:2.11.0' implementation 'commons-io:commons-io:2.11.0'
implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0' implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.asynclayoutinflater:asynclayoutinflater:1.0.0'
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4' androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

View File

@ -1,11 +1,11 @@
package com.tungsten.fcllibrary.component.ui; package com.tungsten.fcllibrary.component.ui;
import android.content.Context; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import androidx.annotation.LayoutRes; import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.asynclayoutinflater.view.AsyncLayoutInflater;
import com.tungsten.fclcore.task.Task; import com.tungsten.fclcore.task.Task;
import com.tungsten.fcllibrary.component.FCLActivity; import com.tungsten.fcllibrary.component.FCLActivity;
@ -30,8 +30,11 @@ public abstract class FCLBaseUI implements FCLUILifecycleCallbacks {
return activity; return activity;
} }
public Task<?> setContentView(@LayoutRes int id) { public void setContentView(@LayoutRes int id, OnInflateFinishedListener listener) {
return Task.runAsync(() -> contentView = LayoutInflater.from(getContext()).inflate(id, null)); new AsyncLayoutInflater(context).inflate(id, null, (view, resid, parent) -> {
contentView = view;
listener.onFinish();
});
} }
public View getContentView() { public View getContentView() {
@ -76,4 +79,8 @@ public abstract class FCLBaseUI implements FCLUILifecycleCallbacks {
public void onDestroy() { public void onDestroy() {
} }
public interface OnInflateFinishedListener {
void onFinish();
}
} }

View File

@ -6,9 +6,7 @@ import android.view.ViewGroup;
import androidx.annotation.LayoutRes; import androidx.annotation.LayoutRes;
import com.tungsten.fclcore.task.Schedulers;
import com.tungsten.fclcore.task.Task; import com.tungsten.fclcore.task.Task;
import com.tungsten.fclcore.util.function.ExceptionalRunnable;
import com.tungsten.fcllibrary.R; import com.tungsten.fcllibrary.R;
import com.tungsten.fcllibrary.anim.DisplayAnimUtils; import com.tungsten.fcllibrary.anim.DisplayAnimUtils;
import com.tungsten.fcllibrary.component.view.FCLUILayout; import com.tungsten.fcllibrary.component.view.FCLUILayout;
@ -22,11 +20,12 @@ public abstract class FCLCommonUI extends FCLBaseUI {
public FCLCommonUI(Context context, FCLUILayout parent, @LayoutRes int id) { public FCLCommonUI(Context context, FCLUILayout parent, @LayoutRes int id) {
super(context); super(context);
this.parent = parent; this.parent = parent;
setContentView(id).thenRunAsync(Schedulers.androidUIThread(), (ExceptionalRunnable<Exception>) this::onCreate).thenRunAsync(Schedulers.androidUIThread(), (ExceptionalRunnable<Exception>) () -> { setContentView(id, () -> {
onCreate();
if (callback != null) { if (callback != null) {
callback.onLoad(); callback.onLoad();
} }
}).start(); });
} }
@Override @Override