在RecyclerView中添加时间段,可以通过以下步骤实现:
示例代码如下:
public class TimeSlot {
private String startTime;
private String endTime;
// 其他属性和方法
public TimeSlot(String startTime, String endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
// getter和setter方法
}
public class TimeSlotAdapter extends RecyclerView.Adapter<TimeSlotAdapter.ViewHolder> {
private List<TimeSlot> timeSlots;
public TimeSlotAdapter(List<TimeSlot> timeSlots) {
this.timeSlots = timeSlots;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView startTimeTextView;
public TextView endTimeTextView;
public ViewHolder(View itemView) {
super(itemView);
startTimeTextView = itemView.findViewById(R.id.start_time_text_view);
endTimeTextView = itemView.findViewById(R.id.end_time_text_view);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_time_slot, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
TimeSlot timeSlot = timeSlots.get(position);
holder.startTimeTextView.setText(timeSlot.getStartTime());
holder.endTimeTextView.setText(timeSlot.getEndTime());
}
@Override
public int getItemCount() {
return timeSlots.size();
}
}
// 在Activity或Fragment中的代码
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private TimeSlotAdapter timeSlotAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<TimeSlot> timeSlots = new ArrayList<>();
timeSlots.add(new TimeSlot("9:00 AM", "10:00 AM"));
timeSlots.add(new TimeSlot("10:00 AM", "11:00 AM"));
// 添加更多时间段数据
timeSlotAdapter = new TimeSlotAdapter(timeSlots);
recyclerView.setAdapter(timeSlotAdapter);
}
}
这样,就可以在RecyclerView中显示时间段了。你可以根据实际需求,自定义时间段的布局和样式。
领取专属 10元无门槛券
手把手带您无忧上云