[Android] materialdatepicker usage notes

Time:2022-5-7

Materialdatepicker usage

background

Previously, we used datepicker for program development. But because my tablet is Nokia N1. The system is always stopped on version 5.1, and datepicker cannot be used. So we must think of another solution to replace it. Replace here with materialdatepicker. Here, try the selection method of time period of materialdatepicker.

Gradle environment configuration

The gradle file corresponding to app. Pay attention to the version of material.
The description on the official website is for version 1.3.0. However, when creating a project, the default version is 1.2. As a result, many interfaces cannot be compiled.

dependencies {
    ....
    implementation 'com.google.android.material:material:1.3.0'
    ....
}

Picture implementation code

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.datepicker.MaterialDatePicker;
import com.google.android.material.datepicker.MaterialPickerOnPositiveButtonClickListener;

import java.util.ArrayList;
import java.util.Calendar;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    //Resolve the parameters provided by aroute
    ARouter.getInstance().inject(this);

    if (mTransFromPath == "") {
        Log.e(TAG, "ARouter parameter[transFromPath] is unrecognized");
    }

    //Create fake data
    dummyData();

    //Gets the object of the recycleview
    RecyclerView list = findViewById(R.id.home_things_list);

    //Set layoutmanager
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    list.setLayoutManager(layoutManager);

    //Set layout and data source
    HomeThingsListAdapter adapter = new HomeThingsListAdapter(this, R.layout.things_list_item, mThings);

    //Set the click event of the list
    adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {

        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            Log.i(TAG, "Fridge things list item has been clicked. view:" + view.getId() + " position:" + position);
            
            //Filter view click events of date
            if (view.getId() == R.id.things_list_date_range) {
                FridgeThings item = mThings.get(position);
                Calendar fromCal = item.getStartDate();
                Calendar toCal = item.getEndDate();
                
                //Use materialdatepicker to select the time. Try the time period selection scheme here. So the daterangepicker is used.
                MaterialDatePicker dialog = MaterialDatePicker.Builder.dateRangePicker()
                            .setSelection(new Pair(fromCal.getTimeInMillis(), toCal.getTimeInMillis()))
                            .build();

                //Add listening event
                dialog.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
                    @Override
                    public void onPositiveButtonClick(Object selection) {
                        Log.i(TAG, "selection:" + selection);

                        //Get the user's selection result and set it in item
                        Pair result = (Pair)selection;
                        fromCal.setTimeInMillis((long)result.first);
                        item.setStartDate(fromCal);
                        toCal.setTimeInMillis((long)result.second);
                        item.setEndDate(toCal);

                        //Notify the adapter of the list that the data has changed.
                        adapter.notifyItemChanged(position);
                    }
                });
                
                //Show time selection box
                dialog.show(getSupportFragmentManager(), "DatePicker");
            };
        }
    });

    //Binding adapter
    list.setAdapter(adapter);
}

Effect display

[Android] materialdatepicker usage notes

Recommended Today

[dry goods!!] Understand redis cache penetration, breakdown and avalanche in three sentences

preface How to effectively understand and distinguish the differences between IDS penetration, breakdown and avalanche has always bothered me. In particular, penetration and breakdown are confused after a period of time. In order to effectively help the author himself and friends with the same troubles, distinguish these three scenes. The author summarizes somekey word, I […]