// Here is my fragment
public class BookAppointmentFragment extends Fragment {
TextView timetv,dateTv;
Button bookAppoinmentBtn,bookingPopup;
RelativeLayout datepickerLayout;
Calendar myCalendar;
DatePickerDialog datePickerDialog;
String strStartDate,strSelectedDate;
List<Boolean> numberList;
List<String>timeSlotList;
RecyclerView recyclerView;
TimeSlotAdapter recyclerGridAdapter;
ProgressBar progressBar;
public BookAppointmentFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.nestedscroll, container, false);
numberList=new ArrayList<>();
progressBar=view.findViewById(R.id.progressBar);
bookAppoinmentBtn = view.findViewById(R.id.bookAppoinment);
timetv = view.findViewById(R.id.timeTv);
datepickerLayout = view.findViewById(R.id.datepickerLayout);
recyclerView = view.findViewById(R.id.recyclerView);
for(int i=0;i<=17;i++){
numberList.add(false);
}
details();
return view;
}
public void details() {
myCalendar = Calendar.getInstance();
datepickerLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
datepickerLayout.setBackgroundResource(R.drawable.green_button);
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(getContext(),new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String strdate=year+"-"+(monthOfYear+1)+"-"+dayOfMonth;
String dateFormat=formateDateFromstring("yyyy-MM-dd","EE dd, MMM yyyy",strdate);
strStartDate=date_after;
new GetContacts().execute();
}
}, yy, mm, dd);
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
datePickerDialog.show();
}
});
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String currentStylistID=strStylistid;
String duration=stylistDetails2.getDuration().toString();
String url= "https://cloudfunctions.net/bokking?date="+strStartDate+"&duration="+duration+"¤tStylistID="+currentStylistID;
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if(jsonStr!=null){
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String str1clk=jsonObj.getString("1");
String str2clk=jsonObj.getString("2");
String str3clk=jsonObj.getString("3");
String str4clk=jsonObj.getString("4");
String str5clk=jsonObj.getString("5");
String str6clk=jsonObj.getString("6");
String str7clk=jsonObj.getString("7");
String str8clk=jsonObj.getString("8");
String str9clk=jsonObj.getString("9");
String str10clk=jsonObj.getString("10");
String str11clk=jsonObj.getString("11");
String str12clk=jsonObj.getString("12");
String str13clk=jsonObj.getString("13");
String str14clk=jsonObj.getString("14");
String str15clk=jsonObj.getString("15");
String str16clk=jsonObj.getString("16");
String str17clk=jsonObj.getString("17");
String str18clk=jsonObj.getString("18");
timeSlotList=new ArrayList<>();
timeSlotList.add(str1clk);
timeSlotList.add(str2clk);
timeSlotList.add(str3clk);
timeSlotList.add(str4clk);
timeSlotList.add(str5clk);
timeSlotList.add(str6clk);
timeSlotList.add(str7clk);
timeSlotList.add(str8clk);
timeSlotList.add(str9clk);
timeSlotList.add(str10clk);
timeSlotList.add(str11clk);
timeSlotList.add(str12clk);
timeSlotList.add(str13clk);
timeSlotList.add(str14clk);
timeSlotList.add(str15clk);
timeSlotList.add(str16clk);
timeSlotList.add(str17clk);
timeSlotList.add(str18clk);
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
int numberOfColumns = 3;
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), numberOfColumns));
recyclerGridAdapter = new TimeSlotAdapter(getContext(), timeSlotList,strSelectedDate,numberList,timetv);
recyclerView.setAdapter(recyclerGridAdapter);
progressBar.setVisibility(View.GONE);
}
}
}
// here is my recycler adapter
public class TimeSlotAdapter extends RecyclerView.Adapter {
private LayoutInflater mInflater;
private String strSelectedDate;
private List<Boolean> numberList;
private List<String> mData;
private TextView timetv;
public TimeSlotAdapter(Context context, List<String> data, String strSelectedDate, List<Boolean> numberList, TextView timetv) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.strSelectedDate = strSelectedDate;
this.numberList = numberList;
this.timetv=timetv;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
return new listViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
// to get current date
SimpleDateFormat currentDate = new SimpleDateFormat("dd/MM/yyyy");
Date todayDate = new Date();
String thisDate = currentDate.format(todayDate);
Log.d("date", thisDate);
if (thisDate.equals(strSelectedDate)) {
// to get current time
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Date currentLocalTime = cal.getTime();
String strTime = currentLocalTime.toString();
String strCurrenttime;
strCurrenttime = formateDateFromstring("EEEE MMM dd hh:mm:ss Z yyyy", "HH", strTime);
if (strCurrenttime.contains("00")) {
strCurrenttime = formateDateFromstring("EEEE MMM dd hh:mm:ss Z yyyy", "hh", strTime);
}
String strCurrentmin = formateDateFromstring("EEEE MMM dd hh:mm:ss Z yyyy", "mm", strTime);
Integer currentHour = Integer.parseInt(strCurrenttime);
Integer currentMin = Integer.parseInt(strCurrentmin);
String strCurrentTimeMin;
if (currentMin < 30) {
currentMin = 00;
String strmin = currentMin.toString();
strmin.concat("00");
strCurrentmin = strmin;
strCurrentTimeMin = strCurrenttime.concat(":").concat(strCurrentmin);
// currentMin=Integer.parseInt(strCurrentmin);
} else {
currentMin = 30;
String strmin = currentMin.toString();
strCurrentmin = strmin;
strCurrentTimeMin = strCurrenttime.concat(":").concat(strCurrentmin);
// currentMin=Integer.parseInt(strCurrentmin);
}
if (mData.get(position).equals("")) {
numberList.set(position, true);
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.inactive_slots);
((listViewHolder) holder).radioButton.setClickable(false);
} else {
String freeBusyHour = formateDateFromstring("HH:mm", "HH", mData.get(position));
Integer hour = Integer.parseInt(freeBusyHour);
if (hour < currentHour) {
numberList.set(position, true);
((listViewHolder) holder).radioButton.setText(mData.get(position));
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.inactive_slots);
((listViewHolder) holder).radioButton.setClickable(false);
} else {
((listViewHolder) holder).radioButton.setTextColor(Color.WHITE);
((listViewHolder) holder).radioButton.setText(mData.get(position));
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.active_slots);
}
if (strCurrentTimeMin.equals(mData.get(position))) {
((listViewHolder) holder).radioButton.setText(mData.get(position));
(((listViewHolder) holder).timeslot).setBackgroundResource(R.drawable.active_slots);
}
}
} else {
if (mData.get(position).equals("")) {
((listViewHolder) holder).radioButton.setText(mData.get(position));
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.inactive_slots);
} else {
((listViewHolder) holder).radioButton.setText(mData.get(position));
((listViewHolder) holder).radioButton.setTextColor(Color.WHITE);
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.active_slots);
}
}
}
@Override
public int getItemCount() {
return mData.size();
}
private class listViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView radioButton;
RelativeLayout timeslot;
String strStartTime;
public listViewHolder(View itemView) {
super(itemView);
radioButton = itemView.findViewById(R.id.radioBtton);
timeslot = itemView.findViewById(R.id.mslot1);
timeslot.setOnClickListener(this);
}
public void onClick(View view) {
if(numberList.get(getAdapterPosition()).equals(true)){
Toast.makeText(itemView.getContext(),"Grey timeslot's are already booked please choose some other timeslot",Toast.LENGTH_LONG).show();
}else {
timeslot.setBackgroundResource(R.drawable.green_button);
strStartTime=radioButton.getText().toString();
timetv.setText(strStartTime);
}
}
}
}
从datepicker中选择日期后,我在recyclerview中使用gridlayout管理器,我通过Firebase云函数传递了一些参数。我从云函数得到的响应是字符串。
如果任何字符串都像(“”)我应该禁用(灰色网格单元)网格单元和其他网格单元应处于活动状态(网格单元颜色为浅蓝色),我将填充网格中的所有字符串)。现在我的问题是,当我选择处于活动状态的网格单元格时,我必须突出显示该单元格。
这里我只能选择活动网格单元,应该是单选。当选择活动网格时,其他活动网格单元应为其他颜色,所选网格单元应为不同颜色。
另外,如果用户再次从日期选择器中选择日期,则我必须刷新网格,并且必须遵循相同的过程。
请帮助我如何实现这一目标,我在选择网格单元时会感到困惑。