动态TextView不在片段中更新

时间:2019-04-22 18:15:49

标签: java android view textview updating

我正在尝试设置一个片段中的TextView,但是文本不会在视图中更新。文本是ViewPager中2页中的1页。 ViewPager在另一个片段中,该片段具有一个按钮,单击该按钮时,它将使用MainActivity的接口,该界面用我的文本调用片段中的changeText()

ViewPager中的片段:

public class PlaceholderFragment extends Fragment {

public TextView textFV;

public PlaceholderFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_pull, container, false);
    textFV = rootView.findViewById(R.id.textFV);
    return rootView;
}

@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public void changeText() {
    textFV.setText("Test"); - Won't update view.
    System.out.println(textFV.getText()); - Shows correct text.
}
}

包含ViewPager的Fragment的相关部分:

OnHeadlineSelectedListener callback;

public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener callback) {
    this.callback = callback;
}

public interface OnHeadlineSelectedListener {
    void onArticleSelected();
}

//...

@Override
public void onClick (View v) {
    callback.onArticleSelected();
}

相关的MainActivity部分:

public void onArticleSelected() {
    PlaceholderFragment articleFrag = (PlaceholderFragment) getSupportFragmentManager().findFragmentById(R.id.viewpager);

    if (articleFrag != null) {
        articleFrag.changeText();
    }
}

这是包含TextView的布局:

<android.support.constraint.ConstraintLayout
    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="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

        <LinearLayout
            android:id="@+id/tab_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp">

            <LinearLayout
                android:id="@+id/containerPull"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:visibility="gone">

                <TextView
                    android:id="@+id/textsLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/label_left_margin"
                    android:layout_marginTop="5dp"
                    android:text="@string/label_common"
                    android:textColor="@color/colorLabelText"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textFV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Hello, World!"
                    android:textColor="@android:color/black"
                    android:textSize="18sp" />

</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>

在这里是按钮和ViewPager所在的位置。该按钮是ImageButton:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <android.support.v7.widget.Toolbar
            android:id="@+id/pullToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="Information"
            app:titleTextAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title.Montserrat">

            <ImageButton
                android:id="@+id/toolbarButtonPull"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginEnd="1dp"
                android:background="@drawable/button_toolbar_default"
                android:contentDescription="Pull"
                android:padding="15dp"
                android:src="@drawable/ic_arrow_downward_black_24dp" />
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabTextAppearance="@style/TextAppearance.Design.Tab.NoCaps">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab1" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tab2" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" />

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

1 个答案:

答案 0 :(得分:-1)

好像您的textFV位于内部布局中,可见性消失了。可能是问题吗?

            <LinearLayout
                android:id="@+id/containerPull"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                **android:visibility="gone">**

                <TextView
                    android:id="@+id/textsLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/label_left_margin"
                    android:layout_marginTop="5dp"
                    android:text="@string/label_common"
                    android:textColor="@color/colorLabelText"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/textFV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Hello, World!"
                    android:textColor="@android:color/black"
                    android:textSize="18sp" />