【问题标题】:android: How to update badge TextView from a class that extends RecyclerViewandroid:如何从扩展 RecyclerView 的类中更新徽章 TextView
【发布时间】:2016-06-24 12:11:40
【问题描述】:

我正在尝试根据回收站视图中按钮的点击事件来更新我的徽章。这是我的代码:

 @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.navigation_drawer, menu);
        item = menu.findItem(R.id.add);
        badgeLayout = (RelativeLayout) MenuItemCompat.getActionView(item);
        mCounter = (TextView) badgeLayout.findViewById(R.id.counter);
        cartButton=(Button) badgeLayout.findViewById(R.id.button1);
        cartButton.setTypeface(icon);

        super.onCreateOptionsMenu(menu, inflater);
    }

我想在这里更新 mCounter 的值:

这是recyclerview代码:

  holder.cartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               mCounter.setText("2");// THIS DOES NOT WORK

            }
        });

请帮助我!

【问题讨论】:

  • 您能解释一下您当前的解决方案会发生什么吗?碰撞?不更新?
  • @Shahid Sarwar 你也可以通过在适配器中制作接口来实现
  • 不更新..它不会崩溃..只是什么都不做

标签: java android android-fragments android-actionbar android-recyclerview


【解决方案1】:

您可以通过 4 种方式执行此任务:

  1. 创建public static 变量并将其值设置为您的徽章TextView。
  2. 创建BroadcastReceiver 并在值更改时调用它。
  3. 使用EventBus reference
  4. 创建一个interface

【讨论】:

    【解决方案2】:

    您应该将此事件(或只是您要设置的数字)传递回活动,调用invalidateOptionsMenu(); 并在函数onPrepareOptionsMenu(Menu menu) 中调用您在onCreateOptionsMenu(Menu menu, MenuInflater inflater) 中的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      相关资源
      最近更新 更多