java - Android操作栏标题在填充自定义ListView后消失

标签 java android listview rss

我的问题是:启动应用程序后,只有一个 Activity,操作栏正确显示应用程序标题“ParseC”(而内容区域仍为空白),但在加载 RSS 后并填充自定义 ListView ,标题消失。这在 API 16 和 23 上进行了测试,效果相同。

onCreate() 解析 RSS feed;完成后,onPostExecute 将使用条目填充 ListView ,此时应用程序栏标题消失。 如果我运行 getSupportActionBar().getTitle().toString(),我确实会得到正确的答案 (ParseC)。

这是我的 MainActivity.java (onCreate):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    context = this;

    new DownloadXmlTask().execute(URL);
}

和(onPostExecute):

@Override
protected void onPostExecute(String result) {
    setContentView(R.layout.activity_main);

    //Adapter code
    ListView yourListView = (ListView) findViewById(R.id.listView);
    // get data from the table by the ListAdapter
    ListAdapter customAdapter = new ListAdapter(context, R.layout.itemlistrow, theEntries);
    yourListView .setAdapter(customAdapter);
}

我期望的是标题保留在那里,因为我没有离开 Activity (我怀疑布局上可能发生错误)。

对于布局,主 Activity 布局 (activity_main.xml) 包括 content_main.xml,其中包含使用自定义布局 itemlistrow.xml 的 ListView 。

我的问题是:什么可能导致应用栏标签被隐藏?

AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="co.samoliver.parsec">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="co.samoliver.parsec.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_info" />

布局: 内容_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="co.samoliver.parsec.MainActivity"
tools:showIn="@layout/activity_main">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView" />

itemlistrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_width="fill_parent">

<TextView android:textColor="#000000"
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Title" android:textStyle="bold"
    android:gravity="center_vertical"
    android:layout_weight="1"
    android:typeface="normal"
    android:height="40sp"
    android:textSize="16dp" />


<TextView android:textColor="#000000"
    android:id="@+id/summary"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Summary"
    android:layout_weight="1"
    android:height="20sp"
    android:typeface="normal" />

<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textColor="#000000"
android:gravity="right"
android:id="@+id/id"
android:text="Id"
android:height="0sp"
android:typeface="normal" />

最佳答案

错误可能出现在您的 list android:theme="@style/AppTheme.NoActionBar" 中。此外,您不需要调用 setContentView() 两次(这也可能会导致您的问题)。一旦进入 onCreate() 就可以了。如果您不调用两次,而是在收到数据时在适配器上调用 notifyDataSetChanged(),它将提高应用的性能。我建议在 onCreate() 本身中设置适配器,并在收到数据时更新它。

关于java - Android操作栏标题在填充自定义ListView后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37779993/

相关文章:

java - 我们可以使用 DateTimeFormatter 的 Maven 依赖项

java - 外部存储的安装状态和正确权限问题

android - 如何在不减慢 ListView 速度的情况下滑动显示本地视频缩略图?

javascript - react native ListView scrollToEnd 它不起作用

java - 获取 ListView 单元格的高度以方便向上滚动

android 可检查 ListView

java - inflater 不会解压缩所有压缩字节

java - 以编程方式下载 Android SDK

java - 使用第三方依赖项部署 Java Web Applet

android - MavenLocal 无法使用 Gradle 找到 repo 协议(protocol)