项目需求:公司出的项目需求是个预约系统,从周一到周天每天的预约情况,一天的预约中:每个节目使用的时间长短所占用的比例高度是不一样的,中间的间隔也是不一样的,总之根据需求可以自己定相关的距离。
上代码:
一:外层想直接用RecyclerView来实现GridView的模块功能,就用了GridManager
GridLayoutManager manager = new GridLayoutManager(this,7);
但是!!!!!后期调整的时候竟然发现里面的RecyclerView不能滑动!!!!!!!!然后各种搜材料,什么事件分发
addOnItemTouchListener中的拦截,自定义RecyclerView强制下发不消费,都不行,所以这个坑要注意,有大佬知道什么愿意欢迎讨论。
recyclerView.setNestedScrollingEnabled(true);这个属性还是要设置的,允许嵌套滑动。
外层Adapter
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
private List<WeekBean> mList;
private Context context;
public MyRecyclerAdapter(List<WeekBean> mList, Context context) {
this.mList = mList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.main_recycler_item,parent,false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
ViewHolder viewHolder= (ViewHolder) holder;
if(mList!=null&&mList.size()>0){
WeekBean weekBean = mList.get(i);
if(weekBean!=null){//这里是对子Adapter进行初始化
viewHolder.textView.setText(weekBean.getTime());
List<WeekBeans> list = weekBean.getList();
RecyclerItemAdapter recyclerAdapter = new RecyclerItemAdapter(context,list);
viewHolder.recyclerView.setAdapter(recyclerAdapter);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false);
viewHolder.recyclerView.setLayoutManager(linearLayoutManager);
}
}
}
@Override
public int getItemCount() {
return 7;//因为我就7条所以就写死了7条
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
RecyclerView recyclerView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.item_time);
recyclerView = itemView.findViewById(R.id.item_recycler);
}
}
二:子Adapter
public class RecyclerItemAdapter extends RecyclerView.Adapter<RecyclerItemAdapter.ViewHolder> {
List<WeekBeans> mList_item;
Context context;
private WeekBeans mWeekBeans = null;//中间替换使用
private int num = 0,marnum = 0;//占用多高,距离上一个多高
public RecyclerItemAdapter(Context context,List<WeekBeans> mList_item) {
this.context = context;
this.mList_item = mList_item;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_recycler_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
WeekBeans weekBeans = mList_item.get(position);
TextView textView = holder.textItem;
num = TimeVIewUtils.GetTextNum(weekBeans.getStart_at(),weekBeans.getEnd_at());
if(position>0){
mWeekBeans = mList_item.get(position-1);
}
if(mWeekBeans!=null){
marnum = TimeVIewUtils.GetTextDistence(weekBeans.getStart_at(),mWeekBeans.getEnd_at());
}else {
marnum = TimeVIewUtils.GetTextDistence_NullLast(weekBeans.getStart_at());
}
String message = TimeVIewUtils.GetTextMessage(weekBeans.getStart_at(),weekBeans.getEnd_at(),weekBeans.getTitle());
TimeVIewUtils.SetTextView(context,textView, num, marnum, message, position);
}
@Override
public int getItemCount() {
return mList_item!=null?mList_item.size():0;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView textItem;
ViewHolder(View view) {
super(view);
textItem = view.findViewById(R.id.item_recycler_item_text);
}
}
}
三:各种计算方法
//获取份额
public static int GetTextNum(String start_at,String end_at){
//"2020-04-29 10:00:00","2020-04-29 12:00:00",
int num = 0;
int start = Integer.parseInt(start_at.substring(11,13));
int end = Integer.parseInt(end_at.substring(11,13));
num = (end-start)*2;
return num;
}
//获取距离上一个的距离
public static int GetTextDistence(String next_start_at,String last_end_at){
int num = 0;
int start = Integer.parseInt(next_start_at.substring(11,13));
int end = Integer.parseInt(last_end_at.substring(11,13));
num = (start-end)*2;
return num;
}
public static int GetTextDistence_NullLast(String next_start_at){
int num = 0;
int start = Integer.parseInt(next_start_at.substring(11,13));
int end = 8;
num = (start-end)*2;
return num;
}
//对TextView的动态设置
public static TextView SetTextView(Context context,TextView textView,int num,int martop,String message,int color){
int top = 0;
child_height = 826/28;
int text_height = 0;
if(num>5){
text_height = num*child_height;
}else {
text_height = 4*child_height;
}
if(martop>5){//8
top = 10*child_height;//8
}else if(martop==0){
top=9;
}else {
top = martop*child_height;
}
textView.setGravity(Gravity.CENTER);
textView.setText(message);
textView.setMaxLines(3);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setPadding(15,0,15,0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(232, text_height);
params.setMargins(9,top, 9, 0);
textView.setLayoutParams(params);
if(color<4){
textView.setBackgroundResource(colors[color]);
}else {
textView.setBackgroundResource(colors[color-4]);
}
textView.setTextColor(Color.WHITE);
return textView;
}
另外还有一个字符串转星期几的Util
public static String getWeekDayNew(String time){
String Week = "";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
SimpleDateFormat sdf = new SimpleDateFormat("E");
try{
Date date = format.parse(time);
Week = sdf.format(date);
}catch (Exception e){
e.printStackTrace();
}
return Week;
}
好了,有啥问题可以留言交流啊,希望大佬可以多多指点

3万+

被折叠的 条评论
为什么被折叠?



