android应用程序的内容部分由许多代码结构组成。 (3000行)我将它们分为3个视点。我开始嵌套3个处理程序,然后将viewtubs膨胀。
但是它并没有再次加速。它仍然非常缓慢地打开。现在,在研究时,我遇到了asyntasklayoutinflater,但我认为我无法正确完成这项工作。内容中没有任何视图。 //我删除了setContentView我也想将我的viewtubs移到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
}
最佳答案
您必须异步加载视图,调用此方法setcontentview(视图)将其设置为活动
像这样:
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);
}
});