我遵循这个示例并向选项卡布局添加通知,我的问题是如何在像 Whats 应用程序这样的片段页面中更新徽章计数器。我尝试了下面的代码,因此它在选项卡布局活动中工作正常,但如果对象计数大小增加,我不知道如何在片段中设置数字增量?请帮助解决这个问题。
这是我的代码:
ApiUtil.getServiceClass().getDiscussion(demo, RTFA + "; FedAuth=" + FedAuth).enqueue(new Callback<Discussion>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onResponse(Call<Discussion> call, Response<Discussion> response) {
List<com.com.ideaentity.csp.daoutils.discussiondao.Result> result=response.body().getD().getResults();
discussion=result.size();
updateDiscussin(discussion);
}
@Override
public void onFailure(Call<Discussion> call, Throwable t) {
}
});
ApiUtil.getServiceClass().getMyDocs(dosurl,RTFA + "; FedAuth=" + FedAuth).enqueue(new Callback<MyDocs>() {
@Override
public void onResponse(Call<MyDocs> call, Response<MyDocs> response) {
if (response.isSuccessful()){
// myDocsList.setValue(response.body().getD().getMyDocsResults());
List<com.com.ideaentity.csp.daoutils.mydocs.Result> myDocsResultList=response.body().getD().getFiles().getResults();
documents=myDocsResultList.size();
updatevalue(documents);
}else {
}
}
@Override
public void onFailure(Call<MyDocs> call, Throwable t) {
}
});
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
String category=Utils.getFromSharedPreference("category");
viewPager = findViewById(R.id.container);
viewPager.setAdapter(mSectionsPagerAdapter);
tabLayout =findViewById(R.id.tabs);
tabLayout.setSelectedTabIndicatorHeight(0);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
if (discussion!=null&&documents!=null){
}
}
public void updateDiscussin(Integer discussion) {
BadgeDrawable badgeDiscussion =
tabLayout.getTabAt(2).getOrCreateBadge();
badgeDiscussion.setVisible(true);
badgeDiscussion.setNumber(discussion);
badgeDiscussion.setBackgroundColor(getResources().getColor(R.color.floating_button_color));
}
private void updatevalue(Integer discussion) {
BadgeDrawable badgeDocuments =
tabLayout.getTabAt(3).getOrCreateBadge();
badgeDocuments.setVisible(true);
badgeDocuments.setNumber(discussion);
badgeDocuments.setBackgroundColor(getResources().getColor(R.color.floating_button_color));
}
badgeDiscussion.setBackgroundColor(getResources().getColor(R.color.floating_button_color));
}
在上面的代码中,我们在主机活动中的 tablayout 对象中设置了数字,但我无法在片段类中使用 setnumber 来更新计数?
例如,如果我们将一些数据添加到 json 对象中并且对象计数增加,那么此时计数器也应该增加但计数增加,当我关闭应用程序并再次打开时它会增加。
更新:我应该在主机活动中添加任何静态方法,然后在调用 rest api 时调用每个片段吗?
注意:我正在为三个片段页面使用 recyclerview apter 来显示数据。
更新 1: 我在片段中添加了以下代码以获取更新计数,但它不起作用:
Integer count=myDocsResultList.size();
BadgeDrawable badgeDrawable = BadgeDrawable.create(getContext());
badgeDrawable.setNumber(count);