Make the first element of a ListView in a different layout

I am trying to make a ListView with the first item displayed in a different layout , and the others in the general layout. Both layouts have the same elements, which in pairs have the same name. When I like it:

public View getView(int position, View convertView, ViewGroup parent) {        
    if(convertView == null) {
        if(position == 0){
            convertView = inflater.inflate(R.layout.article_list_top_item, parent, false);
            Log.d("ALA", "pos = " + position + ", inflated top");
        }
        else {
            convertView = inflater.inflate(R.layout.article_list_item, parent, false);
            Log.d("ALA", "pos = " + position + ", inflated normal");
        }
    }
    // setText, setBitmap etc here
    return convertView;
}

it did not work. From the magazine, which I could tell, inflater.inflatewas called 6 times, inflated top1 time and inflated normal5 times.

What is depicted, article [0] was in the layout article_list_top_item, and article [1] ~ [5] was in article_list_item.

Until this is normal, but the pattern repeats, which means that article [6], article [12], [18], ... were in the layout article_list_top_itemthat I did not want.

What can I do to ONLY the first article appeared in article_list_top_item ??

P.S. article_list_top_item.xml setText setImage .

else return convertView; //setText , .

, , , SwipeRefreshLayout

, .

+4
3

. , , , , :

private class FirstHolder {
    //add a field for each subview in view type 1
}
private class SecondHolder {
    //add a field for each subview in view type 2
}

getViewTypeCount()

@Override
public int getViewTypeCount() {
    return 2;
}

getItemViewType(int position). , .

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        return 0;
    } else {
        return 1;
    }
}

:

public View getView(int position, View convertView, ViewGroup parent) {
    if (position == 0) {
        FirstHolder holder;
        if (convertView == null) {
            holder = new FirstHolder();
            convertView = inflater.inflate(R.layout.whatever_firstlayout, parent, false);
            //for each field of holder find the subview
            convertView.setTag(holder);
        } else {
            holder = (FirstHolder) convertView.getTag();
        }
        //set the data in subview with holder fields
    } else {
        SecondHolder holder;
        if (convertView == null) {
            holder = new SecondHolder();
            convertView = inflater.inflate(R.layout.whatever_secondlayout, parent, false);
            //for each field of holder find the subview
            convertView.setTag(holder);
        } else {
            holder = (SecondHolder) convertView.getTag();
        }
        //set the data in subview with holder fields
    }
    return convertView;
}
+5

addHeaderView. :

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View head = inflater.inflate(R.layout.headerlayout, listview, false);
listview.addHeaderView(head);
+1

. @sonic , ListView, , getViewTypeCount() getItemViewType(int position). :

@Override
public int getViewTypeCount(){
    return 2;
}

@Override
public int getItemViewType(int pos){
    return pos > 0 ? 1 : 0;
}

public View getView(int position, View listItem, ViewGroup parent) {
    if(listItem == null) 
        listItem = inflater.inflate(getItemViewType(position) == 0 ?
                   R.layout.article_list_top_item : R.layout.article_list_item
                   , parent, false);
    // setText setBitmap etc. here
}

: , getViewTypeCount() getItemViewType(int position) , . getItemViewType(int position) , - , .

, . , , , , 0 getViewTypeCount() - 1? , ( ), ?

, .

0
source

Source: https://habr.com/ru/post/1599040/


All Articles