1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| package com.qingxiang.ui.view;
import android.content.Context; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.TextView;
import com.qingxiang.ui.R; import com.qingxiang.ui.bean.DateShowBean; import com.qingxiang.ui.common.CommonViewHolder; import com.qingxiang.ui.utils.Dateutils; import com.qingxiang.ui.utils.DensityUtils;
import java.util.List;
public class DateShowView extends FrameLayout implements View.OnClickListener { private TextView tvTitle; private RecyclerView rv; private MyAdapter mAdapter; private List<DateShowBean> days; private int year; private int month; private int day; private String selectTime;
public DateShowView(Context context) { this(context, null); }
public DateShowView(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public DateShowView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); }
private void init() { year = Dateutils.getYear(); month = Dateutils.getMonth(); day = Dateutils.getDay(); selectTime = Dateutils.getNowDate();
View.inflate(getContext(), R.layout.view_date_show_view, this); tvTitle = (TextView) findViewById(R.id.tv_title); rv = (RecyclerView) findViewById(R.id.id_rv); findViewById(R.id.iv_left).setOnClickListener(this); findViewById(R.id.iv_right).setOnClickListener(this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 7); rv.setLayoutManager(gridLayoutManager); mAdapter = new MyAdapter(); rv.setAdapter(mAdapter);
tvTitle.setText(selectTime); days = Dateutils.getDays(selectTime); mAdapter.changedData(); }
@Override public void onClick(View v) { String[] split = selectTime.split("\\."); int y = Integer.parseInt(split[0]); int m = Integer.parseInt(split[1]); switch (v.getId()) { case R.id.iv_left: { m--; if (m <= 0) { m = 12; y--; } break; } case R.id.iv_right: { m++; if (m >= 13) { m = 1; y++; } break; } } selectTime = y + "." + (m <= 9 ? "0" : "") + m; tvTitle.setText(selectTime); days = Dateutils.getDays(selectTime); mAdapter.changedData(); }
private class MyAdapter extends RecyclerView.Adapter<CommonViewHolder> {
@Override public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(getContext()); View view = inflater.inflate(R.layout.item_alarm_day, parent, false); return new CommonViewHolder(view); }
@Override public void onBindViewHolder(CommonViewHolder holder, int position) { DateShowBean day = days.get(position); if (day == null) { holder.getContentView().setVisibility(INVISIBLE); } else { holder.getContentView().setVisibility(VISIBLE); holder.setText(R.id.item_tv, day.nowDay); boolean isNow = false; String[] dateStr = selectTime.split("\\.");
int y = Integer.parseInt(dateStr[0]); int m = Integer.parseInt(dateStr[1]);
if (y == year && m == month) { int d = Integer.parseInt(day.nowDay); if (d == DateShowView.this.day) { isNow = true; } }
if (isNow) holder.getV(R.id.item_point).setVisibility(View.VISIBLE); else holder.getV(R.id.item_point).setVisibility(View.INVISIBLE); } }
@Override public int getItemCount() { return days == null ? 0 : days.size(); }
public void changedData() { int count = getItemCount(); int line = count / 7; if (count % 7 > 0) line++; LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rv.getLayoutParams(); layoutParams.height = line * DensityUtils.dp2px(getContext(), 34); rv.setLayoutParams(layoutParams); notifyDataSetChanged(); } }
}
|