RecyclerView 中getLayoutInflater().inflate() 设置layoutparam

在RecyclerView 动态设置view,通过getLayoutInflater().inflate(R.layout.schedule_foot, null);获取,因为inflater在inflate一个xml时,需要知道parent的类型,才能生成对应的LayoutParams,才可以把xml根节点的attrs(如layout_width)读进去,如果设置为null的话,在RecyclerView.addView时,发现LayoutParams为null,则生成默认的LayoutParams,

protected LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}

这里需要动态设置LayoutParams,代码如下:

 final View footView = getLayoutInflater().inflate(R.layout.schedule_foot, null);
        ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        footView.setLayoutParams(vlp);

你可能感兴趣的:(android开发技巧系列)