如何在 RecyclerView Adapter 中使用波斯日历

how can to use Persian Calander in RecyclerViewAdapter

我使用特定的日期库(https://github.com/mohamad-amin/PersianMaterialDateTimePicker)

现在我想在 Recycler View 的按钮中使用它。

现在我的问题是我必须在库代码中定义两个选项:

1 - 我应该参考 activity。

2 - 我需要使用 getSupport FragmentManager()。

但此页面已使用 Recycler View.Adapter 扩展,我不知道如何调用此页面上的这两个选项。

RecyclerViewAdapter 页面:

                        PersianCalendar persianCalendar = new PersianCalendar();

                    DatePickerDialog datePickerDialog = DatePickerDialog.newInstance( StudentList.this ,
                            persianCalendar.getPersianYear() ,
                            persianCalendar.getPersianMonth() ,
                            persianCalendar.getPersianDay());




                    datePickerDialog.show( getSupportFragmentManager() , "datePickerDialog" );

在这些图片中你可以很容易地理解我:

enter image description here

enter image description here

enter image description here

你activity必须实现TimePickerDialog.OnTimeSetListener并实现抽象方法onTimeSet:

    public class StudentList extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {


 @Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {

    }
}

onClick

里面
  DatePickerDialog datePickerDialog = DatePickerDialog.newInstance((((StudentList)view.getContext()),  persianCalendar.getPersianYear(),
                        persianCalendar.getPersianMonth(),
                        persianCalendar.getPersianDay());

或:

DatePickerDialog datePickerDialog =DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

            }
        },  persianCalendar.getPersianYear(),
            persianCalendar.getPersianMonth(),
            persianCalendar.getPersianDay());

您可以尝试在创建适配器实例时传递 Activity 的上下文:

//Code in your Activity
MyAdapter myAdapter = new MyAdapter(this, ...);

您的适配器:

private Context context;

    public MyAdapter(Context context, ...){
    this.context = context;
    //Rest of the constructor...
    }

通过这样做,您可以添加这样做:

datePickerDialog.show(context.getSupportFragmentManager(), "datePickerDialog");

您只需要在构造函数中传递一个 activity 上下文。请务必从活动中调用 new Adapter(this,...) 并从片段中调用 new Adapter(getActivity(),...)。

private Context context;

@Override
public void onClick(View v) {
FragmentManager manager = ((Activity) context).getFragmentManager();
FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager();
}

试试这个