Android实现简单日历

Posted 叶落呱呱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android实现简单日历相关的知识,希望对你有一定的参考价值。

android实现简单打卡日历

日历

主要通过ViewPager2+GridView实现打卡日历

需求

服务器记录用户一年打卡状态,返回数据给客户端,客户端根据数据来显示用户近一年打卡日历.并且日历高度要自适应

解决方法

1.ViewPager2高度不能自适应,需要测量ViewPager2当前位置的高度,然后再进行设置高度。
2.GridView的高度不能自适应,日历肯定是7列,计算出有多少行,用行数*GridView item的高度,得到GridView高度

3.ViewPager2+GridView来日历展示

效果图

代码实现

activity_main布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="12dp"
    android:layout_marginEnd="12dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="39dp">

        <ImageView
            android:id="@+id/arrow_left"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_marginStart="28dp"
            android:layout_marginTop="12dp"
            android:src="@mipmap/icon_arrow_pink"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <TextView
            android:id="@+id/date_tv"
            android:layout_width="wrap_content"
            android:layout_height="22dp"
            android:layout_marginTop="9dp"
            android:fontFamily="sans-serif-medium"
            android:textColor="#FE6484"
            android:textSize="14dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/arrow_right"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="28dp"
            android:src="@mipmap/icon_arrow_grey"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FFE8E9" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="16dp"
            android:layout_marginTop="4dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="日"
            android:textColor="#FFB3BB"
            android:textSize="12dp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="16dp"
            android:layout_marginTop="4dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="一"
            android:textColor="#FFB3BB"
            android:textSize="12dp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="16dp"
            android:layout_marginTop="4dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="二"
            android:textColor="#FFB3BB"
            android:textSize="12dp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="三"
            android:textColor="#FFB3BB"
            android:textSize="12dp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="四"
            android:textColor="#FFB3BB"
            android:textSize="12dp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="五"
            android:textColor="#FFB3BB"
            android:textSize="12dp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="六"
            android:textColor="#FFB3BB"
            android:textSize="12dp"
            android:textStyle="normal" />
    </LinearLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vpContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FFE8E9" />


</LinearLayout>

布局效果图

MainActivity

数据:
通过Calendar类得到当天年月日,通过 calendar.add(Calendar.MONTH, -i) 并且通过ArrayList来记录1年内的Calendar

public class MainActivity extends AppCompatActivity 
    private ViewPager2 viewPager2;
    private CalendarAdapter calendarAdapter;
    private TextView dateTv;
    private ImageView arrowLeftImg, arrowRightImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initData();
    

    private void init() 
        viewPager2 = findViewById(R.id.vpContainer);
        //viewPager2的适配器
        calendarAdapter = new CalendarAdapter();
        viewPager2.setAdapter(calendarAdapter);
        dateTv = findViewById(R.id.date_tv);
        arrowLeftImg = findViewById(R.id.arrow_left);
        arrowRightImg = findViewById(R.id.arrow_right);
    

    private void initData() 
        List<Calendar> data = new ArrayList<>();
        //
        for (int i = 11; i >= 0; i--) 
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -i);
            data.add(calendar);
        

        calendarAdapter.refreshData(data);
        viewPager2.setCurrentItem(11, false);
        dateTv.setText(data.get(data.size() - 1).get(Calendar.YEAR) + "-" + (data.get(data.size() - 1).get(Calendar.MONTH) + 1));

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() 
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) 
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            

            @Override
            public void onPageSelected(int position) 
                super.onPageSelected(position);
                int year = data.get(position).get(Calendar.YEAR);
                int month = data.get(position).get(Calendar.MONTH) + 1;
                dateTv.setText(year + "-" + month);
                RecyclerView recyclerView = (RecyclerView) viewPager2.getChildAt(0);
                View view = recyclerView.getLayoutManager().findViewByPosition(position);
                if (view != null)
                    updatePagerHeightForChild(view, viewPager2);

            

            @Override
            public void onPageScrollStateChanged(int state) 
                super.onPageScrollStateChanged(state);
            

            public void updatePagerHeightForChild(View view, ViewPager2 pager) 
                view.post(() -> 
                    int weightMeasureSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
                    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    view.measure(weightMeasureSpec, heightMeasureSpec);
                    if (pager.getLayoutParams().height != view.getMeasuredHeight()) 
                        ViewGroup.LayoutParams layoutParams = pager.getLayoutParams();
                        layoutParams.height = view.getMeasuredHeight();
                        pager.setLayoutParams(layoutParams);
                    
                );

            
        );
        
        
        arrowRightImg.setOnClickListener(V -> 
            arrowRightImg.post(() -> 
                if (viewPager2.getCurrentItem() != 11)
                    viewPager2.setCurrentItem(viewPager2.getCurrentItem() + 1, false);
            );
        );
        arrowLeftImg.setOnClickListener(V -> 
            arrowLeftImg.post(() -> 
                if (viewPager2.getCurrentItem() != 0)
                    viewPager2.setCurrentItem(viewPager2.getCurrentItem() - 1, false);
            );
        );
       

    

