开发者

Creating View manually

How can I create convertView manually in following met开发者_StackOverflow社区hod. I read that convertView can be created manually or inflated from xml file.

public View getView(int position, View convertView, ViewGroup parent)

I am using xml file for layout.


Use View.inflate(context, resource, root);

@Override
public View getView(int position, View convertView, ViewGroup parent){
    final View contentView = convertView != null ? convertView : View.inflate(context, resource, null);
}

where resource is your xml layout resource id like R.layout.list_item. Something like that.


You can use this code inside the override getView method while extending the ArrayAdapter Class

if(convertView == null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
    }

R.layout.list_item is the resource to create the view based on.

the parameter "false" is false to weather to attach to the root.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