My question is based on a previous question about my Code - it's a newsfeed with different views which are scrollable vertically or horizontally or display various content (ads basically)
- VERTICAL VIEW
- HORIZONTAL VIEW
- AD VIEW
Now I managed with the help of Ben P, on my previous post to organize my views with the following code:
this.items = new ArrayList<>();
if (listVertical.size() > 0) {
items.add(listVertical.remove(0));
}
if (listHorizontal.size() > 0) {
items.add(listHorizontal.remove(0));
}
while (listVertical.size() > 0 || listAd.size() > 0) {
if (listAd.size() > 0) {
items.add(listAd.remove(0));
}
int count = 5;
while (count > 0 && listVertical.size() > 0) {
items.add(listVertical.remove(0));
}
}
This is way to static for a feel-alive newsfeed. So I tried organizing my views with a position element in my JSON, like this:
{
"horizontal": [{...,position:"1",},{...,position:"3",...},{...,position:"4",...}] ,
"vertical": [{...,position:"2",},{...,position:"5",},{...,position:"6",}]
"ad": [{...,position:"0",},{...,position:"7",},{...,position:"8",}]
}
And with this code here it should be possible to get the Feed in position:
int length_tot= vertical.size() + horizontal.size() + ad.size();
this.items = new ArrayList<>();
for(int i = 0; i < lenth_tot; i++) {
if(vertical.getallthepositions_somehow == i) {
items.add(vertical.remove(0));
}
if(horizontal.getallthepositions_somehow == i) {
items.add(horizontal.remove(0));
}
if(ad.getallthepositions_somehow == i) {
items.add(ad.remove(0));
}
}
As you can see, I'm stuck on looping through all Lists finding the next position to add in my items-List.
So my question is:
What's the fastest way to find the next position to add? My solution feels like it's a way too heavy, is there a more economical solution in your mind (from the point of view of the processor)?
Here is my onBindViewHolder
:
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder.getItemViewType() == VIEW_TYPE_VERTICAL)
verticalView((VerticalViewHolder) holder, position);
if (holder.getItemViewType() == VIEW_TYPE_HORIZONTAL)
horizontalView((HorizontalViewHolder) holder, position);
}
And the getItemViewType
from the adapter:
@Override
public int getItemViewType(int position) {
if (items.get(position) instanceof Vertical)
return VIEW_TYPE_VERTICAL;
if (items.get(position) instanceof Horizontal)
return VIEW_TYPE_HORIZONTAL;
}
EDIT:
So what I'm trying to get out of my JSON from the Server:
{
"horizontal": [{...,position:"1",},{...,position:"3",...},{...,position:"4",...}] ,
"vertical": [{...,position:"2",},{...,position:"5",},{...,position:"6",}]
"ad": [{...,position:"0",},{...,position:"7",},{...,position:"8",}]
}
is a Newsfeed with the following view-order:
- [AD] (position=0)
- [HORIZONTAL] (position =1)
- [VERTICAL] (position=2)
- [HORIZONTAL]
- [HORIZONTAL]
and so on...
So the server basically decides the order of my views in my adapter due the psoition item in each json-array
Ben P - Code for the right order in my List:
Object[] itemsObject = new Object[listVertical.size() + listHorizontal.size() + adList.size()];
for (Vertical item : listVertical) {
itemsObject[Integer.parseInt(item.getPosition())] = item;
}
for (Horizontal item : listHorizontal) {
itemsObject[Integer.parseInt(item.getPosition())] = item;
}
for (Adfeed item : adList) {
it
ArrayList<Object> itemsPos = new ArrayList<>();
itemsPos.addAll(listVertical);
itemsPos.addAll(listHorizontal);
itemsPos.addAll(adList);
Collections.sort(items, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return Integer.compare(o1.getPosition(),o2.getPosition())
}
});
Vertical, Horizontal and Adfeed contain .getPosition(). So how do I access them in my Comparator?