Reputation: 53
I have a list of let's say 20 items. I want to be able to load them like this
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20
I'm using a RecyclerView
with a GridLayoutManager
Using GridLayoutManager(context, 2)
, it loads the items as follows
1 2
3 4
5 6
7 8
....
Which isn't my desired output
Upvotes: 3
Views: 886
Reputation: 167
GridLayoutManager is not same as LinearLayout,default orientation is VERTICAL
,so you should set HORIZONTAL
, means that if space is not enough , more items will rank horizontally,like this:
recyclerView.setLayoutManager(new GridLayoutManager(activity, 2, GridLayoutManager.HORIZONTAL, false));
Upvotes: 0
Reputation: 1519
The simplest way to achieve this would be sort the ArrayList accordingly to the required order, you pass the your arraylist to rearrangeTheArrayList function get the new ArrayList and pass this to recycler view Adaptor to achieve above mentioned order .
public ArrayList<Integer> rearrangeTheArrayList(ArrayList<Integer> integerArrayList) {
ArrayList<Integer> resultArrayList = new ArrayList<>();
int halfLength = 0;
if (integerArrayList.size() % 2 == 1) {
halfLength = (integerArrayList.size() / 2) + 1;
} else {
halfLength = integerArrayList.size() / 2;
}
for (int i = 0; i < halfLength; i++) {
resultArrayList.add(integerArrayList.get(i));
if ( (i + halfLength)<(integerArrayList.size() )) {
resultArrayList.add(integerArrayList.get(i + halfLength));
}
}
return resultArrayList;
}
Upvotes: 2
Reputation: 1902
You may implement logic like this. It will work smoothly
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, pos: Int) {
var arraySize = 21
var half = 0
if (array % 2 == 0)
half = array % 2
else half = (array % 2 + 0.5).toInt()
if (pos % 2 == 0)
print(pos)
else print(half + pos)
}
Upvotes: 0
Reputation: 4443
The GridLayoutManager
always teachers the RecyclerView
to draw from left to right and up to down. The reason's default scale of this view is vertical (or horizontal). It can't calculate how many rows should it show in this dynamic case. I have a solution for you. However, you should not keep this business logic.
All you need is one adapter with two view types and simple logic for indexing the right thing. Sample code:
public class SampleAdapter extends RecyclerView.Adapter<ViewHolder> {
private final Context context;
private List<Integer> listLeft;
private List<Integer> listRight;
public SampleAdapter (Context context) {
this.context = context;
}
public void setListLeft(List<Integer> list) {
this.listLeft = list;
}
public void setListRight(List<Integer> list) {
this.listRight = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.sample, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
int index = position / 2; // assumes there are two lists of equal lengths
int viewType = getItemViewType(position);
if (viewType == TYPE_1) {
holder.textView.setText(listLeft.get(index).toString());
} else if (viewType == TYPE_2) {
holder.textView.setText(listRight.get(index).toString());
}
}
@Override
public int getItemCount() {
return listLeft.size() + listRight.size();
}
@Override
public int getItemViewType(int position) {
return position % 2 == 0 ? VIEW_TYPE_1 : VIEW_TYPE_2;
}
}
Upvotes: 0