Inflating custom View in ListView according to position

醉酒当歌 提交于 2019-12-13 04:29:10

问题


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?


回答1:


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);
}



回答2:


Inflate custom layout outside convertView == null. Since, convertView will usually return a previously recycled view and hence, your custom view never gets inflated.



来源:https://stackoverflow.com/questions/16098886/inflating-custom-view-in-listview-according-to-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!