当前位置: 首页 > news >正文

如何获取网站根目录网页代码小游戏

如何获取网站根目录,网页代码小游戏,网站备案个人信息,免费的舆情网站app下载众所周知#xff0c;都2225年了#xff0c;如果你还在用Java敲安卓代码#xff0c;findViewById已经是一种非常繁琐的操作#xff0c;如果要去获取的id数量多#xff0c;则对开发更加不友好。如果一个页面id过多#xff0c;经常会有如下场景#xff1a; TextView title…众所周知都2225年了如果你还在用Java敲安卓代码findViewById已经是一种非常繁琐的操作如果要去获取的id数量多则对开发更加不友好。如果一个页面id过多经常会有如下场景 TextView title findViewById(R.id.*tv_title*); TextView title2 findViewById(R.id.tv_title2); TextView title3 findViewById(R.id.tv_title3); TextView title4 findViewById(R.id.tv_title4); TextView title5 findViewById(R.id.tv_title5); TextView title6 findViewById(R.id.tv_title6); TextView title7 findViewById(R.id.tv_title7); TextView title8 findViewById(R.id.tv_title8); TextView title9 findViewById(R.id.tv_title28); TextView title10 findViewById(R.id.tv_title9); TextView title11 findViewById(R.id.tv_title10); TextView title12 findViewById(R.id.tv_title11); TextView title13 findViewById(R.id.tv_title12); TextView title14 findViewById(R.id.tv_title13); TextView title15 findViewById(R.id.tv_title14); TextView title16 findViewById(R.id.tv_title15); TextView title17 findViewById(R.id.tv_title16); TextView title18 findViewById(R.id.tv_title17); TextView title19 findViewById(R.id.tv_title18);...数量一多你会发现这已经极其不友好。其实不光是不友好有问题了解findViewById的原理后你也会发现其内部实现在一定情况下对整体性能有轻微影响。 一 、 findViewById() 的原理 findViewById()的流程原理其实非常简单以activity中的findViewById流程为例activity要么继承自android.app.Activity要么继承自androidx.appcompat.app.AppCompatActivity你要是没适配AndroidX的话那就是support包。这其中 1⃣️、android.app.Activity继承类会通过getWindow得到Window对象来调用findViewById() Nullable public T extends View T findViewById(IdRes int id) {return getWindow().findViewById(id); }2⃣️、androidx.appcompat.app.AppCompatActivity继承类会通过getDelegate()得到AppCompatDelegate委派类的实例对象后调用其findViewByid()这个对象实际是AppCompatDelegateImpl对象创建其时传入了activity.getWindow得到的window对象。 SuppressWarnings(TypeParameterUnusedInFormals) Override public T extends View T findViewById(IdRes int id) {return getDelegate().findViewById(id); }1⃣️和2⃣️最后都会调用WindowgetWindow里的findViewById()。 Nullable public T extends View T findViewById(IdRes int id) {return getDecorView().findViewById(id); }Window类中通过getDecorView()来得到View对象(实际上是一个ViewGroup对象) Nullable public final T extends View T findViewById(IdRes int id) {if (id NO_ID) {return null;}return findViewTraversal(id); }通过调用findViewById来调用ViewGroup中重写的findViewTraversal。下面源码图片是通过在线浏览网站获取到的ViewGroup类中findViewTraversal的相关实现 可以看到这就是个遍历方法如果对应界面内子元素是个View只要id配对上可以直接返回如果是一个ViewGroup则会调用子ViewGroup或子View的这个方法依次遍历直到找到目标id。很明显这是个深度优先搜索时间复杂度为O(n)。 二 、 相关替代方案 1、 ButterKnife 大名鼎鼎的黄油刀使用方式极为简便项目地址 https://github.com/JakeWharton/butterknife在gradle中依赖 implementation com.jakewharton:butterknife:xxx annotationProcessor com.jakewharton:butterknife-compiler:xxx对应在activity中操作为具体一键生成方式这里不表: public class MainActivity extends AppCompatActivity {BindView(R.id.tv_title1)TextView tvTitle1;BindView(R.id.tv_title2)TextView tvTitle1;BindView(R.id.tv_title3)TextView tvTitle3;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//绑定处理ButterKnife.bind(this);} ...... }其实ButterKnife只是通过注解对findViewById的进行的一个取代增加代码的可读性而findViewById的各种缺点依然存在。当然就这个开源框架而言功能绝不仅仅是替代findViewById()。自从kt语言出来后黄油刀的功效捉襟见肘非Java版的老工程不推荐。 2、kotlin-android-extensions 如果你项目可以使用kotlin则可以使用kotlin-android-extensions。 在module的gradle中加入 plugins {id kotlin-android-extensions }可直接在对应类中通过其id的形式控制其控件相当于已经获取了一遍控件id。 import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.test.demo.main.activity_main.*class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)tv_hello.setOnClickListener {}} }这种方法其本质上依旧还是通过findViewById去实现有兴趣的小伙伴可以反编译看看。虽然现在此法已不被官方推荐但其便利性还是首屈一指。 3、ViewBinding 一曲新人笑几度旧人哭。此法一出kotlin-android-extensions已不被官方推荐。 (注意此法只能在AndroidStudio3.6及更高版本上可用 使用方式在build.gradle中依赖: android {...buildFeatures {viewBinding true} }reload后系统会为每个layout目录下 XML 布局文件生成一个绑定类。每个绑定类均包含对根视图以及具有 ID 的所有视图的引用。系统会通过以下方式生成绑定类的名称将 XML 文件的名称转换为驼峰式大小写并在末尾添加“Binding”一词。 例如某个布局命名为activity_login其所生成的绑定类的名称就为LoginActivityBinding这个绑定类就会完成findViewById的工作。 不同布局会生成不同绑定类他们所生成的路径在在build/generated/data_binding_base_class_source_out/debug/out/com/xxx/yyy/databinding/目录下。 当然如果不想xml文件生成 Binding 类可以在 xml 布局文件中根 view 写入此属性 tools:viewBindingIgnoretrue其在代码中使用方式如下ViewBinding在Java和kotlin类中都可使用这里仅是拿kotlin类举例 · Activity Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ActivityMainBinding binding ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());binding.tvTest.setText(This is ViewBinding); }可见setContentView中的参数改为了XXXbing.getroot。调用布局中的某控件只需要XXXbing.viewID驼峰原则可直接拿到实例对象(上述代码中的binding.tvTest控件在xml中的id为tv_test)。例如xml中TextView控件id为tv_demo则在activity中对应实例为XXXbing.tvDemo。 · Fragment中: public class MyFragment extends Fragment {private FragmentMyBinding binding;private Context context;Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {binding FragmentMyBinding.inflate(getLayoutInflater(), container, false);return binding.getRoot();}Overridepublic void onViewCreated(NonNull View view, Nullable Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);binding.tvTitle.setText(Hello ViewBinding);}Overridepublic void onDestroyView() {super.onDestroyView();binding null;} }可以看出跟Activity的引用方式区别不大这里需要稍微注意Fragment 的存在时间比其视图长。在 Fragment对应onDestroyView()时要清除对绑定类实例的所有引用。 ·RecyclerView adapter中 public class MyAdapter extends RecyclerView.AdapterMyAdapter.ViewHolder { 、、、、、、NonNullOverridepublic ViewHolder onCreateViewHolder(NonNull ViewGroup parent, int viewType) {ItemLayoutBinding itemBinding ItemLayoutBinding.inflate(inflater, parent, false);return new ViewHolder(itemBinding);}Overridepublic void onBindViewHolder(NonNull ViewHolder holder, int position) {holder.textView.setText(mData.get(position));}static class ViewHolder extends RecyclerView.ViewHolder {TextView textView;public ViewHolder(NonNull ItemLayoutBinding itemBinding) {super(itemBinding.getRoot());textView itemBinding.textView;}} }可见应用方式无大致区别。 总结 1、findViewById兼容性好适用所有场景且灵活 2、findViewById性能略差底层就是个深度优先搜索且id过多情况下容易造成可读性极差的情况从上述的原理流程中不难看出在Activity中调用findViewById实际上是调用Window中的findViewById但是Fragment中并没有单独的WindowFragment中调用findViewById的效果和Activity中调用的效果一模一样。所以如果一个Activity中有多个FragmentFragment中的控件名称又有重复的直接使用findViewById会爆错。 3、ButterKnife可一键生成方便至极但缺点跟findViewById一样。如果不是老工程此法已不推荐使用。 4、Google官方表示与使用 findViewById 相比ViewBinding具有一些很显著的优点 · 空指针安全由于视图绑定ViewBinding会创建对视图的直接引用因此不存在因视图 ID 无效而引发 Null 指针异常的风险。此外如果视图仅出现在布局的某些配置中则绑定类中包含其引用的字段会使用 Nullable 标记。说白了就是让你丫代码少爆空指针 · 类型安全每个绑定类中的字段均具有与它们在 XML 文件中引用的视图相匹配的类型。这意味着不存在发生类转换异常的风险。 这些差异意味着布局和代码之间的不兼容将会导致构建在编译时而非运行时失败。 下篇预告第四种方式DataBinding。
http://www.pierceye.com/news/226810/

