gpt4 book ai didi

android - 在打开/关闭软键盘之前,RecyclerView 不会更新

转载 作者:行者123 更新时间:2023-11-29 00:59:00 35 4
gpt4 key购买 nike

我有一个Posts Feed(类似于Facebook News Feed) Activity ,在它的布局中我有一个EditText,分享Button顶部和下面的 RecyclerView。 RecyclerView通过OnCreate方法中的Adapter从Server获取所有的Posts并绑定(bind)数据来显示所有用户的Posts。

当用户通过在 EditText 中键入一些文本并点击分享按钮来发布内容时,数据将发送到服务器(我正在使用 Retrofit),并且在服务器成功响应后,我将调用 OnCreate< 中调用的相同函数 显示所有帖子以更新 RecyclerView 的方法。

我面临的问题是数据已发布到服务器,但仅当我在键入后按后退按钮隐藏/关闭键盘或通过点击 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());
}
});

这里是 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);
}

PS:我是 Android 新手,如果我做错了事情,我深表歉意。欢迎您提出建议。

注意:针对 ListView here 提出了相同的问题和 here但它并没有解决我的问题

最佳答案

我在Arsalan Khan的帮助下解决了上述问题回答。

实际上 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());
}
});
}

我清除了适配器数据,然后用更新的数据再次填充它。我希望我的回答能帮助遇到同样问题的人。

PS: 我知道我没有以正确的方式使用我的适配器模型,我只能为所有值使用一个 ArrayList,我稍后会修改它现在我专注于主要功能。

关于android - 在打开/关闭软键盘之前,RecyclerView 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52484912/

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