如何获取网站根目录,网页代码小游戏,网站备案个人信息,免费的舆情网站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。