Repaint/Invalidate 片段视图

Repaint/Invalidate Fragment View

我有一个 activity 片段,其中包含用户输入的不同视图。 每当此片段更改时,视图似乎都不会更新,如下面的屏幕截图所示。

我也试图使视图无效。我错过了什么?

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

正如 Blackbelt 提到的,片段被加载了两次,因为我忘记了在我的 activity 布局中也添加了片段。

<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" />