当前位置: 首页 > 工具软件 > Viewfinder > 使用案例 >

XUtil学习之ViewFinder(三)

郏瀚
2023-12-01

先贴代码
其实ViewFinder是一个封装了View ,Activity ,PreferenceGroup ,PreferenceActivity 四个属性的类,分别用构造方法传入相应的值。提供的方法有findViewById(提供2种重载方法,id和pid),findViewByInfo,
findPreference,getContext方法。

public class ViewFinder {

    private View view;
    private Activity activity;
    private PreferenceGroup preferenceGroup;
    private PreferenceActivity preferenceActivity;

    public ViewFinder(View view) {
        this.view = view;
    }

    public ViewFinder(Activity activity) {
        this.activity = activity;
    }

    public ViewFinder(PreferenceGroup preferenceGroup) {
        this.preferenceGroup = preferenceGroup;
    }

    public ViewFinder(PreferenceActivity preferenceActivity) {
        this.preferenceActivity = preferenceActivity;
        this.activity = preferenceActivity;
    }

    public View findViewById(int id) {
        return activity == null ? view.findViewById(id) : activity.findViewById(id);
    }

    public View findViewByInfo(ViewInjectInfo info) {
        return findViewById((Integer) info.value, info.parentId);
    }

    public View findViewById(int id, int pid) {
        View pView = null;
        if (pid > 0) {
            pView = this.findViewById(pid);
        }

        View view = null;
        if (pView != null) {
            view = pView.findViewById(id);
        } else {
            view = this.findViewById(id);
        }
        return view;
    }

    @SuppressWarnings("deprecation")
    public Preference findPreference(CharSequence key) {
        return preferenceGroup == null ? preferenceActivity.findPreference(key) : preferenceGroup.findPreference(key);
    }

    public Context getContext() {
        if (view != null) return view.getContext();
        if (activity != null) return activity;
        if (preferenceActivity != null) return preferenceActivity;
        return null;
    }
}
 类似资料: