Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类

               

    在我们的开发工作时,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);    }}

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值