修改xutils框架以支持fragment

好像xutils框架目前还不支持fragment的反射,看了下,比较好改,主要修改ViewFinder。

修改一:

ViewUtils.java

public static void inject(Fragment fragment) {
    injectObject(fragment, new ViewFinder(fragment));
}

修改二:

ViewFinder.java



import android.app.Activity;
import android.content.Context;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.support.v4.app.Fragment;
import android.view.View;

public class ViewFinder {

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

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

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

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

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

    public View findViewById(int id) { // 修改这里
//        View v = activity == null ? view.findViewById(id) : activity.findViewById(id);
    	View v = null;
        if(activity != null) {
        	v = activity.findViewById(id);
        } else if(this.fragment != null) {
        	v = this.fragment.getView().findViewById(id);
        } else {
        	v = view.findViewById(id);
        }
        return v;
    }

    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;
        if (fragment != null) return fragment.getActivity();
        return null;
    }
}

如何使用:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, null);
        return v;
}

public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewUtils.inject(this);
        AppLog.log("homefragment.onCreateView" + this.title_icon);
        initView();
}


你可能感兴趣的:(android,android,框架)