一 需求分析
日历支持左右滑动
日历需要在具体日期下面展示签到情况的标识,例如 异常 正常
点击日期查看当天的签到情况、在日历下方以列表展示
二 图文展示
三 实现思路
1 最外层:scrollview包裹 需要自定义 避免滑动冲突
public class ObservableScrollView extends ScrollView {
private int downX;
private int downY;
private int mTouchSlop;
private OnScollChangedListener onScollChangedListener = null;
public ObservableScrollView(Context context) {
super(context);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public void setOnScollChangedListener(OnScollChangedListener onScollChangedListener) {
this.onScollChangedListener = onScollChangedListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (onScollChangedListener != null) {
onScollChangedListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downX = (int) e.getRawX();
downY = (int) e.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) e.getRawY();
if (Math.abs(moveY - downY) > mTouchSlop) {
return true;
}
default:
}
return super.onInterceptTouchEvent(e);
}
public interface OnScollChangedListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
}
2 日历部分: monthview + dayView组成 dayview与monthview的组成比较简单,可以通过自定义组件 或者 xml都可以实现
实现左右滑动:viewpager/flipper
/**
* ViewPager wrapContent解决方案
* 用于gradview *6 注意
*/
public class CalendarViewPager extends ViewPager {
private OnViewpagerListener onViewpagerListener;
public void setOnViewpagerListener(OnViewpagerListener onViewpagerListener) {
this.onViewpagerListener = onViewpagerListener;
}
public CalendarViewPager(Context context) {
super(context);
init();
}
public CalendarViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight() * 6;
if (h > height)
height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void init() {
//此处设置为500初始化
setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return 1000;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public int getItemPosition(@NonNull Object object) {
View view = (View) object;
int currentPage = getCurrentItem(); // Get current page index
if (currentPage == (Integer) view.getTag()) {
return POSITION_NONE;
} else {
return POSITION_UNCHANGED;
}
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
GridView gv = initCalendar(position);
gv.setTag(position);
container.addView(gv);
return gv;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
});
}
private GridView initCalendar(int position) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
GridView gView = new GridView(getContext());
gView.setLayoutParams(params);
gView.setNumColumns(7);
gView.setGravity(Gravity.CENTER);
gView.setSelector(new ColorDrawable(Color.TRANSPARENT));
gView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
DayAdapter dayAdapter = new DayAdapter(getContext());
gView.setAdapter(dayAdapter);
dayAdapter.setOnItemSelectListener(new DayAdapter.OnItemSelectListener() {
@Override
public void onSelectItem(String day) {
onViewpagerListener.onChildViewChange(day);
}
});
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, position - 500);
int y = calendar.get(Calendar.YEAR);
int m = calendar.get(Calendar.MONTH) + 1;
int d = calendar.get(Calendar.DATE);
List dayItemList = CalendarFactory.getMonthOfDayList(y, m);
dayAdapter.setList(dayItemList);
return gView;
}
public interface OnViewpagerListener {
void onChildViewChange(String day);
}
}
3 列表部分 listview/ recyclewview 根据当前的年月日请求后台数据
链接: https://download.csdn.net/download/qq_35001400/11328920