带有AsyncLayoutInflater的Android片段onCreateView

时间:2019-08-20 14:49:40

标签: android android-fragments

发现我的onCreateView用了将近一秒钟的时间来执行inflater.inflate(...)方法后,我进行了一些搜索,找到了一个新的类AsyncLayoutInflater

我试图在片段中实现此构造函数,但由于总是产生空白视图,因此我无法弄清楚如何正确实现它。

我认为我的问题是如何对从onCreateView返回的空视图(super结果)中的片段进行充气。

这是有效的“普通”版本:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_intervento_luogo, container, false);
    //do my stuffs
    return view;
}

这不是,并且显示为空视图:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    AsyncLayoutInflater asyncLayoutInflater = new AsyncLayoutInflater(getContext());
    asyncLayoutInflater.inflate(R.layout.fragment_intervento_luogo, container, new AsyncLayoutInflater.OnInflateFinishedListener() {
        @Override
        public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent) {
            // do my stuffs
        }
    });
    return super.onCreateView(inflater, container, savedInstanceState);
}

我也尝试将这种异步充气机放在onResume,onViewCreated和onCreate内,但没有人起作用。

我应如何正确实施?我不敢相信没有办法跳过onCreateView内部的通货膨胀。

谢谢

2 个答案:

答案 0 :(得分:1)

您可以做的是创建一个带有 FrameLayout 和 ProgressBar 的 loading_view.xml 以在加载其他布局时显示它:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</FrameLayout>

现在您要异步加载另一个布局并将其附加到 loading_view。您还想在加载布局后隐藏 ProgressBar。

   override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)

        // inflate loading view
        val final_view = inflater.inflate(R.layout.loading_view, container, false)
        // get progressbar
        var progressBar = final_view.findViewById<ProgressBar>(R.id.progressBar)
        // initialize async layout inflater
        val asyncLayoutInflater = context?.let { AsyncLayoutInflater(it) }
        // inflate other layout
        asyncLayoutInflater?.inflate(R.layout.fragment_layout, null) { view, resid, parent ->
            // when layout loaded show...
            (final_view as? ViewGroup)?.addView(view) // add view to already inflated view
        }

        return final_view
    }

答案 1 :(得分:-1)

在onCreateView(...)内,您创建并返回片段的布局。当前,您返回super.onCreateView(inflater,container,savedInstanceState);因此为空白,当您的AsyncLayoutInflater完成时,不会影响您的片段布局。

当布局膨胀时-将其添加到您的ViewGroup中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    AsyncLayoutInflater asyncLayoutInflater = new AsyncLayoutInflater(getContext());
    asyncLayoutInflater.inflate(R.layout.fragment_intervento_luogo, container, new AsyncLayoutInflater.OnInflateFinishedListener() {
        @Override
        public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent) {
            parent.addView(view);
            onViewCreated(view, savedInstanceState)
        }
    });
    return super.onCreateView(inflater, container, savedInstanceState);
}

,还添加onViewCreated来通知系统创建了什么布局。

像这样的代码对我有用:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val asyncLayoutInflater = AsyncLayoutInflater(context!!)
    asyncLayoutInflater.inflate(R.layout.fragment_characters, container) { view, resid, parent ->
        binding = FragmentCharactersBinding.bind(view)
        binding.vm = vm
        container?.addView(binding.root)
        onViewCreated(binding.root, savedInstanceState)
    }

    return super.onCreateView(inflater, container, savedInstanceState)
}