错误 - 无法更改supportActionBar标题

时间:2017-01-06 02:36:42

标签: android android-layout kotlin anko

我试图从网络回调中调用的方法更改supportActionBar标题。即使我从主线程中调用它,标题也不会改变!如果我尝试任何其他方法。

我的布局:

  <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/userProfile"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/str_act_main_user_list_item_user_profile_picture" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

MainActivity:

class ActivityMain : BaseActivity(), ContractMainView {

    @Inject
    lateinit var mPresenter: ContractMainPresenter

    private var mUser: User? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        App.component!!.inject(this)
        setSupportActionBar(toolbar)
        initPageView()
        setUpProfile()
        mPresenter.init()

    }

    override fun getContext(): AppCompatActivity {
        return this
    }

    override fun getView(): BaseView? {
        return this
    }

    override fun getPresenter(): BasePresenter? {
        return mPresenter
    }

    private fun initPageView() {
        container.adapter = SectionsPagerAdapter(supportFragmentManager)
        container.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                if (position == 0 && positionOffset > 0.95) {
                    app_bar.setExpanded(false)
                }
            }

            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    app_bar.setExpanded(true)
                }
            }
        })
    }

    override fun fillUserInterface(user: User) {
        mUser = user
        runOnUiThread {
            println("changing title to: ${user.name}")
            supportActionBar!!.title = "This is a Test"
        }
    }

    private fun setUpProfile() {
        Picasso.with(this)
                .load("http://images.mentalfloss.com/sites/default/files/borat_primary.jpg")
                .fit()
                .centerCrop()
                .into(findViewById(R.id.userProfile) as ImageView)
    }
}

被叫方法是:fillUserInterface(user: User)。无论我做什么,头衔永远不会改变。感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

使用Activity.setTitle(CharSequence)代替Toolbar.setTitle(CharSequence)。