RecycleView实现Android自定义日历

 移动端有自带的控件  其实并不需要自己写 既然写了 就留个纪念吧  主要是在iptv上使用

分析:第一行是LinearLayout包含一个左箭头 两个TextView显示年和月 一个右箭头 其中左右箭头可以点击

第二行可以用RecycleView 其中GridLayoutManager的spanCount设置为7  把星期填上

第三行开始是一个RecycleView 与星期对齐

首先 我默认的是显示当前月份的日历 判断本月的周一是星期几

/**
     * 功能:返回星期 1:星期一,2:星期二 ... 6:星期六 0:星期日
     *
     * @param date
     * @return
     */
    public static int getWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int week = c.get(Calendar.DAY_OF_WEEK) - 1;
        return week;
    }

判断一个月有多少天 主要是判断二月份

    public static int judgeDay(int year) {
        Calendar c = Calendar.getInstance();
        c.set(year, 2, 1);// year年的3月1日
        c.add(Calendar.DAY_OF_MONTH, -1);//将3月1日往左偏移一天结果是2月的天数
        return c.get(Calendar.DAY_OF_MONTH);
    }
        int dayNum = 31;
        List<Integer> longList = Arrays.asList(1, 3, 5, 7, 8, 10, 12);
        List<Integer> shortList = Arrays.asList(4, 6, 9, 11);
        if (longList.contains(monthNew)) {
            dayNum = 31;
        } else if (shortList.contains(monthNew)) {
            dayNum = 30;
        } else {
            dayNum = DateUtils.judgeDay(yearNew) + 1;
        }

数据写入 因为第一行有空格 空格数我默认用0补齐 然后设置成INVISIBLE

因为设置的是左右显示的 所以在加载新的月份的时候 把上个月的list清空

        mDay.clear();
        for (int j = 0; j < week; j++) {
            mDay.add(0);
        }
        for (int i = 1; i <= dayNum; i++) {
            mDay.add(i);
        }
        mCalendarAdapter.notifyDataSetChanged();

因为后面要用 所以我再写一下完整的方法

public void onChangeData(int choiceYear, int choiceMonth) {
        Date date = DateUtils.parseDate(choiceYear + "-" + choiceMonth + "-01");
        int week = DateUtils.getWeek(date);
        int dayNum = 31;
        List<Integer> longList = Arrays.asList(1, 3, 5, 7, 8, 10, 12);
        List<Integer> shortList = Arrays.asList(4, 6, 9, 11);
        if (longList.contains(monthNew)) {
            dayNum = 31;
        } else if (shortList.contains(monthNew)) {
            dayNum = 30;
        } else {
            dayNum = DateUtils.judgeDay(yearNew) + 1;
        }
        mDay.clear();
        for (int j = 0; j < week; j++) {
            mDay.add(0);
        }
        for (int i = 1; i <= dayNum; i++) {
            mDay.add(i);
        }
        mCalendarAdapter.notifyDataSetChanged();
    }

左右按钮的点击事件:

            case R.id.iv_left_icon:
                int choiceYearL = Integer.parseInt(tvChoiceYear.getText().toString());
                int choiceMonthL = Integer.parseInt(tvChoiceMonth.getText().toString());
                choiceMonthL--;
                if (choiceYearL == yearNew) {
                    if (choiceMonthL < monthNew) {
                        showToast("选择日期不能早于今天");
                    } else {
                        tvChoiceMonth.setText(choiceMonthL + "");
                        onChangeData(choiceYearL, choiceMonthL);
                    }
                } else {
                    if (choiceMonthL == 0) {
                        choiceMonthL = 12;
                        choiceYearL--;
                    }
                    tvChoiceMonth.setText(choiceMonthL + "");
                    onChangeData(choiceYearL, choiceMonthL);
                }
                break;
            case R.id.iv_right_icon:
                int choiceYear = Integer.parseInt(tvChoiceYear.getText().toString());
                int choiceMonth = Integer.parseInt(tvChoiceMonth.getText().toString());
                choiceMonth++;
                if (choiceMonth > 12) {
                    choiceMonth = 1;
                    choiceYear = choiceYear + 1;
                }
                tvChoiceMonth.setText(choiceMonth + "");
                tvChoiceYear.setText(choiceYear + "");
                onChangeData(choiceYear, choiceMonth);
                break;

 

1. 首先,在布局文件中定义BottomSheet和RecycleView: ``` <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.bottomsheet.BottomSheetBehavior android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" app:behavior_hideable="true" app:behavior_peekHeight="0dp" app:layout_behavior="@string/bottom_sheet_behavior"> <LinearLayout android:id="@+id/bottom_sheet_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </com.google.android.material.bottomsheet.BottomSheetBehavior> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 2. 在Activity中,初始化BottomSheetBehavior和RecycleView: ``` private BottomSheetBehavior bottomSheetBehavior; private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new MyAdapter()); bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottom_sheet)); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); // 设置地图的触摸事件监听器,用于处理拖拽事件 AMap aMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); aMap.setOnMapTouchListener(new AMap.OnMapTouchListener() { @Override public void onTouch(MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) { if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) { bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); } } } }); } ``` 3. 在MyAdapter中,实现RecyclerView的内容: ``` public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { private List<String> data = new ArrayList<>(); public MyAdapter() { for (int i = 0; i < 20; i++) { data.add("Item " + i); } } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { holder.textView.setText(data.get(position)); } @Override public int getItemCount() { return data.size(); } } public class MyViewHolder extends RecyclerView.ViewHolder { public TextView textView; public MyViewHolder(@NonNull View itemView) { super(itemView); textView = itemView.findViewById(R.id.text_view); } } ``` 4. 在布局文件中定义item_layout.xml: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/listPreferredItemHeight" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Item" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout> ``` 5. 运行程序,拖动地图时,BottomSheet会自动展开,显示RecyclerView的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值