测量ViewPager2的高度
ViewPager2的主要通过RecyclerView进行实现,通过得到RecyclerView的位置,来得到相应ViewPager2当前位置的View

RecyclerView recyclerView = (RecyclerView) viewPager2.getChildAt(0);
View view = recyclerView.getLayoutManager().findViewByPosition(position);

通过对ViewPager2监听,来得到当前位置j的view,然后要等view绘制完毕,来计算高度

    public void updatePagerHeightForChild(View view, ViewPager2 pager) 
                view.post(() -> 
                    int weightMeasureSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
                    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    view.measure(weightMeasureSpec, heightMeasureSpec);
                    if (pager.getLayoutParams().height != view.getMeasuredHeight()) 
                        ViewGroup.LayoutParams layoutParams = pager.getLayoutParams();
                        layoutParams.height = view.getMeasuredHeight();
                        pager.setLayoutParams(layoutParams);
                    
                );

            

ViewPager2的适配器


public class CalendarAdapter extends RecyclerView.Adapter<CalendarView> 
    private List<Calendar> calendar = new ArrayList<>();

    public CalendarAdapter() 

    

    @NonNull
    @Override
    public CalendarView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        return new CalendarView(LayoutInflater.from(parent.getContext()).inflate(R.layout.calender_view, parent, false));
    

    @Override
    public void onBindViewHolder(@NonNull CalendarView holder, int position) 
        if (calendar.size() != 0)
            holder.initData(calendar.get(position));
    

    @Override
    public int getItemCount() 
        return calendar.size();
    

    public void refreshData(List<Calendar> data) 
        for (int i = 0; i < data.size(); i++) 
            calendar.add(data.get(i));
        
        notifyDataSetChanged();

    

CalenderView(主要是Grid View)

布局

<?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="match_parent">

    <GridView
        android:id="@+id/wgvCalendar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="7"
        android:stretchMode="columnWidth" />
</LinearLayout>

逻辑

主要是GridView的处理
主要是通过Calendar得到每个月第一天为星期几和每个月有几天,通过集合对每个月数据进行添加,通过每个月第一天为星期几,来判断需要填充多少天空白数据

    public void initData(Calendar calendar) 
        ArrayList<DateBean> data = new ArrayList<>();
        //获取第一天是星期几然后计算出需要填充的空白数据
        for (int i = 0; i < getMonthOneDayWeek(calendar); i++) 
            //填充空白的
            data.add(new DateBean(0, Android开发中几种有用的的日历控件实现

做一个android日历,从哪儿入手?

android日历控件

android中怎么在日历中实现签到

如何通过单击一个简单的按钮在 android 中显示日历

Android开发之日历控件实现