我的 Android 应用程序中的 Activity 加载速度非常慢,它使用 TwitterKit 将推文加载到 RecyclerView 中。LogCat 显示以下消息:“
跳过了 1491 帧!应用程序可能在其主线程上做了太多工作”
经过一番研究,我了解到我需要实现 AsyncTask 以将工作负载从主线程中移开。我试图实现它,但是,我无法让它工作。一个正确方向的点将不胜感激。请看下面的代码
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
//twitter imports
import com.twitter.sdk.android.core.Twitter;
import com.twitter.sdk.android.tweetui.TweetTimelineRecyclerViewAdapter;
import com.twitter.sdk.android.tweetui.UserTimeline;
public class travel_updates extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_line_info);
View view = getSupportActionBar().getCustomView();
Twitter.initialize(this);
workHorse workhorse = new workHorse();
workHorse.execute(UserTimeline);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private class workHorse extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
final UserTimeline searchTimeline = new UserTimeline.Builder().screenName("TflTravelAlerts")
.maxItemsPerRequest(20)
.build();
final TweetTimelineRecyclerViewAdapter adapter =
new TweetTimelineRecyclerViewAdapter.Builder(this)
.setTimeline(searchTimeline)
.setViewStyle(R.style.tw__TweetLightWithActionsStyle)
.build();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
}