18

我想使用 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”读取

PS这里是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>
4

4 回答 4

21

这对我来说很好:

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: ");
    }
于 2020-07-17T12:33:13.030 回答
4

使用 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
    }

}
于 2021-03-05T11:01:26.013 回答
0

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

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
    }
}
于 2022-02-02T05:55:52.853 回答
0

如果你在构造函数中膨胀你的 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()
    }

}
于 2022-03-03T16:20:33.360 回答