最新消息:文章中包含代码时,请遵守代码高亮规范!

【原创】自定义布局的日期选择器

Android kyle 9234浏览 0评论

先上效果图:

Screenshot_2016-05-22-17-43-17

 

思路:日期:年月日,所以我用三个ListView联动来完成这个目的,至于如何联动,下面上代码:

datedialog.xml:(这里我多放了时和分,但是没有写)

<xml version="1.0" encoding="utf-8"&gt;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape2"
    android:orientation="vertical"
  >

   <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@drawable/shape"</View>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="选择开始时间"
        android:textSize="16sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:background="#F4F4F4"
        android:orientation="horizontal">

       <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:gravity="center"
            android:text="年"
            android:textColor="#CC3B0C" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="月"
            android:textColor="#CC3B0C" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="日"
            android:textColor="#CC3B0C" />

       <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="时"
            android:textColor="#CC3B0C" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="分"
            android:textColor="#CC3B0C" />
    </LinearLayout>
    <LinearLayout
        android:paddingBottom="100dp"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#FAFAFA"
        android:orientation="horizontal">
<RelativeLayout
    android:layout_width="0dp"
    android:layout_weight="1.5"
    android:layout_height="match_parent">
        <cn.zmit.mis.myapplication.MyListView
            android:id="@+id/lv_year"
            android:scrollbars="none"
            android:dividerHeight="0dp"
            android:divider="@null"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
      </cn.zmit.mis.myapplication.MyListView>

</RelativeLayout>
       <RelativeLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent">
           <;cn.zmit.mis.myapplication.MyListView
                android:id="@+id/lv_month"
                android:scrollbars="none"
                android:dividerHeight="0dp"
                android:divider="@null"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
               </cn.zmit.mis.myapplication.MyListView>
     </RelativeLayout>
<RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent">
       <cn.zmit.mis.myapplication.MyListView
            android:id="@+id/lv_day"
            android:scrollbars="none"
            android:dividerHeight="0dp"
            android:divider="@null"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           </cn.zmit.mis.myapplication.MyListView>
   </RelativeLayout>
<RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent">
       <cn.zmit.mis.myapplication.MyListView
            android:id="@+id/lv_hour"
            android:scrollbars="none"
            android:dividerHeight="0dp"
            android:divider="@null"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
          </cn.zmit.mis.myapplication.MyListView>
   </RelativeLayout>
<RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent">
   <cn.zmit.mis.myapplication.MyListView
            android:id="@+id/lv_minute"
            android:scrollbars="none"
            android:dividerHeight="0dp"
            android:divider="@null"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            &amp;lt;/cn.zmit.mis.myapplication.MyListView>
  </RelativeLayout>
</LinearLayout>
</LinearLayout>

item_date.xml:一个textview 很简单

<?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="match_parent">
<TextView
    android:id="@+id/tv_date"
    android:gravity="center"
    android:textSize="16sp"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:text="2"/>
</LinearLayout>

Constants.java:(包含着存储年月日textview的map)

package cn.zmit.mis.myapplication;

import android.widget.TextView;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/19 0019.
 */
public class Constance {
    public static Map&lt;Integer,TextView&gt;map_year=new HashMap&lt;&gt;();
    public static Map&lt;Integer,TextView&gt;map_month=new HashMap&lt;&gt;();
    public static Map&lt;Integer,TextView&gt;map_day=new HashMap&lt;&gt;();
    public static Map&lt;Integer,TextView&gt;map_hour=new HashMap&lt;&gt;();
    public static Map&lt;Integer,TextView&gt;map_minute=new HashMap&lt;&gt;();

}

MyAdapter:(适配器)

package cn.zmit.mis.myapplication;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by kyle on 2016/5/19 0019.
 */
public class MyAdapter extends BaseAdapter {

    private List&lt;String&gt;dates;
    private Context context;
    private int flag;//标识年月日,1为年,2为月,3为日

