无法理解布局充气器?它的作用是什么?为什么我们使用它?
Can't able to understand layout inflater?What it does? and why we use it?
我看不懂layout inflater的逻辑
什么是在 adapetr class.
中使用布局 inflater
public class WordAdapter extends ArrayAdapter<Word> {
public WordAdapter(Activity context, ArrayList<Word> words) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, words);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
您可以通过 Android Official Documentation.
LayoutInflater(将 XML 布局文件转换为相应的 ViewGroups 和 Widgets)以及它在 Fragment 的 onCreateView() 方法中膨胀视图的方式。
LayoutInflater 是一个 class 用于将布局 XML 文件实例化到其相应的视图对象中,可以在 java 程序中使用。
我看不懂layout inflater的逻辑 什么是在 adapetr class.
中使用布局 inflaterpublic class WordAdapter extends ArrayAdapter<Word> {
public WordAdapter(Activity context, ArrayList<Word> words) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, words);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
您可以通过 Android Official Documentation.
LayoutInflater(将 XML 布局文件转换为相应的 ViewGroups 和 Widgets)以及它在 Fragment 的 onCreateView() 方法中膨胀视图的方式。
LayoutInflater 是一个 class 用于将布局 XML 文件实例化到其相应的视图对象中,可以在 java 程序中使用。