动手写日历

动手写日历

看下最终效果

分析

1、 首先样式上是个Gridview,固定为7列的。
2、 我们需要计算出当前月的1号是周几,然后从第几个位置开始
3、 当前月份的天数
这样一来就简单不少了,接下来一步步来实现了。

代码

1、 xml布局部分的代码

<LinearLayout
    android:id="@+id/ll_rili_week"
    android:layout_width="match_parent"
    android:layout_height="39dp"
    android:orientation="horizontal"
    android:paddingRight="15dp"
    android:paddingLeft="15dp"
    android:gravity="center_vertical"
    app:layout_constraintTop_toBottomOf="@id/cl_nian"
    android:layout_marginTop="@dimen/line_height"
    android:background="@color/white">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="日"
        android:textColor="@color/cc1c1c2"
        android:textSize="@dimen/sp12"
        android:gravity="center"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="一"
        android:textColor="@color/cc1c1c2"
        android:textSize="@dimen/sp12"
        android:gravity="center"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="二"
        android:textColor="@color/cc1c1c2"
        android:textSize="@dimen/sp12"
        android:gravity="center"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="三"
        android:textColor="@color/cc1c1c2"
        android:textSize="@dimen/sp12"
        android:gravity="center"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="四"
        android:textColor="@color/cc1c1c2"
        android:textSize="@dimen/sp12"
        android:gravity="center"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="五"
        android:textColor="@color/cc1c1c2"
        android:textSize="@dimen/sp12"
        android:gravity="center"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="六"
        android:textColor="@color/cc1c1c2"
        android:textSize="@dimen/sp12"
        android:gravity="center"
        />

</LinearLayout>

<GridView
    android:id="@+id/grid_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="7"
    android:layout_below="@id/ll_rili_week"
    android:paddingRight="15dp"
    android:paddingLeft="15dp"
    app:layout_constraintTop_toBottomOf="@id/ll_rili_week"
    android:background="@color/white"
    />

2、 计算出当前月份的DAY_OF_WEEK

private void setDate(String dateStr) {
    // TODO Auto-generated method stub
    start = 0; //用 每月的一号是周几  来判断 从哪个 位移量 开始计算
    Calendar date = Calendar.getInstance();
    try {
        date.setTime(format.parse(dateStr));

        switch (date.get(Calendar.DAY_OF_WEEK)) {
            case 1: //周日
                start = 0;
                break;
            case 2: //周一
                start = 1;
                break;
            case 3:
                start = 2;
                break;
            case 4:
                start = 3;
                break;
            case 5:
                start = 4;
                break;
            case 6:
                start = 5;
                break;
            case 7:
                start = 6;
                break;
            default:
                break;
        }


    } catch (Exception e) {
        Log.e("TTT",e.getMessage());
        // TODO: handle exception
    }



    dateAdapter = new DateSignInAdapter(getActivity(), riqiBeanList,start,tv_nianfen.getText().toString()+"-"+tv_yuefen.getText().toString().replace("月","")+"-01");
    grid_date.setAdapter(dateAdapter);
    dateAdapter.notifyDataSetChanged();
}

3、 接下来就是在adapter中按要求排版和加样式了。上边我们已经计算出了start的值了,那么将其传到adapter中,在adapter中与position比较决定从哪个位置开始

if (position < start) {

        viewHolder.btnDate.setVisibility(View.INVISIBLE);


    } else {

        viewHolder.btnDate.setVisibility(View.VISIBLE);

    }
    viewHolder.btnDate.setText(position - start + 1+""); //显示 号数。。因为 positopn从0开始 所以 +1;

adapter中的代码比较灵活,根据自己需求写


以上是自己需求中临时写的一个简单的日历,如有疑问请留言讨论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值