ViewBinding封装基类(BaseActivity,BaseFragment)

//混淆规则

-keep class  包名.databinding.* {*;}

使用反射

BaseActivity:

public class BaseActivity<T extends ViewBinding> extends AppCompatActivity {
    protected T binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Type superclass = getClass().getGenericSuperclass();
        Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
        try {
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class);
            binding = (T) method.invoke(null, getLayoutInflater());
            setContentView(binding.getRoot());
        } catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

使用 泛型传入ViewBinding即可:

public class MainActivity extends BaseActivity<ActivityMainBinding> {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // binding.button1 ……
      
    }

}

BaseFragment

public class BaseFragment<T extends ViewBinding> extends Fragment {
    protected T binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Type superclass = getClass().getGenericSuperclass();
        Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
        try {
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class,ViewGroup.class,boolean.class);
            binding = (T) method.invoke(null, getLayoutInflater(),container,false);
        } catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
            e.printStackTrace();
        }
        return binding.getRoot();
    }
}

使用:

public class BlankFragment extends BaseFragment<FragmentBlankBinding> {
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //binding.textView ……
    }
}

kotlin的写法:

open class BaseActivity<VB:ViewBinding> :AppCompatActivity() {
    protected val binding: VB by lazy {
        //使用反射得到viewbinding的class
        val type = javaClass.genericSuperclass as ParameterizedType
        val aClass = type.actualTypeArguments[0] as Class<*>
        val method = aClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        method.invoke(null, layoutInflater) as VB
    }

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

open class BaseFragment<VB:ViewBinding>:Fragment(){

    lateinit var binding: VB

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val type = javaClass.genericSuperclass as ParameterizedType
        val aClass = type.actualTypeArguments[0] as Class<*>
        val method = aClass.getDeclaredMethod("inflate", LayoutInflater::class.java,ViewGroup::class.java,Boolean::class.java)
        binding = method.invoke(null,layoutInflater,container,false) as VB
        return binding.root
    }
}

不使用反射

关于要不要用反射,以及反射的性能问题,我测试了几次,反射的耗时为20-350毫秒,不使用反射的话,基本在10毫秒以内,所以用不用看个人吧

如果不想使用反射,binding就需要自己实例化,其实写起来也简单:

open class ViewActivity<VB : ViewBinding>(open val block:(LayoutInflater)->VB) : BaseActivity(){
    protected val binding by lazy { block(layoutInflater) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
    }
}

abstract class ViewFragment2<VB : ViewBinding>(val bindingBlock:(LayoutInflater, ViewGroup?,Boolean)->VB) : BaseFragment(){
    private lateinit var _binding: VB
    protected val binding get() = _binding
    override fun getLayoutId() = 0
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        isLoaded = false
        _binding = bindingBlock(inflater,container,false)
        return _binding.root
    }
}

使用:

class MainActivity2 : ViewActivity<ActivityMain2Binding>(ActivityMain2Binding::inflate){
}

class MyFragment:ViewFragment2<FragmentMyBinding>(FragmentMyBinding::inflate){
    
}

  • 19
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
封装Selenium基类是一种常见的测试框架设计模式,它可以提供一些通用的方法和属性,以便在测试过程中更方便地使用Selenium库。下面是一个简单的封装Selenium基类的示例: ```python from selenium import webdriver class BasePage: def __init__(self, driver): self.driver = driver def open_url(self, url): self.driver.get(url) def find_element(self, locator): return self.driver.find_element(*locator) def click(self, locator): element = self.find_element(locator) element.click() def input_text(self, locator, text): element = self.find_element(locator) element.clear() element.send_keys(text) # 其他通用方法... ``` 在这个示例中,`BasePage`类接受一个`driver`参数,该参数是一个已经初始化好的Selenium WebDriver对象。`BasePage`类提供了一些常用的方法,如`open_url`用于打开指定的URL,`find_element`用于查找页面元素,`click`用于点击元素,`input_text`用于输入文本等。 通过封装Selenium基类,你可以在具体的测试页面类中继承`BasePage`类,并直接使用其中定义的方法,从而简化测试代码的编写。例如: ```python class LoginPage(BasePage): def __init__(self, driver): super().__init__(driver) self.username_locator = (By.ID, 'username') self.password_locator = (By.ID, 'password') self.login_button_locator = (By.ID, 'login-button') def login(self, username, password): self.input_text(self.username_locator, username) self.input_text(self.password_locator, password) self.click(self.login_button_locator) # 其他页面特定方法... ``` 在`LoginPage`类中,我们继承了`BasePage`类,并定义了一些页面特定的元素定位器和方法,同时可以直接使用`BasePage`类中定义的通用方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值