Search code examples
listviewlayoutput

Put layout into a listview at specific position after adapter is done


I've created a listview with adapter like this

enter image description here

now i need to put a layout after positions 3, 6, 9, etc. of the listview like this

enter image description here

Can anyone tell me how i can do that and show me an example?


Solution

  • your question is good enough just try to put your images inside the question and not on a link ;)

    here a simple solution that I propose to you :

    below each item (row) of your Listview put your view that needed to be add (define it in your xml file of your item) with a visibility as "GONE"

    and in your adapter of the listView put it as VISIBILE when the position are 3,6,9... : modulo (remainder after division) by 3 is equal 0

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(position % 3 == 0) {
            yourViewExtension.setVisibility(View.VISIBLE);
        }else {
            yourViewExtension.setVisibility(View.GONE);
    
        }
    }
    

    if any trouble leave a comment

    Good lluck