【问题标题】:Dynamic TextView not updating in fragment动态 TextView 未在片段中更新
【发布时间】:2019-04-22 18:15:48
【问题描述】:

我正在尝试设置片段中的 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.
}
}

Fragment 中包含 ViewPager 的相关部分:

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>

【问题讨论】:

  • 您何时何地致电setText?你想要完成的事情是微不足道的,所以问题可能不在于设置你的文本,而在于你这样做的时间/地点,或者你的布局本身。
  • 我已经编辑了帖子。

标签: java android view textview updating


【解决方案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" />

【讨论】:

  • 刚刚尝试将可见性设置为可见但没有运气。
猜你喜欢
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多