【问题标题】:Android: setTitle inside RecyclerView addOnItemTouchListener is not workingAndroid:RecyclerView addOnItemTouchListener 中的 setTitle 不起作用
【发布时间】:2019-05-10 22:16:23
【问题描述】:

[以下更新]

单击 onLoadFinished (AsyncTaskLoader) 内的 RecyclerView 中的项目后,我试图更改默认标题栏中的标题,但标题没有改变。这个想法是当我在 RecyclerView 中选择一个项目并加载数据时,标题栏文本也应该更改,但它没有。

catsRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, catsRecyclerView,new RecyclerTouchListener.ClickListener() {
   @Override
   public void onClick(View view, int position) {
          Categories category = categories.get(position);
          int catId = category.getId();
          setTitle(category.getTitle());
          drawerLayout.closeDrawer(GravityCompat.START);
          commonActions(); //restarting loader and executing other methods
   }

   @Override
          public void onLongClick(View view, int position) {

           }
}));

当我记录 category.getTitle() 时,我得到了正确的文本,但标题没有改变我怎样才能做到这一点?

[更新]

将默认标题栏替换为自定义操作栏,这是活动的布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include layout="@layout/actionbar"/>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
     >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

  </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

动作栏布局

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:id="@+id/primaryToolbar">

</android.support.v7.widget.Toolbar>

活动

toolbar = findViewById(R.id.primaryToolbar);
    setSupportActionBar(toolbar);
    setTitle("My new title");
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);

但它的尺寸太大了,占据了整个屏幕

【问题讨论】:

  • 贴出整个代码,尤其是setTitle()
  • 这是包含 setTitle 的函数,它在 onLoadFinished 里面

标签: android android-recyclerview listener asynctaskloader android-titlebar


【解决方案1】:

我认为问题出在听众身上。当我将侦听器附加到回收器视图时,我会在整个 Using interface in adapter 中执行此操作,this 博客显示了该方法,还请查看此StackOverflow 答案,其中提供了示例。

【讨论】:

    【解决方案2】:

    您必须通过设置以下标志来启用操作栏来 setTitle()。

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
    

    以上代码适用于 AppCompatActivity。

    【讨论】:

    • 添加此代码后如何更改标题?我有默认标题栏,我应该删除它并添加操作栏吗?
    • 将默认标题栏替换为自定义操作栏,但添加代码后根本没有标题!!!!我可以看到菜单图标但没有标题
    【解决方案3】:

    我有 2 个问题。第一个是关于在 RecyclerView 中通过 tab 键重新加载 AsyncTaskLoader 后不更改标题栏的标题。问题是除了 onLoadFinished 之外,Loader 函数中还有另一个 setTitle,所以我将其删除并保留在 onLoadFinished 中,现在它工作正常,因此无需将默认标题栏替换为操作栏。

    当我尝试添加操作栏时的第二个问题,正如您在图像中看到的那样,标题占据了整个屏幕,因为根视图中有两个元素,即 actionBar 和 NavigationView,它们使 actionBar 成为实际这就是为什么它被调整大小以占据整个屏幕的原因。所以我把它放在一个 LinearLayout 里面,实际的内容在它下面,解决了第二个问题

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多