如何将AsyncLayoutInflater与导航组件和数据绑定一起使用?

时间:2019-09-22 10:11:58

标签: android android-fragments data-binding navigation

我的布局很复杂。充气需要几秒钟。所以我想使用AsyncLayoutInflater类对其进行充气。

public class VoicelessSoundFragment extends Fragment implements View.OnClickListener {
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Context context = getContext();
        new AsyncLayoutInflater(context)
                .inflate(R.layout.fragment_voiceless_sound, container,
                        (view, resid, parent) -> {
                            ViewDataBinding binding = DataBindingUtil.bind(view);
                            binding.setVariable(foo.bar.BR.onClickListener, this);
                            parent.removeAllViews();
                            parent.addView(binding.getRoot());
                        });
        TextView textView = new TextView(context);
        textView.setText("Loading...");
        return textView;
    }

    @Override
    public void onClick(View v) {
        MobileNavigationDirections.ActionGlobalKanaDetailFragment acton =
                MobileNavigationDirections.actionGlobalKanaDetailFragment((String) v.getTag());
        Navigation.findNavController(v).navigate(acton);
    }

}

但是我不知道将AsyncLayoutInflater与Navigation组件和Databinding一起使用的正确方法。当我单击“后退”按钮导航回去时,此片段与先前的片段重叠而不消失。另外,如果我通过AsyncLayoutInflater而不是DataBindingUtil来增加布局,则Textview的app:autoSizeTextType="uniform"属性将无法正常工作。

家庭片段:

Home Fragment

具有复杂布局(文本不是自动大小)的片段:

enter image description here

导航后的重叠:

enter image description here

0 个答案:

没有答案