在我们的开发工作时,findViewById可能是用得最多的函数之一,但它特别讨厌的地方就是我们经常需要对返回的view进行类型转换,输入麻烦、代码丑陋,例如以前我们在Activity中找一些子控件一般是这样 :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 查找子控件
TextView textView = (TextView)findViewById(R.id.my_textview);
ImageView imageView = (ImageView)findViewById(R.id.my_imageview);
ListView listView = (ListView)findViewById(R.id.my_listview);
}
如果页面中的控件比较多,就会有很多的类型转换,这么搞下去还能不能在Android上愉快地开发项目了? 而使用ViewFinder则免去了类型转换,ViewFinder是一个在一个布局中找某个子控件的工具类,用户需要在使用时调用ViewFinder.initContentView函数来初始化ContentView,参数为Context和布局id。然后使用ViewFinder.findViewById来获取需要的view,返回的view则直接是你接收的类型,而不需要进行强制类型转换。
示例如下 :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化
ViewFinder.initContentView(this, R.layout.activity_main) ;
// 查找子控件
TextView textView = ViewFinder.findViewById(R.id.my_textview);
ImageView imageView = ViewFinder.findViewById(R.id.my_imageview);
ListView listView = ViewFinder.findViewById(R.id.my_listview);
}
ViewFinder的实现
/**
* view finder, 方便查找View。用户需要在使用时调用initContentView,
* 将Context和布局id传进来,然后使用findViewById来获取需要的view
* ,findViewById为泛型方法,返回的view则直接是你接收的类型,而不需要进行强制类型转换.比如,
* 以前我们在Activity中找一个TextView一般是这样 :
* TextView textView = (TextView)findViewById(viewId);
* 如果页面中的控件比较多,就会有很多的类型转换,而使用ViewFinder则免去了类型转换,
* 示例如下 :
* TextView textView = ViewFinder.findViewById(viewId);
*
* @author mrsimple
*/
public final class ViewFinder {
/**
* LayoutInflater
*/
static LayoutInflater mInflater;
/**
* 每项的View的sub view Map
*/
private static SparseArray<View> mViewMap = new SparseArray<View>();
/**
* Content View
*/
static View mContentView;
/**
* 初始化ViewFinder, 实际上是获取到该页面的ContentView.
*
* @param context
* @param layoutId
*/
public static void initContentView(Context context, int layoutId) {
mInflater = LayoutInflater.from(context);
mContentView = mInflater.inflate(layoutId, null, false);
if (mInflater == null || mContentView == null) {
throw new RuntimeException(
"ViewFinder init failed, mInflater == null || mContentView == null.");
}
}
/**
* @return
*/
public static View getContentView() {
return mContentView;
}
/**
* @param viewId
* @return
*/
@SuppressWarnings("unchecked")
public static <T extends View> T findViewById(int viewId) {
// 先从view map中查找,如果有的缓存的话直接使用,否则再从mContentView中找
View tagetView = mViewMap.get(viewId);
if (tagetView == null) {
tagetView = mContentView.findViewById(viewId);
mViewMap.put(viewId, tagetView);
}
return tagetView == null ? null : (T) mContentView.findViewById(viewId);
}
}
分享到:
相关推荐
Android注解省去findviewbyid,在Android开发的过程中有很多控件都要findviewbyid的,这样太麻烦了。
一个根据布局文件自动生成findViewById代码的android studio插件
简单实现Android的findviewbyid IOC框架
Android开发中经常使用 findviewbyid来找控件, 这个工具类可以方便的生成代码,减少代码的书写。
支持链式操作 支持自动转换成子类而无需再次强制转换;支持常用属性的快速设置 支持点击事件的快速绑定 是不是很酷啊 "> 支持链式操作 vq id R id TextView1 text "I find hello world" ; vq id R id ...
Android技术知识点:如何使用视图绑定来消除findViewById()
一个简单的注解实现 findViewById 和 setOnClickListener 的例子
Android Studio 插件 传播:heart: 关于 在GitHub上看到Android-Studio-Plugins 项目并有了想法,这里想做一份比较全面的Android Studio开发插件整理,希望可以帮助更多的 Android 开发者来加快开发速度,也希望...
【问题描述】 Android中如下代码: LinearLayout groupPollingAddress = (LinearLayout)findViewById(R.layout....AndroidGUI27中findViewById返回null的快速解决办法 – 玄机逸士的专栏 – 博客频道 – CSDN.NET
AndroidFindView FindViewById无需转换类型进口 将其添加到项目库的末尾的build.gradle中: repositories { // ... maven { url " https://jitpack.io " }} 步骤2.在表单中添加依赖项 dependencies { compile ' ...
简单的注解类,代替android中的findViewById().
主要介绍了Android中findViewById获取控件返回为空问题怎么解决的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
用DOM4J 写的一个小工具类 避免麻烦的FindViewById的书写
这是可以让你告别繁杂的findViewById的一个实例,通过注解的方式来实现。
下面小编就为大家带来一篇android getActivity.findViewById获取ListView 返回NULL的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了AndroidGUI27中findViewById返回null的快速解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
获取xml文件里的视图控件,进而对控件进行相关的属性设置,所以,要想使用自动 化你还得明白其中的基本原理,好了,下面开始正式的介绍自动化方法有哪些: 方法一:借助相关网站资源。 点击有惊喜 使用方法如图: ...
创建全新的视图将满足我们独特的UI需求。 本文介绍在指南针开发中会用到的罗盘的界面UI,通过继承View类实现的自定义视图,以此来深刻了解自定义视图。