无法理解布局inflater?它有什么作用?为什么我们使用它?

时间:2017-08-08 04:16:27

标签: android layout-inflater

我无法理解布局inflater的逻辑 什么是adapetr类中的布局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);
        }

1 个答案:

答案 0 :(得分:0)

您可以浏览Android Official Documentation

LayoutInflater(将XML布局文件转换为相应的ViewGroups和Widgets)以及它在Fragment的onCreateView()方法中膨胀视图的方式。

LayoutInflater是一个用于将布局XML文件实例化为相应视图对象的类,可以在java程序中使用。