You do not need ViewInject to simplify your findViewById and viewinject.

Source: Internet
Author: User

You do not need ViewInject to simplify your findViewById and viewinject.

This blog will be very short. You may be bored with writing code.findViewById, Me too. At this time, you may choose to write with annotations.ViewInjectTo simplify this operation,... I really don't like itViewInject.

Now, I want to simplify it myself.findViewById, How to use it after it is simplified? See the following code,

TextView textView = V.f(this, R.id.textView);ImageView imageView = V.f(convertView, R.id.image);

This solution solves two problems,

1. simplified the 'findviewbyid' method. 2. If no code is available, the code length type conversion will be increased.

Then how should we implement these twoV.fHow? It is actually very simple.

/** * view utils * @author loader * */public class V {    /**     * activity.findViewById()     * @param context     * @param id     * @return     */    public static <T extends View> T f(Activity context, int id) {        return (T) context.findViewById(id);    }    /**     * rootView.findViewById()     * @param rootView     * @param id     * @return     */    public static <T extends View> T f(View rootView, int id) {        return (T) rootView.findViewById(id);    }}

This is done with just one line of code. Here we use the type derivation feature of the java generic method.

OK. This blog is so simple. Everyone can copy the code.

Copyright Disclaimer: This article is from the Loader's Blog and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.