重新绘制/使片段视图无效

时间:2015-02-19 13:55:39

标签: java android fragment

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

fragment view

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

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

1 个答案:

答案 0 :(得分:0)

正如Blackbelt所说,Fragment被加载了两次,因为我忘记了在我的活动布局中,片段也被添加了。

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