适配器的Android Update工具栏/操作栏标题 [英] Android Update toolbar/actionbar title from Adapter

查看:115
本文介绍了适配器的Android Update工具栏/操作栏标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从适配器更新ActionBar标题?

How can I update ActionBar title from Adapter?

我尝试过:

((MainActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.home_item));

,但是这种方式仅适用于片段。所以我这样做:

but this way only works within fragments. So I do this:

((MainActivity) view.getContext()).getSupportActionBar().setTitle(getString(R.string.home_item));

并获取强制转换异常。我不知道该怎么做。

and get cast exception. I don't know how to do it.

因此可以从适配器更新ActionBar标题吗?谢谢

So it is possible to update ActionBar Title from Adapter? Thanks

推荐答案

在适配器中传递活动上下文。

pass the activity context in your adapter.

Myadapter adapter = new Myadapter (this);

在Myadapter类中:

In Myadapter class:

Context context;
public Myadapter(Context c){
      this.context = c;
}

现在使用上下文设置标题如下:

Now use context to set the title like:

((MainActivity) context.getSupportActionBar().setTitle(getString(R.string.home_item));

这篇关于适配器的Android Update工具栏/操作栏标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