gpt4 book ai didi

java - 我该如何做 AsyncLayoutInflater onCreate

转载 作者:行者123 更新时间:2023-12-01 18:37:32 25 4
gpt4 key购买 nike

Android应用程序的内容部分由大量的代码结构组成。 (3000行)我把它们分成3个viewstub。我启动了 3 个嵌套处理程序,然后膨胀了 View stub 。

但它并没有再次加速。它仍然打开得很慢。现在,在研究时,我遇到了asyntasklayoutinflater,但我认为我无法正确完成这项工作。内容中没有显示任何 View 。//我删除了setContentView 我也想将我的viewstubs移动到asynctask中,但它不是静态的,我该怎么做?你能帮忙吗?提前致谢!

   ViewStub viewStub1;
ViewStub viewStub2;
ViewStub viewStub3;
View coachStub1;
View coachStub2;
View coachStub3;

@SuppressLint("InflateParams")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}


new AsyncLayoutInflater(this).inflate(R.layout.activity_main, null, new AsyncLayoutInflater.OnInflateFinishedListener() {
@Override
public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent) {
long start = System.currentTimeMillis();

viewStub1 = view.findViewById(R.id.viewStub1);
viewStub2 = view.findViewById(R.id.viewStub2);
viewStub3 = view.findViewById(R.id.viewStub3);


viewStub1.setLayoutResource(R.layout.viewstub1);
coachStub1 = viewStub1.inflate();

viewStub2.setLayoutResource(R.layout.viewstub2);
coachStub2 = viewStub2.inflate();

viewStub3.setLayoutResource(R.layout.viewstub3);
coachStub3 = viewStub3.inflate();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Objects.requireNonNull(notificationManager).cancelAll();
initialize(view);
new MainAsyncTask(MainActivity.this).execute();

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
VKIPagerAdapter = new MyViewPagerAdapter();
vkipager.setAdapter(VKIPagerAdapter);
VKIPagerAdapter.notifyDataSetChanged();
vkipager.setOffscreenPageLimit(10);
vkipager.addOnPageChangeListener(viewPagerPageChangeListener);
long finish = System.currentTimeMillis();
Log.d("Handler Displayed", "\t" + (finish - start));
}
}, 100);


menu1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

///
}
});



inters_ad1 = new InterstitialAd(context);
inters_ad2 = new InterstitialAd(context);


new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (blink_settings && !MainActivity.this.isFinishing()) {
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(500);
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
profile.startAnimation(anim);
}
}
}, 8000);



}
});



}

异步任务

  private static class MainAsyncTask extends AsyncTask<Integer, Integer, String> {
private WeakReference<MainActivity> activityWeakReference;

MainAsyncTask(MainActivity activity) {
activityWeakReference = new WeakReference<>(activity);
}

@Override
protected String doInBackground(Integer... integers) {
MainActivity activity = activityWeakReference.get();
activity.sharedPreferencesKeys();
activity.alertDialogClickListener();
activity.changeListener();

return "MainAsyncTask Worked!";
}


@Override
protected void onPostExecute(String s) {
Log.d("MainAsyncTask", "" + s);
}
}

初始化方法

private void initialize(View view) {
loadframe = view.findViewById(R.id.loadframe);
menu = view.findViewById(R.id.menu);
bottombar = view.findViewById(R.id.bottombar);
menu1 = view.findViewById(R.id.menu1);


pageIndicator = coachStub1.findViewById(R.id.pageIndicator);
vkisonuctw = coachStub1.findViewById(R.id.vkisonuctw);

numberpicker = coachStub2.findViewById(R.id.numberpicker);
radiogrouphareket = coachStub2.findViewById(R.id.radiogrouphareket);

belkalcasonuctw = coachStub3.findViewById(R.id.belkalcasonuctw);
belkalcasonuctwinfo = coachStub3.findViewById(R.id.belkalcasonuctwinfo);

// much more

}

最佳答案

你必须异步加载 View ,调用这个方法setcontentview(view)将其设置到activity

像这样:

new AsyncLayoutInflater(context).inflate(layout, null, new AsyncLayoutInflater.OnInflateFinishedListener() {
@Override
public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent) {
//load view into activity
activity.setContentView(view);
}
});

关于java - 我该如何做 AsyncLayoutInflater onCreate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60006139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com