简体   繁体   中英

Dynamic TextView not updating in fragment

I'm trying to set a TextView that's in a fragment but the text won't update in the view. The text is 1 of 2 pages in a ViewPager. The ViewPager is inside another fragment that has a button which when clicked uses an interface to the MainActivity that calls the changeText() that's inside the fragment with my text.

Fragment Inside 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.
}
}

Relevant part of Fragment that contains ViewPager:

OnHeadlineSelectedListener callback;

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

public interface OnHeadlineSelectedListener {
    void onArticleSelected();
}

//...

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

Relevant MainActivity Part:

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

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

Here is the layout that contains the 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>

Here's where the button and ViewPager is. The button is the 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>

Looks like your textFV is inside layout that has visibility gone. Can it be the problem?

            <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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM