我只是想更改位于实际 fragment 内的textView 的文本。它没有抛出任何错误,但它根本没有更新。除此之外, fragment 通过 viewpager 成功附加到 Activity 。
fragment :
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);
}
}
fragment 的布局:
<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>
最佳答案
您应该在 onCreateView
中返回 view
,不要像那样膨胀新布局。
关于java - 使用 ViewPager 和 Fragments 时无法动态更改 textView 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31936877/