私は、起動時に(かなり厳しく)活動があります。遅れは通常1秒未満続くが、それは目立つものであり、私はそれが望ましくない。読み込み時にアクティビティが遅くなる
私は何が遅れているのかはわかりませんが、ロード時にバターとして滑らかになるように修正する必要があります。ここで
は活動です:
public class ProfileActivity extends AppCompatActivity {
private CoordinatorLayout coordinatorLayout;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private SwipeRefreshLayout swipeRefreshLayout;
private EndlessRecyclerViewScrollListener scrollListener;
private int userId;
private User user;
private List<Object> data = new ArrayList<>();
protected UserAdapter userAdapter;
private TextView username, userNumPosts;
private ImageView userBackground, userColor, userIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
// Get userId from intent
userId = getIntent().getExtras().getInt("userId");
// Check if the user is in realm db
user = getRealm().where(User.class)
.equalTo("id", userId)
.findFirst();
// Make status bar black
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#000000"));
}
if (getSupportActionBar() != null) {
// Change toolbar color to the user's chosen color
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#" + user.getColor())));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setRefreshing(true);
username = (TextView) findViewById(R.id.username);
userNumPosts = (TextView) findViewById(R.id.userNumPosts);
userBackground = (ImageView) findViewById(R.id.userBackground);
userColor = (ImageView) findViewById(R.id.userColor);
userIcon = (ImageView) findViewById(R.id.userIcon);
username.setText(user.getUsername());
userNumPosts.setText(user.getNumPosts());
userColor.setBackgroundColor(Color.parseColor("#" + user.getColor()));
// Add the user icon
Glide.with(this)
.load(user.getIcon())
.into(userIcon);
// Add the user background
Glide.with(this)
.load(user.getBackgroundImage())
.into(userBackground);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setNestedScrollingEnabled(false);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
userAdapter = new UserAdapter(this, data);
recyclerView.setAdapter(userAdapter);
userAdapter.setUserAdapterListener(new UserAdapter.UserAdapterListener() {
@Override
public void onRequestRefresh() {
swipeRefreshLayout.setRefreshing(true);
getUserData(1);
}
});
scrollListener = new EndlessRecyclerViewScrollListener(layoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
getUserData(page);
}
};
recyclerView.addOnScrollListener(scrollListener);
getUserData(1);
}
public void getUserData(final int page) {
ApiInterface apiService = ApiClient.createService(ApiInterface.class, userAuthToken);
Call<BasicResponse> call = apiService.getUserData(userId, page);
call.enqueue(new Callback<BasicResponse>() {
@Override
public void onResponse(Call<BasicResponse> call, Response<BasicResponse> response) {
if (response.isSuccessful()) {
// Show the data
List<Message> messageData = response.body().getData();
data.addAll(messageData);
userAdapter.notifyDataSetChanged();
}
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onFailure(Call<BasicResponse> call, Throwable t) {
//
}
});
}
}
それがロードされていたときに私の活動が遅れさせることができましたか?私はすべての私のメインコードをonCreate()
に入れておくべきですか?
私はいくつかのログステートメントをそこに振って、タイムスタンプを調べます。 2つの間に遅延があるはずです。それは問題がどこにあるかを示します。私の推測はデータベースのものですが、グライド、私はそれに精通していない。 – nasch
Glideは非同期であるはずです。 @ user198122レイアウトは大規模に複雑に見えますが、これが問題です。投稿できますか? – Eselfar
ちょうどいくつかのログを追加し、あなたのタイミングをlogcatでチェックインしてください。[。](https://android-app-promotion.com/) – BayK