今天编写了日历的显示,用了自定义控件,中途遇到的问题,一直显示Binary XML file line #9: Error inflating class错误,晚上查阅资料http://blog.csdn.net/huangxiaohu_coder/article/details/8497286,根据上面的原因,排除了出错原因。但是依旧没有发现自己的问题所在,这个bug弄的好心累呀。明天继续加油。

相关代码:

package com.example.keeprunning7;

import android.content.Context;
import android.graphics.Color;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class AdapterDate extends BaseAdapter {

    private Context context;
    private List<Integer> days=new ArrayList<>();

    public void setOnSignedSuccess(OnSignedSuccess onSignedSuccess){
        this.onSignedSuccess=onSignedSuccess;
    }

    //日历数据
    private List<Boolean> status=new ArrayList<>();
    //签到状态,实际应用中初始化签到状态可通过改字段传递
    private OnSignedSuccess onSignedSuccess;
    //签到成功的回调方法,相应的可自行添加签到始白时的回调函数
    public AdapterDate(Context context){
        this.context=context;
        ///获取当月天数
        int maxDay= DateUtil.getCurrentMonthLastDay();
        for(int i=0;i<DateUtil.getFirstDayOfMonth()-1;i++){
            days.add(0);//0代表需要隐藏的item
            status.add(false);//false为签到状态
        }

        for(int i=0;i<maxDay;i++){
            //初始化日历数据
            days.add(i+1);
            //初始化日历签到状态
            status.add(false);
        }
    }

    @Override
    public int getCount() {
        return days.size();
    }

    @Override
    public Object getItem(int position) {
        return days.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder{
        RelativeLayout rlItem;
        TextView tv;
        ImageView ivStatus;
    }
    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_gv, null);
            viewHolder = new ViewHolder();
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.tv = view.findViewById(R.id.tvWeek);
        viewHolder.rlItem=view.findViewById(R.id.rlItem);
        viewHolder.ivStatus=view.findViewById(R.id.ivStatus);
        viewHolder.tv.setText(days.get(position)+"");
        if(days.get(position)==0){
            viewHolder.rlItem.setVisibility(View.GONE);
        }
        if(status.get(position)){
            viewHolder.tv.setTextColor(Color.parseColor("#DF0000"));
            viewHolder.ivStatus.setVisibility(View.VISIBLE);
        }else{
            viewHolder.tv.setTextColor(Color.parseColor("#666666"));
            viewHolder.ivStatus.setVisibility(View.GONE);
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(status.get(position)){
                    Toast.makeText(context,"Already sign in!",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(context,"Sign in success!",Toast.LENGTH_SHORT).show();
                    status.set(position,true);
                    notifyDataSetChanged();
                    if(onSignedSuccess!=null){
                        onSignedSuccess.OnSignedSuccess();
                    }
                }
            }
        });
        return view;
    }

}
View Code
package com.example.keeprunning7;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AdapterWeek extends BaseAdapter {

    private String[] week={"日","一","二","三","四","五","六"};
    private Context context;

    public AdapterWeek(Context context){
        this.context=context;
    }
    @Override
    public int getCount() {
        return week.length;
    }

    @Override
    public Object getItem(int position) {
        return week[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder{
        TextView tv;
    }
    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder=null;
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.item_gv,null);
            viewHolder=new ViewHolder();
            view.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder)view.getTag();
        }
        viewHolder.tv=view.findViewById(R.id.tvWeek);
        viewHolder.tv.setText(week[position]);
        return view;
    }
}
View Code
package com.example.keeprunning7;

import java.util.Calendar;
import java.util.concurrent.CancellationException;

public class DateUtil {
    public static int getCurrentMonthLastDay(){
        Calendar a=Calendar.getInstance();
        //把日期设置为当月第一天
        a.set(Calendar.DATE,1);
        //把日期回滚一天,也就是最后一天
        a.roll(Calendar.DATE,-1);

        int maxDate=a.get(Calendar.DATE);
        return maxDate;

    }

    public static String getCurrentYearAndMonth(){
        Calendar a=Calendar.getInstance();
        int year=a.get(Calendar.YEAR);
        int month=a.get(Calendar.MONTH)+1;
        return year+"年"+month+"月";
    }

    //获取当月第一天是星期几,星期日是第一天
    public static int getFirstDayOfMonth(){
        Calendar a=Calendar.getInstance();
        a.set(Calendar.DAY_OF_MONTH,1);
        int i=a.get(Calendar.DAY_OF_WEEK);
        return i;
    }
}
View Code

 

posted on 2020-04-22 23:09  阡陌祁画  阅读(113)  评论(0编辑  收藏  举报