上來就干侨颈,是我一貫的作風!
先預(yù)覽下效果圖
分析控件結(jié)構(gòu):
從上到下依次是TextView→GridView→GridView
布局代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<TextView
android:id="@+id/tvYear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="2017年7月"
android:gravity="center"
android:textColor="@android:color/white"
android:background="@color/colorAccent"/>
<com.lida.signdate.InnerGridView
android:id="@+id/gvWeek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="7">
</com.lida.signdate.InnerGridView>
<com.lida.signdate.InnerGridView
android:id="@+id/gvDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="7">
</com.lida.signdate.InnerGridView>
</LinearLayout>
1构眯、tvYear負責顯示年月陨收。
2、gvWeek負責顯示星期幾
3鸵赖、gvDate負責顯示日歷
把整體布局作為組合控件封裝在SignDate類里面务漩,SignDate主要代碼如下:
public class SignDate extends LinearLayout {
private TextView tvYear;
private InnerGridView gvWeek;
private InnerGridView gvDate;
private AdapterDate adapterDate;
public SignDate(Context context) {
super(context);
init();
}
...
private void init(){
View view = View.inflate(getContext(),R.layout.layout_signdate,this);
tvYear = view.findViewById(R.id.tvYear);
gvWeek = view.findViewById(R.id.gvWeek);
gvDate = view.findViewById(R.id.gvDate);
tvYear.setText(DateUtil.getCurrentYearAndMonth());
gvWeek.setAdapter(new AdapterWeek(getContext()));
adapterDate = new AdapterDate(getContext());
gvDate.setAdapter(adapterDate);
}
/**
* 簽到成功的回調(diào)
* @param onSignedSuccess
*/
public void setOnSignedSuccess(OnSignedSuccess onSignedSuccess){
adapterDate.setOnSignedSuccess(onSignedSuccess);
}
}
代碼中AdapterWeek就是簡單的設(shè)置星期幾數(shù)據(jù),這里不多介紹它褪。
在AdapterDate中實現(xiàn)了本月天數(shù)計算饵骨,開始天數(shù)應(yīng)該顯示在星期幾的位置,和點擊簽到實現(xiàn)簽到效果茫打,代碼如下:
public class AdapterDate extends BaseAdapter {
private Context context;
private List<Integer> days = new ArrayList<>();
//日歷數(shù)據(jù)
private List<Boolean> status = new ArrayList<>();
//簽到狀態(tài)居触,實際應(yīng)用中初始化簽到狀態(tài)可通過該字段傳遞
private OnSignedSuccess onSignedSuccess;
//簽到成功的回調(diào)方法妖混,相應(yīng)的可自行添加簽到失敗時的回調(diào)方法
public AdapterDate(Context context) {
this.context = context;
int maxDay = DateUtil.getCurrentMonthLastDay();//獲取當月天數(shù)
for (int i = 0; i < DateUtil.getFirstDayOfMonth() - 1; i++) {
//DateUtil.getFirstDayOfMonth()獲取當月第一天是星期幾,星期日是第一天轮洋,依次類推
days.add(0);
//0代表需要隱藏的item
status.add(false);
//false代表為簽到狀態(tài)
}
for (int i = 0; i < maxDay; i++) {
days.add(i+1);
//初始化日歷數(shù)據(jù)
status.add(false);
//初始化日歷簽到狀態(tài)
}
}
@Override
public int getCount() {
return days.size();
}
@Override
public Object getItem(int i) {
return days.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
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(i)+"");
if(days.get(i)==0){
viewHolder.rlItem.setVisibility(View.GONE);
}
if(status.get(i)){
viewHolder.tv.setTextColor(Color.parseColor("#FD0000"));
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 view) {
if(status.get(i)){
Toast.makeText(context,"Already sign in!",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"Sign in success!",Toast.LENGTH_SHORT).show();
status.set(i,true);
notifyDataSetChanged();
if(onSignedSuccess!=null){
onSignedSuccess.OnSignedSuccess();
}
}
}
});
return view;
}
class ViewHolder{
RelativeLayout rlItem;
TextView tv;
ImageView ivStatus;
}
public void setOnSignedSuccess(OnSignedSuccess onSignedSuccess){
this.onSignedSuccess = onSignedSuccess;
}
}
代碼實在簡單制市,沒有什么技術(shù)上的難點,初始化數(shù)據(jù)弊予、簽到失敗回調(diào)祥楣、界面效果等一些功能,大家可以自行補充汉柒。
工程源碼地址:https://github.com/StormFeng/SignDate