Usm*_*war 7 android refresh updates android-layout android-recyclerview
我有一个帖子提要(类似于 Facebook 新闻提要)活动,在它的布局中我有一个EditText,顶部有共享按钮,下面有一个RecyclerView。RecyclerView 通过从 Server 获取所有 Posts 并通过 Adapter in OnCreateMethod绑定数据来显示所有用户的 Posts 。
当用户通过在 EditText 中输入一些文本并点击共享按钮发布内容时,数据将发送到服务器(我正在使用 Retrofit),并在服务器成功响应后调用相同的函数,该函数调用OnCreateMethod 以显示所有帖子以更新回收器视图。
我面临的问题是数据已发布到服务器,但仅当我在键入后按后退按钮隐藏/关闭键盘或通过点击 EditText 显示/打开键盘时,布局才会更新。
以下是一些代码以便更好地理解:
在这里,当用户发布某些内容时,我向服务器发送请求:
Call<CreatePost> call = mAPIService.sharePost(apiKey, post_description);
call.enqueue(new Callback<CreatePost>() {
@Override
public void onResponse(@NonNull Call<CreatePost> call, @NonNull Response<CreatePost> response) {
boolean error = response.body().getError();
if (!error) {
displayFeedPosts();
}
}
@Override
public void onFailure(@NonNull Call<CreatePost> call, @NonNull Throwable t) {
Log.d(TAG, "Error: " + t.getMessage());
}
});
Run Code Online (Sandbox Code Playgroud)
这是 displayFeedPosts 方法:
private void displayFeedPosts() {
Call<FeedPosts> call = mAPIService.displayFeedPosts(apiKey);
call.enqueue(new Callback<FeedPosts>() {
@Override
public void onResponse(@NonNull Call<FeedPosts> call, @NonNull Response<FeedPosts> response) {
boolean error = response.body().getError();
if (!error) {
ArrayList<Post> feedPosts = response.body().getPosts();
for (Post post : feedPosts) {
mTripFeedPostUserNames.add(post.getFirstName() + " " + post.getLastName());
mTripFeedPostTime.add(post.getPostDatetime());
mTripFeedPostContent.add(post.getPostDescription());
}
}
}
@Override
public void onFailure(@NonNull Call<FeedPosts> call, @NonNull Throwable t) {
Log.d(TAG, "Error: " + t.getMessage());
}
});
initRecyclerView();
}
private void initRecyclerView() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true);
RecyclerView recyclerView = findViewById(R.id.trip_feed_recycler_view);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(layoutManager);
mTripFeedPostRecyclerViewAdapter = new TripFeedPostRecyclerViewAdapter(this, mTripFeedPostProfileImages, mTripFeedPostUserNames, mTripFeedPostTime, mTripFeedPostContent, mTripFeedPostImages, mTripFeedPostID);
recyclerView.setAdapter(mTripFeedPostRecyclerViewAdapter);
}
Run Code Online (Sandbox Code Playgroud)
PS:我是 Android 新手,如果我做错了事,我深表歉意。欢迎您提出建议。
我在Arsalan Khan Answer的帮助下解决了上述问题。
实际上并initRecyclerView()没有在方法之外执行onResponse()。将其包含在onResponse()方法中并使用notifyDataSetChanged()而不是initRecyclerView()解决了问题。
数据被填充两次的解决方案我通过 displayFeedPosts()以下方式修改解决了它:
private void updateFeedPosts() {
Call<FeedPosts> call = mAPIService.displayFeedPosts(apiKey);
call.enqueue(new Callback<FeedPosts>() {
@Override
public void onResponse(@NonNull Call<FeedPosts> call, @NonNull Response<FeedPosts> response) {
boolean error = response.body().getError();
if (!error) {
mTripFeedPostProfileImages.clear();
mTripFeedPostUserNames.clear();
mTripFeedPostTime.clear();
mTripFeedPostContent.clear();
mTripFeedPostImages.clear();
mTripFeedPostID.clear();
mTripFeedPostRecyclerViewAdapter.notifyDataSetChanged();
ArrayList<Post> feedPosts = response.body().getPosts();
for (Post post : feedPosts) {
mTripFeedPostProfileImages.add(post.getProfilePicture());
mTripFeedPostUserNames.add(post.getFirstName() + " " + post.getLastName());
mTripFeedPostTime.add(post.getPostDatetime());
mTripFeedPostContent.add(post.getPostDescription());
mTripFeedPostImages.add(post.getPostImagePath());
mTripFeedPostID.add(post.getPostTripfeedId());
}
mTripFeedPostRecyclerViewAdapter.notifyDataSetChanged();
}
}
@Override
public void onFailure(@NonNull Call<FeedPosts> call, @NonNull Throwable t) {
Log.d(TAG, "Error: " + t.getMessage());
}
});
}
Run Code Online (Sandbox Code Playgroud)
我清除了适配器数据,然后再次使用更新的数据填充它。我希望我的回答能够帮助有同样问题的人。
PS:我知道我没有以正确的方式使用我的适配器模型,我只能对ArrayList所有值使用一个,稍后我会修改它,现在我专注于主要功能。
| 归档时间: |
|
| 查看次数: |
1924 次 |
| 最近记录: |