Android xml布局项目未更新

时间:2016-05-30 01:06:30

标签: android android-layout android-fragments

我有一个细节片段,其中有几个项目,主要是TextView。我还有一个CursorLoader,onLoadFinished应该在布局中设置所有这些值。

例如,在此代码段中,它打印出该文本被设置为我从ContentProvider收到的值

  String dur = this.expected_duration + " " + this.durationUnit;
    Log.v(LOG_TAG, "SETTING DURATION " + dur);
    if(dur != null || !"".equals(dur)) {
        if(durationTextView != null) {
            Log.v(LOG_TAG, "SETTING TEXT VIEW " + dur);
            durationTextView.setText(dur);
        }
    }else{
        Log.v(LOG_TAG, "FAIL SETTING TEXT VIEW " + dur);
    }

enter image description here

我在日志中看到了结果,但是这个结果永远不会到达实际的TextView。

我知道这不是可见性或文字字体的问题。我将TextView以粉红色突出显示,并确保模拟文本显示在屏幕上。我有一种感觉,我可能正在创建重复的片段,一个在另一个之上(不确定它是否可能)。我可以完美地实例化所有TextView并提取所有值但不能将它们放在屏幕上。请帮忙。另外,如果我的重复片段理论是正确的,请解释你如何追踪它。谢谢。

我的布局文件(以防万一)

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jennyeckstein.udacitycoursepicker.DetailActivityFragment"
    >



    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/course_subtitle"
                    tools:text="How to Make an Android App"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:id="@+id/course_layout_detail"
                    android:layout_below="@id/course_subtitle"
                   >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/duration"
                    android:background="@color/pink"
                    android:textSize="16sp"
                    tools:text="333 years"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/course_level"
                    tools:text="ADVANCED"/>
            </LinearLayout>

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/begin_course_button"
                android:text="Go to Udacity"
                android:layout_below="@+id/course_layout_detail"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/required_knowledge"
                android:layout_below="@id/begin_course_button"
                tools:text="If you are new to programming and don’t know where to start"
                />



            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/required_knowledge"
                android:id="@+id/summary"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/summary"
                android:background="@color/green"
                android:id="@+id/syllabus"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/syllabus"
                android:id="@+id/faq"/>



    </RelativeLayout>
    </ScrollView>
</FrameLayout>

GitHub Repo

上的源代码

布局名称:fragment_detail.xml和 片段名称:DetailActivityFragment.java

1 个答案:

答案 0 :(得分:1)

问题是因为您正在按照您的想法多次加载详细信息片段。

在布局文件activity_detail.xml中,第79-84行有一个静态详细信息片段:

        <fragment
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:name="com.jennyeckstein.udacitycoursepicker.DetailActivityFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

当您的应用到达onCreate中方法DetailActivity.java中的第42行时,它会调用setContentView(R.layout.activity_detail);来加载您的第一个详细信息片段。

然后在同一方法的第47-49行,你打电话

    DetailActivityFragment fragment = new DetailActivityFragment();
    fragment.setArguments(arguments);
    getFragmentManager().beginTransaction().add(R.id.fragment_detail_container, fragment).commit();

加载细节片段的第二个副本。

activity_detail.xml中,尝试注释掉第79-84行,

        <fragment
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:name="com.jennyeckstein.udacitycoursepicker.DetailActivityFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

如果您现在运行您的应用并选择一个课程,详细信息屏幕将打开,文本视图中显示的文字显示为粉红色背景。

有关静态和动态片段的更多信息,您可能会发现以下Android文档很有帮助。 https://developer.android.com/guide/components/fragments.html

相关问题