相关文章:

  • 旅游去过的地方可做标识网站百度一下网页入口
  • 做ps找图的网站有哪些法与家国建设征文网站
  • 途途外贸企业网站管理系统aspnet网站模板
  • 网站建设企业网站常用参数
  • 深圳市建设工程质量检测网站网站建设公司 待遇
  • 站长工具大全php做在线直播网站
  • 品牌建设网站公司排名3d模型代做网站
  • 保定网站建设模板联系方式网站设计时图片怎么做
  • 网站策划书内容鄂尔多斯网站制作 建设
  • 广州展厅设计公司排名seo快速排名首页
  • 网站命名方式潍坊市建设工程管理处网站
  • 暴利产品竞价单页网站上海做网站建设的公司排名
  • 坪地网站建设如何做网站前端多少钱
  • 国内用不了的网站展示图片的网站模板
  • 网站优化首页付款网站制作职业
  • 做网站的软件有些什么建设公司门户网站
  • 浙江省长兴县建设局网站自己专业做网站
  • 做网站外包最牛的公司东莞网站制作十年乐云seo
  • 慈溪专业做网站公司网站后台内容更换怎么做
  • wordpress网站搭建教程视频网站优化前景
  • 门户网站的优点seo月薪
  • 怎样做网站静态什么网站做二维码比较好
  • 共享虚拟主机做网站够用么抖音短剧推广怎么做
  • 个人网站备案内容写什么西部数码网站管理助手v3.1
  • 搜索引擎 网站模板wordpress 图片走cdn
  • 常见cms网站源码下载重庆微信网站开发公司
  • 网站开发用什么电脑天津室内设计公司排名
  • 云南网站建设招商建设公司网站计入哪个科目
  • 网站备案在哪里查询海外市场推广方案
  • 中诺建设集团有限公司网站微信商家小程序收费吗