1. Introduction
This article is a simple encapsulation used by databinding, mainly in the base classes BaseActivity and BaseFragment
2. Specific steps
1. Turn on databinding in
dataBinding { enabled = true }
2. The encapsulation in BaseActivity is mainly obtained through reflection. as follows
package ; import ; import ; import ; import ; import ; import ; import ; import ; /** * describe: * @author: zw * @time: 2023/5/22 17:33 */ public abstract class BaseActivity<VB extends ViewDataBinding> extends AppCompatActivity { public VB mBinding; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { (savedInstanceState); createBinding(); } private void createBinding() { Class<VB> vbClass = getVbClass(); try { if (vbClass == null) { return; } Method method = ("inflate", ); mBinding = (VB) (null, getLayoutInflater()); setContentView(()); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { (); } } private Class<VB> getVbClass() { ParameterizedType parameterizedType = (ParameterizedType) ().getGenericSuperclass(); if (parameterizedType == null) { return null; } Class<VB> vbClass = (Class<VB>) ()[0]; return vbClass; } }
3. Use the following in Activity
package ; import ; import ; import ; import ; import ; /** * describe: * @author: zw * @time: 2023/5/22 17:33 */ public class TestActivity extends BaseActivity<ActivityTestBinding> { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { (savedInstanceState); ("Test java activity"); (v -> { initFragment(); }); } private void initFragment() { FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = (); ((), new TestFragment()); (); } }
4. Encapsulation in BaseFragment
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * describe: * @author: zw * @time: 2023/5/22 17:33 */ public abstract class BaseFragment<VB extends ViewDataBinding> extends Fragment { public VB mBinding; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { createBinding(inflater, container); return (); } private void createBinding(LayoutInflater inflater, ViewGroup container) { Class<VB> vbClass = getVbClass(); try { if (vbClass == null) { return; } /* Method method = ("inflate", , , );*/ Method method = ("inflate", ); mBinding = (VB) (null, inflater); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { (); } } private Class<VB> getVbClass() { ParameterizedType parameterizedType = (ParameterizedType) ().getGenericSuperclass(); if (parameterizedType == null) { return null; } return (Class<VB>) ()[0]; } }
5. The use of Fragment is as follows
package ; import ; import ; import ; import ; import ; /** * describe: * @author: zw * @time: 2023/5/22 17:33 */ public class TestFragment extends BaseFragment<FragmentTestBinding> { @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { (view, savedInstanceState); ("Test java Fragment"); } }
Version
package import import import import import /** * describe: * @author: zw * @time: 2023/5/22 17:33 */ abstract class BaseActivityKotlin<VB : ViewDataBinding> : AppCompatActivity() { public lateinit var mBinding: VB override fun onCreate(savedInstanceState: Bundle?) { (savedInstanceState) mBinding = createBinding() setContentView() } private fun createBinding(): VB { val vbClass = getVBClass() val method = ("inflate", LayoutInflater::) return (null, layoutInflater) as VB } @Suppress("UNCHECKED_CAST") private fun getVBClass(): Class<VB> { val type = as ParameterizedType return [0] as Class<VB> } }
package import import /** * describe: * @author: zw * @time: 2023/5/22 17:33 */ class TestActivityKotlin: BaseActivityKotlin<ActivityTestKotlinBinding>() { override fun onCreate(savedInstanceState: Bundle?) { (savedInstanceState) = "Test Kotlin Activity" { initFragment() } } private fun initFragment() { val fm = supportFragmentManager val ft = () (, TestFragmentKotlin()) () } }
package import import import import import import import /** * describe: * @author: zw * @time: 2023/5/22 17:33 */ abstract class BaseFragmentKotlin<VB : ViewDataBinding> : Fragment() { public lateinit var mBinding: VB override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { mBinding = createBinding(inflater, container) return } private fun createBinding(inflater: LayoutInflater, container: ViewGroup?): VB { val vbClass = getVBClass() val method = ( "inflate", LayoutInflater::, ViewGroup::, Boolean:: ) return (null, inflater, container, false) as VB } @Suppress("UNCHECKED_CAST") private fun getVBClass(): Class<VB> { val type = as ParameterizedType return [0] as Class<VB> } }
package import import import /** * describe: * @author: zw * @time: 2023/5/22 17:33 */ class TestFragmentKotlin : BaseFragmentKotlin<FragmentTestKotlinBinding>() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { (view, savedInstanceState) = "Testing Kotlin Fragment" } }
Summarize
This is the end of this article about the simple encapsulation of dataBinding in Android. For more related content about Android dataBinding encapsulation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!