在我们的开发工作时,findViewById可能是用得最多的函数之一,但它特别讨厌的地方就是我们经常需要对返回的view进行类型转换,输入麻烦、代码丑陋,例如以前我们在Activity中找一些子控件一般是这样 :

@Overrideprotected 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则直接是你接收的类型,而不需要进行强制类型转换。

示例如下 :

@Overrideprotected 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);    }}

更多相关文章

  1. .net基础初学Android
  2. Android自定义View-圆形图片控件
  3. Android控件之文本控件---TextView 两种效果+SpannableString
  4. Android(安卓)布局单位转换
  5. ScrollView内部嵌套Recyclerview,防止recyclerview自动获得焦点
  6. Android之控件保持在软键盘上面
  7. PX(像素)转换工具类
  8. 教你如何去掉滑动控件的阴影
  9. android 绕过R文件,通过代码获取styleable的属性

随机推荐

  1. 【Android】EditText实现搜索功能,把键盘
  2. 离线下载安装android sdk
  3. Android 双屏异显
  4. Android工具之Hierarchy Viewer--分析应
  5. android定时器
  6. APP开发实战85-帧动画
  7. Android 导入 aar包引起的Error:Failed t
  8. 在代码中获取Android(安卓)theme中的attr
  9. Android的CTS测试
  10. 探索 Android(安卓)Q:位置权限