使用 ViewPager 和 Fragments 时无法动态更改 textView 的文本
Can't change text of textView dynamically when using ViewPager and Fragments
我只是想更改位于实际片段中的 textView 的文本。它没有抛出任何错误,但它只是没有更新。除此之外,该片段通过 viewpager 成功附加到 activity。
片段:
public class FragmentPlotData extends Fragment {
public static TextView textViewPlotData;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_plot_data, container, false);
textViewPlotData = (TextView) view.findViewById(R.id.textViewPlotData);
textViewPlotData.setText("Changed text ");
return inflater.inflate(R.layout.fragment_plot_data, container, false);
}
}
片段的布局:
<TextView
android:id="@+id/textViewPlotData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:textColor="@color/black_process_2"
android:text="@string/original text"
android:gravity="center"/>
</RelativeLayout>
您应该 return view
在 onCreateView
中,不要像那样膨胀新布局。
只需替换:
return inflater.inflate(R.layout.fragment_plot_data, container, false);
至:
return view;
我只是想更改位于实际片段中的 textView 的文本。它没有抛出任何错误,但它只是没有更新。除此之外,该片段通过 viewpager 成功附加到 activity。
片段:
public class FragmentPlotData extends Fragment {
public static TextView textViewPlotData;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_plot_data, container, false);
textViewPlotData = (TextView) view.findViewById(R.id.textViewPlotData);
textViewPlotData.setText("Changed text ");
return inflater.inflate(R.layout.fragment_plot_data, container, false);
}
}
片段的布局:
<TextView
android:id="@+id/textViewPlotData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:textColor="@color/black_process_2"
android:text="@string/original text"
android:gravity="center"/>
</RelativeLayout>
您应该 return view
在 onCreateView
中,不要像那样膨胀新布局。
只需替换:
return inflater.inflate(R.layout.fragment_plot_data, container, false);
至:
return view;