I'm trying to integrate a timetable into my app. At first I used a normal GridLayout, but I want to connect school subjects. So I switched to StaggeredGridLayoutManager to set different heights to the items . Everything works, except for one thing. If a subject goes only for one lesson on a day, the first item in the next row is placed in the gap.
Here's what I mean:
This is what I expected
How it looks
Number 7 should be Monday, not Thursday.
This is in my ScheduleActivity to call my custom adapter
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.schedule_recycler_view);
recyclerView.setHasFixedSize(true);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(7, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
ScheduleAdapter rcAdapter = new ScheduleAdapter(subjects);
recyclerView.setAdapter(rcAdapter);
And here is my Adapter
public class ScheduleAdapter extends RecyclerView.Adapter<ScheduleAdapter.ViewHolder> {
private ArrayList<SubjectModel> items;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public TextView content;
public ViewHolder(View view) {
super(view);
cardView = (CardView) view.findViewById(R.id.schdule_card);
content = (TextView) view.findViewById(R.id.schedule_content);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public ScheduleAdapter(ArrayList<SubjectModel> items) {
this.items = items;
}
// Create new views (invoked by the layout manager)
@Override
public ScheduleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.schedule_card, parent, false);
return new ViewHolder(view);
}
public static float convertDpToPixel(float dp){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final SubjectModel subject = items.get(position);
int lessons = subject.getLessonCount();
int i = lessons*2-2;
int height = (int) (holder.itemView.getMinimumHeight()*lessons + convertDpToPixel(i));
holder.cardView.setMinimumHeight(height);
String text = subject.getSubjectName().substring(0, 2)+"\n"+subject.getSubjectRoom();
holder.content.setText(text);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return items.size();
}
}
Sry that the pictures are only linked. I am new here and still have no authorization to integrate the pictures directly.
Does anyone have an idea how I could solve the problem? I couldn't find anything yet.
Thanks for your help!