Reputation: 671
I want to use ViewBinding to work with Views in Fragment.
FragmentBlankBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = FragmentBlankBinding.inflate(inflater, container, false);
return binding.getRoot();
}
But when I try to get RecyclerView from that binding like this:
binding.notesRecyclerView.setAdapter(adapter);
I get NullPointerException:
java.lang.NullPointerException: Attempt to read from field 'androidx.recyclerview.widget.RecyclerView com.myapps.notes.databinding.FragmentBlankBinding.notesRecyclerView' on a null object reference
P.S. here's 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>
Upvotes: 30
Views: 55903
Reputation: 354
For Fragments following implementation of view binding works for me :
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
}
}
Upvotes: 0
Reputation: 556
Considering the below link
I prefer to implement it in this way. There is a little difference in limitations for manipulating binding.
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
}
}
Upvotes: 5
Reputation: 373
Simple and the best way
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();
}
}
Remember to always put return binding.getRoot()
at the end of onCreateView()
Upvotes: 0
Reputation: 79
If you're inflating your Fragment in the constructor, view binding can be done like this:
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()
}
}
Upvotes: 7
Reputation: 917
You can simply do this and if you avoid mismatch view error, you can destroy the view after a move or replace the fragment.
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
}
}
Upvotes: 1
Reputation: 7022
View bind in fragment using 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
}
}
Upvotes: 15
Reputation: 2550
This working fine for me:
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: ");
}
Upvotes: 42