android 中两种适配器语法的不同
Different between two type of adapter syntax in android
模型一:
private Context mContext;
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate("layout name",parent, false);
}
模型二:
private Context mContext;
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate("layout name", null);
}
两个片段的区别:
convertView = inflater.inflate("layout name" , null);
和
convertView = inflater.inflate("layout name", parent, false);
不同之处在于,您可以为膨胀布局指定父元素,并可以控制膨胀布局是否应附加到父元素。您可以找到 LayoutInflaters here.
的文档
顺便说一句。您可以使用更易读的语法,例如:
final View viewToAdd = LayoutInflater.from(this).inflate(layoutId, null);
用 2 辆婴儿车充气:
inflate(int resource, ViewGroup root)
用 3 辆婴儿车充气:
inflate(int resource, ViewGroup root, boolean attachToRoot)
resource: int:要加载的 XML 布局资源的 ID(例如,R.layout.main_page)
root: ViewGroup:可选视图作为生成的层次结构的父视图(如果 attachToRoot 为真),或者只是一个为根提供一组 LayoutParams 值的对象返回的层次结构(如果 attachToRoot 为 false。)
attachToRoot: 布尔值:膨胀的层次结构是否应该附加到根参数?如果为 false,root 仅用于为 XML.
中的根视图创建正确的 LayoutParams 子类
模型一:
private Context mContext;
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate("layout name",parent, false);
}
模型二:
private Context mContext;
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate("layout name", null);
}
两个片段的区别:
convertView = inflater.inflate("layout name" , null);
和
convertView = inflater.inflate("layout name", parent, false);
不同之处在于,您可以为膨胀布局指定父元素,并可以控制膨胀布局是否应附加到父元素。您可以找到 LayoutInflaters here.
的文档顺便说一句。您可以使用更易读的语法,例如:
final View viewToAdd = LayoutInflater.from(this).inflate(layoutId, null);
用 2 辆婴儿车充气:
inflate(int resource, ViewGroup root)
用 3 辆婴儿车充气:
inflate(int resource, ViewGroup root, boolean attachToRoot)
resource: int:要加载的 XML 布局资源的 ID(例如,R.layout.main_page)
root: ViewGroup:可选视图作为生成的层次结构的父视图(如果 attachToRoot 为真),或者只是一个为根提供一组 LayoutParams 值的对象返回的层次结构(如果 attachToRoot 为 false。)
attachToRoot: 布尔值:膨胀的层次结构是否应该附加到根参数?如果为 false,root 仅用于为 XML.
中的根视图创建正确的 LayoutParams 子类