2

I have an activity with a fragment, which contains different views for User Input. Whenever this fragment changes, the view seems not to be updated, as you can see in the screenshot below.

fragment view

I also tried to invalidate the view. What am I missing?

public class EventGeneralFragment extends Fragment {

    protected TextView _timeStartView;
    protected TextView _timeEndView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_event_general, container, false);

        _timeStartView = (TextView) view.findViewById(R.id.textViewStart);
        _timeEndView = (TextView) view.findViewById(R.id.textViewEnd);

        View.OnClickListener timeClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePickerDialog((TextView) v);
            }
        };

        _timeStartView.setOnClickListener(timeClickListener);
        _timeEndView.setOnClickListener(timeClickListener);

        return view;
    }

public void showTimePickerDialog(final TextView caller) {
        int hour = 18;
        int minute = 0;

        TimePickerDialog dialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                // ...
            }
        }, hour, minute, DateFormat.is24HourFormat(getActivity()));

        dialog.show();

        // Enforce repainting
        View view = getView();
        if (view != null) {
            view.invalidate();
        }
    }

}
public class CreateActivity extends ActionBarActivity {

    public enum FragmentType {
        General,
        EventRestriction
    }

    protected EventGeneralFragment _eventGeneralFragment;
    protected EventRestrictionFragment _eventRestrictionFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_create);

        _eventGeneralFragment = new EventGeneralFragment();

        getFragmentManager().beginTransaction().replace(R.id.fragment, _eventGeneralFragment).commit();
    }
}
van
  • 380
  • 3
  • 10

1 Answers1

1

As Blackbelt mentioned, the Fragment was loaded twice, because I forgot that in my activity layout the fragment was also added.

<fragment     android:name="at.phe.calendar.ui.activities.Fragments.EventGeneralFragment"
              android:id="@+id/fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />
van
  • 380
  • 3
  • 10