Fragment中的ViewBinding

问题描述 投票:0回答:8

我想使用 ViewBinding 来处理 Fragment 中的视图。

FragmentBlankBinding binding;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    binding = FragmentBlankBinding.inflate(inflater, container, false);
    return binding.getRoot();
}

但是当我尝试从该绑定中获取 RecyclerView 时,如下所示:

binding.notesRecyclerView.setAdapter(adapter);

我得到 NullPointerException:

java.lang.NullPointerException:尝试从字段读取 'androidx.recyclerview.widget.RecyclerView com.myapps.notes.databinding.FragmentBlankBinding.notesRecyclerView' 在空对象引用上

附注这是fragment_blank.xml:

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

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/newNoteActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_margin="3dp"
        android:elevation="10dp"
        app:fab_icon="@drawable/ic_baseline_add_24" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/notesRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" /> 
</RelativeLayout>
android android-fragments android-viewbinding
8个回答
36
投票

这对我来说效果很好:

private FragmentJavaPracticeBinding binding;

@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentJavaPracticeBinding.inflate(inflater,container,false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        binding.notesRecyclerView.setVisibility(View.VISIBLE);//this hide/show recyclerview visibility 
        Log.d("TAG", "hidden: ");
    }

13
投票

使用 kotlin 查看片段中的绑定

class SignInFragment : Fragment() {

    private lateinit var bindingSignIn: SignInFragmentBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        //return inflater.inflate(R.layout.sign_in_fragment, container, false)

        // Inflate the layout for this fragment
        bindingSignIn = SignInFragmentBinding.inflate(inflater, container, false)

        bindingSignIn.txtMessage.text="Sample Text"

        return bindingSignIn.root
    }

}

6
投票

如果您在构造函数中膨胀 Fragment,则可以像这样完成视图绑定:

class ExampleFragment : Fragment(R.layout.fragment_example) {

    private var binding: FragmentExampleBinding? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding = FragmentExampleBinding.bind(view)
    }

    override fun onDestroyView() {
        binding = null
        super.onDestroyView()
    }

}

3
投票

考虑以下链接

开发者.android

我更喜欢用这种方式来实现。操作绑定的限制略有不同。

class SignInFragment : Fragment() {
private var _binding: FragmentSignInBinding? = null
private val binding get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {

    }
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = FragmentSignInBinding.inflate(inflater, container, false)
    return binding.root
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
 }
}

1
投票

您可以简单地执行此操作,如果避免不匹配视图错误,您可以在移动后销毁视图或替换片段。

class QuestionFragment : Fragment() {
    private var _binding: FragmentQuestionBinding? = null
    // This property is only valid between onCreateView and onDestroyView.
    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

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

        // Inflate the layout for this fragment
        _binding = FragmentQuestionBinding.inflate(inflater, container, false)
        val view = binding.root
        return view
    }

    override fun onDestroyView() {
        super.onDestroyView()
       _binding = null
    }
}

0
投票

简单又最好的方法

public class MainFragment extends Fragment {
    
    private FragmentMainBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMainBinding.inflate(getLayoutInflater(), container, false);

        // binding.yourWidget.set...

        return binding.getRoot();
    }
}

记住始终将

return binding.getRoot()
放在 onCreateView()
end


0
投票

对于以下片段,视图绑定的实现对我有用:

class HomeFragment : Fragment() {
    private lateinit var viewModel: HomeViewModel
    private lateinit var binding: FragmentHomeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = FragmentHomeBinding.inflate(layoutInflater)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        viewModel = ViewModelProvider(this)[HomeViewModel::class.java]
        binding = FragmentHomeBinding.inflate(inflater, container, false)

        binding.txtVw.text = menuItem.title
        binding.imgVwIcon.setImageResource(menuItem.iconId)

        return binding.root
    }
}

-1
投票

这是我发现将 ViewBinding 与 Fragments 结合使用的最佳实践。

使用 inflate(inflater,parent,attachToParent) 方法。我们在 onCreateView() 方法中进行布局膨胀和绑定。

之前

之后

  • 上图中你可以看到我们已经创建了一个绑定实例 称为绑定。

  • 我们还创建了一个fragmentBlankBinding实例 FragmentBlankBinding 并为其分配了一个绑定对象。

  • 由于绑定是本地对象,我们必须使用fragmentBlankBinding onCreateView() 方法之外的对象。

  • 我们已将fragmentBlankBinding声明为可为空,以便我们可以 需要时销毁绑定类的引用。

  • Fragment 的寿命比视图长,因此最好删除该实例 onDestroyView() 方法中的绑定类。

  • 您可以在上面的例子中看到,每次我们在使用fragmentBlankBinding对象时都必须进行空检查。我们将使用 getter 方法来改进这一点。

来源:https://medium.com/@abhineshchandra1234/view-binding-in-kotlin-android-bda2b35d3e29

© www.soinside.com 2019 - 2024. All rights reserved.