Android fragments 使用新的数据更新刷新recyclerView
我使用改型2.0从api中检索数据,并使用recyclerView显示数据 我的主要活动有一个选项卡布局,其中一个选项卡有recyclerView,该选项卡的片段类用于检索数据和更新布局 在我的主布局中,我有一个制作帖子的fab(所有帖子都在fragment类中检索),这个fab具有在主活动中制作帖子的功能 那么,当fab的功能结束并且帖子成功保存在我的数据库中时,我如何刷新布局呢 基本上 用户单击fab>Makes his post>Alert dialog closes>recyclerView,应使用添加的新数据刷新 我的片段类:Android fragments 使用新的数据更新刷新recyclerView,android-fragments,android-recyclerview,retrofit2,Android Fragments,Android Recyclerview,Retrofit2,我使用改型2.0从api中检索数据,并使用recyclerView显示数据 我的主要活动有一个选项卡布局,其中一个选项卡有recyclerView,该选项卡的片段类用于检索数据和更新布局 在我的主布局中,我有一个制作帖子的fab(所有帖子都在fragment类中检索),这个fab具有在主活动中制作帖子的功能 那么,当fab的功能结束并且帖子成功保存在我的数据库中时,我如何刷新布局呢 基本上 用户单击fab>Makes his post>Alert dialog closes>recyclerVi
public class PostsRecentTab extends Fragment {
private static final String TAG = MainActivity.class.getSimpleName();
private RecyclerView feedView;
private ProgressDialog pDialog = MainActivity.pDialog;
LinearLayoutManager layoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.tab_recent_posts, container, false);
pDialog.setCancelable(false);
layoutManager = new LinearLayoutManager(this.getContext());
feedView = (RecyclerView) v.findViewById(R.id.feedView);
requestData();
return v;
}
public void requestData() {
SocialHubAPI apiService = ApiClient.getClient().create(SocialHubAPI.class);
pDialog.setMessage("Refreshing...");
showDialog();
Call<StatusResponse> call = apiService.getStatuses();
call.enqueue(new Callback<StatusResponse>() {
@Override
public void onResponse(Call<StatusResponse> call, Response<StatusResponse> response) {
int statusCode = response.code();
List<Status> statuses = response.body().getStatuses();
Log.d(TAG, "Status Code: " + statusCode);
hideDialog();
updateView(statuses);
}
@Override
public void onFailure(Call<StatusResponse> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
private void updateView(List<Status> statuses) {
StatusesAdapter adapter = new StatusesAdapter(statuses, R.layout.feed_item, getContext());
feedView.setLayoutManager(layoutManager);
feedView.setAdapter(adapter);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
Fab onClick调用此方法:
protected void processPost(String postText, String token) {
SocialHubAPI apiService = ApiClient.getClient().create(SocialHubAPI.class);
pDialog.setMessage("Posting...");
showDialog();
final PostRequest postRequest = new PostRequest();
postRequest.setStatus(postText);
Call<PostResponse> call = apiService.postStatus(postRequest, token);
call.enqueue(new Callback<PostResponse>() {
@Override
public void onResponse(Call<PostResponse> call, Response<PostResponse> response) {
hideDialog();
Toast.makeText(getApplicationContext(), "Status Posted Successfully!", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<PostResponse> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
protectedvoidProcessPost(字符串postText、字符串标记){
SocialHubAPI apiService=ApiClient.getClient().create(SocialHubAPI.class);
pDialog.setMessage(“发布…”);
showDialog();
final PostRequest PostRequest=新PostRequest();
postRequest.setStatus(postText);
Call Call=apiService.postStatus(postRequest,token);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
hideDialog();
Toast.makeText(getApplicationContext(),“状态发布成功!”,Toast.LENGTH\u LONG.show();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(TAG,t.toString());
}
});
}
您应该在更新视图(列表状态)
中使列表无效,而不是再次设置适配器。仅在onCreate()
中实例化适配器
此函数应如下所示:
adapter.addNewStatutes(statuses)
在适配器类中
public void addnewstates(列表状态)
{
this.statuses.addAll(statuses);
notifyDataSetChanged();
}
另外,在onResponse中使用EventBus或Rx,因为您的视图可能会被破坏,并且此方法可能会使您的应用程序崩溃
根据文档添加。您应该在
更新视图(列表状态)
中使列表无效,而不是再次设置适配器。仅在onCreate()
中实例化适配器
此函数应如下所示:
adapter.addNewStatutes(statuses)
在适配器类中
public void addnewstates(列表状态)
{
this.statuses.addAll(statuses);
notifyDataSetChanged();
}
另外,在onResponse中使用EventBus或Rx,因为您的视图可能会被破坏,并且此方法可能会使您的应用程序崩溃
根据文档添加。make you fab不可见,在检索完所有数据后,make fabvisible@sushildlh但这将如何帮助我刷新我的回收商视图?你的晶圆厂
onclick
方法在哪里??@sushildlh更新了!这两个API是不同的还是相同的?make you fab是不可见的,在所有数据检索之后,make fabvisible@sushildlh但这将如何帮助我刷新我的回收商视图?你的晶圆厂onclick
方法在哪里??@sushildlh更新了!这两种API是不同的还是相同的??
public void addNewStatutes(List<Status> statuses)
{
this.statuses.addAll(statuses);
notifyDataSetChanged();
}