AppCompat和Androidx之间的兼容性

时间:2019-08-22 08:29:31

标签: android material-design androidx android-datepicker material-components-android

我需要从片段中创建DatePeriodPicker,如下所示:

enter image description here

要创建日期时段对话框,我需要使用.show();方法。 在这种方法中,我需要传递fragmentManager。

                DatePickerDialog dpd = DatePickerDialog.newInstance(
                        Fragment_Details.this,
                        1999,
                        3-1,
                        2,2000,2-1,3
                );
                dpd.setYearRange(2000,2020);
                Calendar max_calendar = Calendar.getInstance();
                dpd.setMaxDate(max_calendar);
                dpd.show(getFragmentManager(),"P")

我的项目已迁移到androidX,因此每当我通过片段管理器时,它都会通过androidx片段管理器:(来自我的项目)

import androidx.fragment.app.Fragment;

但是,我尝试使用的库中位于类中的方法.show()使用此导入:

import android.app.Fragment;

我尝试创建一个Java类,扩展了appcompat活动,并将fragmentmanager(android.app.Fragment)传递给方法.show()。该方法识别出片段管理器来自(AppCompat),但是在我从片段创建此类的新实例后,它可能会崩溃。

这是我崩溃时的日志:

D/ViewRootImpl@13a430d[PopupWindow:c0edac8]: ViewPostIme pointer 1
D/AbsListView: onTouchUp() mTouchMode : 0
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mybimpos, PID: 19385
    java.lang.IllegalStateException: Activity has been destroyed
        at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1901)
        at android.app.BackStackRecord.commitInternal(BackStackRecord.java:688)
        at android.app.BackStackRecord.commit(BackStackRecord.java:646)
        at android.app.DialogFragment.show(DialogFragment.java:233)
        at com.example.mybimpos.Dialog_attemp_2.<init>(Dialog_attemp_2.java:17)
        at com.example.mybimpos.Fragment_Reports_Details$2.onItemSelected(Fragment_Reports_Details.java:802)
        at org.angmarch.views.NiceSpinner$2.onItemClick(NiceSpinner.java:177)
        at android.widget.AdapterView.performItemClick(AdapterView.java:350)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1683)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:4094)
        at android.widget.AbsListView$10.run(AbsListView.java:6583)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

这是我创建的试图通过appcompat片段管理器的类:

public class Dialog_attemp_2 extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
    public Dialog_attemp_2() {
        DatePickerDialog dpd = DatePickerDialog.newInstance(
                Dialog_attemp_2.this,
                1999,
                3-1,
                2,2000,2-1,3
        );
        dpd.setYearRange(2000,2020);
        Calendar max_calendar = Calendar.getInstance();
        dpd.setMaxDate(max_calendar);
        dpd.show(getFragmentManager(),"pierre");
    }
    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth, int yearEnd, int monthOfYearEnd, int dayOfMonthEnd) {

    }

}

我使用以下代码从片段中调用此类:

new Dialog_attemp_2();

2 个答案:

答案 0 :(得分:3)

使用androidx,您可以使用实例DialogFragment定义一个DatePickerDialog

import androidx.fragment.app.DialogFragment;

public static class DatePickerFragment extends DialogFragment
                            implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }
}

并通过以下方式调用它:

DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");

选项2:新的材料组件

有了Material Components for Android,您可以使用新的MaterialDatePicker

当前它正在积极开发中,并且需要版本 1.1.0 的Android库的材料组件。

implementation 'com.google.android.material:material:1.1.0-alpha09'

要选择单个日期,只需使用:

       MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
       builder.setTitleText(R.string.your_text);
       MaterialDatePicker<Long> picker = builder.build();
       picker.show(getSupportFragmentManager(), picker.toString());

要选择范围日期,您可以使用DateRange Picker,方法是:

MaterialDatePicker.Builder<Pair<Long, Long>> builder =
                    MaterialDatePicker.Builder.dateRangePicker();
                CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();                                        builder.setCalendarConstraints(constraintsBuilder.build());
                MaterialDatePicker<?> picker = builder.build();
                picker.show(getSupportFragmentManager(), picker.toString());

enter image description here

答案 1 :(得分:0)

如果您确实需要使用此fragmentmanager,则可以添加应用程序兼容性依赖项

添加gradle依赖项:

"implementation 'androidx.appcompat:appcompat:1.0.2'"

并从AppCompatActivity扩展您的活动。然后将getFragmentManager()更改为getSupportFragmentManager()。