    public MyAdapter(Context context, List&lt;String&gt;dates, int flag) {
        this.context = context;
        this.dates = dates;
        this.flag = flag;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object getItem(int position) {
        return dates.get(position % dates.size());
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_date, null);
            holder = new MyHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.tv_date);
            convertView.setTag(holder);
        } else {
            holder = (MyHolder) convertView.getTag();
        }
        holder.textView.setText(dates.get(position % dates.size()));
        if (flag == 1) {
            Constance.map_year.put(position % dates.size(), holder.textView);
        } else if (flag == 2) {
            Constance.map_month.put(position % dates.size(), holder.textView);
        } else if (flag == 3) {
            Constance.map_day.put(position % dates.size(), holder.textView);
        }
        return convertView;
    }

    private class MyHolder {
        private TextView textView;
    }

    public void change(List&lt;String&gt;list) {
        dates = list;
        notifyDataSetChanged();
    }
}

TestActivity.java:(主程序)

package cn.zmit.mis.myapplication;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

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

/**
 * Created by KYLE on 2016/5/19 0019.
 */
public class TestActivity extends Activity {
    private MyListView lvYear;
    private MyListView lvMonth;
    private MyListView lvDay;
    private MyListView lvHour;
    private MyListView lvMinute;
    private MyAdapter adapterYear;
    private MyAdapter adapterMonth;
    private MyAdapter adapterDay;
    private MyAdapter adapterHour;
    private MyAdapter adapterMinute;
    private List&lt;String&gt;dates_year;
    private List&lt;String&gt;dates_month;
    private List&lt;String&gt;dates_day;

    private int year = 1902;
    private int month = 3;
    private int day = 3;
    public TestActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        Dialog dialog = new Dialog(this);
        View view = View.inflate(this, R.layout.datedialog, null);
        dialog.setContentView(view, new ViewGroup.LayoutParams(dip2px(this, 300), dip2px(this, 300)));
        lvYear = (MyListView) view.findViewById(R.id.lv_year);
        lvMonth = (MyListView) view.findViewById(R.id.lv_month);
        lvDay = (MyListView) view.findViewById(R.id.lv_day);
        lvHour = (MyListView) view.findViewById(R.id.lv_hour);
        lvMinute = (MyListView) view.findViewById(R.id.lv_minute);

        dates_year = new ArrayList&lt;&gt;();
        for (int i = 1900; i &lt;= 2100; i++) {
            dates_year.add(i + "");
        }
        adapterYear = new MyAdapter(this, dates_year, 1);
        lvYear.setAdapter(adapterYear);

        dates_month = new ArrayList&lt;&gt;();
        for (int i = 1; i&lt;= 12; i++) {
            dates_month.add(i + "");
        }
        adapterMonth = new MyAdapter(this, dates_month, 2);
        lvMonth.setAdapter(adapterMonth);

        dates_day = new ArrayList&lt;&gt;();
        for (int i = 1; i &lt;= 31; i++) {
            dates_day.add(i + "");
        }
        adapterDay = new MyAdapter(this, dates_day, 3);
        lvDay.setAdapter(adapterDay);

