You are looking for different View types. It's possible with using these
GetViewTypeCount()
this is an overridable method which returns how many view type you have in your listview-recycleview.
getItemViewType(int pos)
get which item view type should return at the given position
For example if you want to add a different view in every 10 item, and there is only 2 type of views, you should use a code like this:
@Override
public int getItemViewType(int position){
if (position % 10 == 0)
return SECOND_ITEM;
return FIRST_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
And at the getView()
function you can handle it with a switch-case or if-else structure like this:
switch (getItemViewType(cursor.getPosition())) {
case SECOND_ITEM:
//something to do
break;
default:
//something to do
}
You might want to use 2 different layout file to inflate in the switch-case statement above. However, if the layouts of both items are not different that much, I recommend to create just 1 layout, and make views inside it visible and gone according to item you want to get.
Oh and in case you don't know where to use them, you use them at your adapter class. The functions may vary as which kind of adapter you use however, they all work with the same logic.
And finally, I recommend you to use recyclerview. It is just a bit harder to implement than listview but it is a great substitution of listview which is more powerful and flexible. You can do a research for it.
Here is how you can achieve this like instagram, facebook etc. : You inflate a scrollable view at the given positions.
I hope the answer helps.
As always,
Have a nice day.