Search code examples
javaandroidandroid-listviewandroid-custom-viewandroid-inflate

Inflating custom View in ListView according to position


I have a ListView where I would like to inflate a custom View at a certain position. It's actually the 11th item (position 10).

The getCount() method returns 11.

Here is the top part of the getView() method:

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {              
        if (position < 10) {
            convertView = mInflater.inflate(R.layout.standard, null);           
        } else {
            convertView = mInflater.inflate(R.layout.custom, null);
            Log.i(TAG, "Inflated custom layout");
        }
        Log.i(TAG, "getView, position = "+position);
        holder = new ViewHolder();
        holder.tv = (TextView) convertView.findViewById(R.id.tv);
        holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton1);             
        convertView.setTag(holder);             
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    ...
    Log.i(TAG, "getView() : position = " + position);
}

Here's what I see in the logcat:

getView, position = 0
getView, position = 1
getView, position = 2

The rest of the getView logs do NOT show; I would assume that this line would appear all the way up to getView, position = 10.

And this line: convertView = mInflater.inflate(R.layout.custom, null); is never called, because Inflated custom layout doesn't show up in the logcat.

It's funny though, because the bottom logcat is called always (as I scroll down the ListView):

getView() : position = 0
getView() : position = 1
getView() : position = 2
getView() : position = 3
getView() : position = 4
getView() : position = 5
getView() : position = 6
getView() : position = 7
getView() : position = 8
getView() : position = 9
getView() : position = 10

Why is my custom layout not inflated?


Solution

  • You need to inflate the layout when the getView() methods View convertView is not null. The View which is returned from getView() will be reused. If you check the view is null and inflate it will inflate only when its first time returning the View.

    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {              
            if (position < 10) {
                convertView = mInflater.inflate(R.layout.standard, null);           
            }
            Log.i(TAG, "getView, position = "+position);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.tv);
            holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton1);             
            convertView.setTag(holder);             
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ...
    
        if(position > 10) {
             convertView = mInflater.inflate(R.layout.custom, null);
             Log.i(TAG, "Inflated custom layout");
        }
    
        Log.i(TAG, "getView() : position = " + position);
    }