从其他应用程序导航时如何通知 recyclerview 适配器更改
How to notify recyclerview adapter changes when navigating from other app
已在我的 activity
上初始化 RecyclerView
private RecyclerView listView;
private ListCardAdapter listCardAdapter;
private Subscription subscription;
@Override
protected void onCreate(Bundle savedInstanceState) {
listView = findViewById(R.id.subscription_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.VERTICAL);
listView.setLayoutManager(layoutManager);
listCardAdapter= new ListCardAdapter(getApplicationContext());
listCardAdapter.setOnItemClickListener(this);
listCardAdapter.setAdapter(subscriptionListCardAdapter);
getPurchases(); //runs a service and sets subscription value and calls back onSkuDetails
}
@Override
public void onSkuDetails(List<SkuDetails> skuDetails) {
removeProgressDialog();
listCardAdapter.setData(subscription, skuDetails);
}
//ListCardAdapter
public void setData(Subscription subscription, List<SkuDetails> skuDetailsList) {
this.subscription = subscription;
this.skuDetailsList = skuDetailsList;
notifyDataSetChanged();
}
当用户在我的 activity 上选择一个按钮时,它会调用一个方法并回调 onSkuDetails
,然后它会更新适配器的数据,然后您将在项目列表中看到更改.
其中一个按钮将启动 google play store app
并导航到用户的订阅页面,用户取消订阅,当用户单击返回按钮直到他返回应用程序时,它将调用 onSkuDetails
,之后,recyclerview 适配器不会触发导致项目不更新的 onBindViewHolder
,这仅在从其他应用程序(Play 商店)导航时发生,但当我尝试从屏幕上下拉(刷新)时,它将调用 onBindViewHolder
然后更新项目列表。
运行 notifyDataSetChanged()
onUIthread.
private void notifyAdapterDataSetChanged() {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
subscriptionListCardAdapter.notifyDataSetChanged();
}
});
}
已在我的 activity
上初始化 RecyclerViewprivate RecyclerView listView;
private ListCardAdapter listCardAdapter;
private Subscription subscription;
@Override
protected void onCreate(Bundle savedInstanceState) {
listView = findViewById(R.id.subscription_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.VERTICAL);
listView.setLayoutManager(layoutManager);
listCardAdapter= new ListCardAdapter(getApplicationContext());
listCardAdapter.setOnItemClickListener(this);
listCardAdapter.setAdapter(subscriptionListCardAdapter);
getPurchases(); //runs a service and sets subscription value and calls back onSkuDetails
}
@Override
public void onSkuDetails(List<SkuDetails> skuDetails) {
removeProgressDialog();
listCardAdapter.setData(subscription, skuDetails);
}
//ListCardAdapter
public void setData(Subscription subscription, List<SkuDetails> skuDetailsList) {
this.subscription = subscription;
this.skuDetailsList = skuDetailsList;
notifyDataSetChanged();
}
当用户在我的 activity 上选择一个按钮时,它会调用一个方法并回调 onSkuDetails
,然后它会更新适配器的数据,然后您将在项目列表中看到更改.
其中一个按钮将启动 google play store app
并导航到用户的订阅页面,用户取消订阅,当用户单击返回按钮直到他返回应用程序时,它将调用 onSkuDetails
,之后,recyclerview 适配器不会触发导致项目不更新的 onBindViewHolder
,这仅在从其他应用程序(Play 商店)导航时发生,但当我尝试从屏幕上下拉(刷新)时,它将调用 onBindViewHolder
然后更新项目列表。
运行 notifyDataSetChanged()
onUIthread.
private void notifyAdapterDataSetChanged() {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
subscriptionListCardAdapter.notifyDataSetChanged();
}
});
}