简体   繁体   中英

Can't change text of textView dynamically when using ViewPager and Fragments

I'm simply trying to change the text of a textView that is located within the actual fragment. It isn't throwing any error but it simply isn't updating. Aside from this the fragment successfully attaches to the activity via a viewpager.

The 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);
    }
}

The fragment's layout:

    <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 ,不要像这样膨胀新的布局。

Just replace:

return inflater.inflate(R.layout.fragment_plot_data, container, false);

to:

return view;

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