AppCompat 和 Androidx 之间的兼容性

Compatibility between AppCompat and Androidx

我需要从我的片段创建一个 DatePeriodPicker,如下所示:

要创建日期时间段对话框,我需要使用方法 .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;

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

import android.app.Fragment;

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

这是我崩溃时的日志:

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.onItemSelected(Fragment_Reports_Details.java:802)
        at org.angmarch.views.NiceSpinner.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.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)

这是我创建的 class 以尝试传递 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) {

    }

}

我从我的片段中调用这个 class 使用:

new Dialog_attemp_2();

使用 androidx 你可以定义一个 DialogFragment with an instance of 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 组件

通过 Material Components for Android,您可以使用新的 MaterialDatePicker

目前它正在积极开发中,需要 android 库的 material 组件版本 1.1.0

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

要select一个单一日期只需使用:

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

要select一个范围日期,您可以使用日期范围选择器:

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

如果你真的需要使用这个fragmentmanager你可以添加app compat依赖

添加 gradle 依赖项:

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

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