I need to modify the vertical LinearLayoutManager which I use with a RecyclerView so it can be scrolled horizontally.
When the RecyclerView is scrolled vertically it adds new items because I’m extending LinearLayoutManager.
I thought this will solve the problem.
public class CustomLayoutManager extends LinearLayoutManager {
public TableLayoutManager(Context context) {
super(context);
}
@Override
public boolean canScrollHorizontally() {
return true;
}
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
int delta = -dx;
offsetChildrenHorizontal(delta);
return -delta;
}
}
Now when I scroll the RecyclerView horizonatlly it works but the content can be scrolled offscreen.
Also when the content is scrolled horizontally and then I scroll vertically, the RecycerView will add the new views aligned with left screen border and not with initial views.
Video that demonstrates the behaviour:
http://youtu.be/FbuZ_jph7pw
What I'm doing wrong ?
UPDATE
(as suggested by @yigit)
Now I'm using the modified version of LinearLayoutManager (I'm not extending it anymore because I need to add offset to RecyclerView children in private method fill(....)).
These are the changes I made:
private int childrenLeftOffset = 0;
private int childrenMaxWith = 800; // I must update this value with children max width
@Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
int delta = -dx;
int newOffset = childrenLeftOffset + delta;
if (newOffset < 0) {
if(newOffset < -childrenMaxWith) {
//right edge reached
offsetChildrenHorizontal(-(childrenMaxWith - Math.abs(childrenLeftOffset)));
childrenLeftOffset = -childrenMaxWith;
delta = 0; // RecyclerView will draw right edge effect.
} else {
//scroll horizontally
childrenLeftOffset = childrenLeftOffset + delta;
offsetChildrenHorizontal(delta);
}
} else {
//left edge reached
offsetChildrenHorizontal(Math.abs(childrenLeftOffset));
childrenLeftOffset = 0;
delta = 0; // RecyclerView will draw left edge effect.
}
return -delta;
}
private int fill(RecyclerView.Recycler recycler, RenderState renderState, RecyclerView.State state, boolean stopOnFocusable) {
........
........
left = left + myLeftOffset;
layoutDecorated(view, left + params.leftMargin, top + params.topMargin, right - params.rightMargin, bottom - params.bottomMargin);
.......
.......
}
Scrolling CustomLinearLayout (updated video) http://youtu.be/DHeXkvpgruo
The problem now is to get the maximum width of RecyclerView children and update childrenMaxWith.