Answers for "how to use view binding in fragment"

1

binding fragment android

example:

public View onCreateView(LayoutInflater inflater, 
                         @Nullable ViewGroup container, 
                         @Nullable Bundle savedInstanceState) {
    MartianDataBinding binding = DataBindingUtil.inflate(
            inflater, R.layout.martian_data, container, false);
    View view = binding.getRoot();
    //here data must be an instance of the class MarsDataProvider
    binding.setMarsdata(data);
    return view;
}
Posted by: Guest on January-28-2021
1

use view binding in fragment

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: ");
    }
Posted by: Guest on July-05-2021
2

kotlin binding views to activity

// Adding this to your build.gradle (Module level)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    //Added:
    id 'kotlin-kapt'
    id 'kotlin-android-extensions'
}


// And having this in your layout
<Button
	android:id="@+id/btn_finish"
    (...)

// You ca use this in the .kt file
btn_finish.setOnClickListener {
	// Do Something
}
Posted by: Guest on December-13-2020

Code answers related to "how to use view binding in fragment"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language