        dialog.show();
        setClick();
    }

    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private AbsListView.OnScrollListener onScrollListener_month;

    private void setClick() {

        lvYear.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == 0) {
                    year = Integer.parseInt(Constance.map_year.get((view.getFirstVisiblePosition() % dates_year.size() + 2) % dates_year.size()).getText().toString());
                    onScrollListener_month.onScrollStateChanged(lvMonth, 0);
                    lvYear.setSelection(view.getFirstVisiblePosition());
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (visibleItemCount == 5 || visibleItemCount == 6) {
                    Constance.map_year.get(firstVisibleItem % dates_year.size()).setTextColor(Color.parseColor("#d4d4d4"));
                    Constance.map_year.get((firstVisibleItem % dates_year.size() + 1) % dates_year.size()).setTextColor(Color.parseColor("#acacac"));
                    Constance.map_year.get((firstVisibleItem % dates_year.size() + 2) % dates_year.size()).setTextColor(Color.parseColor("#ffffff"));
                    Constance.map_year.get((firstVisibleItem % dates_year.size() + 3) % dates_year.size()).setTextColor(Color.parseColor("#acacac"));
                    Constance.map_year.get((firstVisibleItem % dates_year.size() + 4) % dates_year.size()).setTextColor(Color.parseColor("#d4d4d4"));
                }
            }
        });
        onScrollListener_month = new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == 0) {
                    month = Integer.parseInt(Constance.map_month.get((view.getFirstVisiblePosition() % dates_month.size() + 2) % dates_month.size()).getText().toString());
                    switch (month) {
                        case 2:
                            if (year % 4 == 0) {
                                dates_day = new ArrayList&lt;String&gt;();
                                for (int i = 1; i &lt;= 29; i++) {
                                    dates_day.add(i + "");
                                }
                                adapterDay.change(dates_day);
                            }
                            break;
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                            dates_day = new ArrayList&lt;String&gt;();
                            for (int i = 1; i &lt;= 31; i++) {
                                dates_day.add(i + "");
                            }
                            adapterDay.change(dates_day);
                            break;
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                            dates_day = new ArrayList&lt;String&gt;();
                            for (int i = 1; i &lt;= 30; i++) {
                                dates_day.add(i + "");
                            }
                            adapterDay.change(dates_day);
                            break;

                    }
                    if (month == 2) {
                        if (year % 4 == 0) {
                            dates_day = new ArrayList&lt;String&gt;();
                            for (int i = 1; i &lt;= 29; i++) {
                                dates_day.add(i + "");
                            }
                            adapterDay.change(dates_day);
                        } else {
                            dates_day = new ArrayList&lt;String&gt;();
                            for (int i = 1; i &lt;= 28; i++) {
                                dates_day.add(i + "");
                            }
                            adapterDay.change(dates_day);
                        }
                    }
                    lvMonth.setSelection(view.getFirstVisiblePosition());
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (visibleItemCount == 5 || visibleItemCount == 6) {
                    Constance.map_month.get(firstVisibleItem % dates_month.size()).setTextColor(Color.parseColor("#d4d4d4"));
                    Constance.map_month.get((firstVisibleItem % dates_month.size() + 1) % dates_month.size()).setTextColor(Color.parseColor("#acacac"));
                    Constance.map_month.get((firstVisibleItem % dates_month.size() + 2) % dates_month.size()).setTextColor(Color.parseColor("#ffffff"));
                    Constance.map_month.get((firstVisibleItem % dates_month.size() + 3) % dates_month.size()).setTextColor(Color.parseColor("#acacac"));
                    Constance.map_month.get((firstVisibleItem % dates_month.size() + 4) % dates_month.size()).setTextColor(Color.parseColor("#d4d4d4"));
                }
            }
        };
        lvMonth.setOnScrollListener(onScrollListener_month);
        lvDay.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == 0) {
                    lvDay.setSelection(view.getFirstVisiblePosition());
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (visibleItemCount == 5 || visibleItemCount == 6) {
                    Constance.map_day.get(firstVisibleItem % dates_day.size()).setTextColor(Color.parseColor("#d4d4d4"));
                    Constance.map_day.get((firstVisibleItem % dates_day.size() + 1) % dates_day.size()).setTextColor(Color.parseColor("#acacac"));
                    Constance.map_day.get((firstVisibleItem % dates_day.size() + 2) % dates_day.size()).setTextColor(Color.parseColor("#ffffff"));
                    Constance.map_day.get((firstVisibleItem % dates_day.size() + 3) % dates_day.size()).setTextColor(Color.parseColor("#acacac"));
                    Constance.map_day.get((firstVisibleItem % dates_day.size() + 4) % dates_day.size()).setTextColor(Color.parseColor("#d4d4d4"));
                }
            }
        });
    }
}

代码并不复杂,使用OnScrollListener滑动时判断firstVisibleItem 然后给Textiew改变颜色,在滑动结束时,改变Day的adapter的数据。

 

 

 

 

转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/16443.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者kyle的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!