findviewbyid简化
This commit is contained in:
@ -8,11 +8,8 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.gh.common.util.Utils;
|
||||
import com.gh.gamecenter.listener.OnCallBackListener;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
@ -22,46 +19,16 @@ import de.greenrobot.event.EventBus;
|
||||
*/
|
||||
public class BaseFragment extends Fragment implements OnCallBackListener {
|
||||
|
||||
// private Unbinder unbinder;
|
||||
|
||||
protected View view;
|
||||
|
||||
protected Handler handler = new Handler();
|
||||
|
||||
protected boolean isEverpause;
|
||||
|
||||
protected void init(int layout, boolean flag) {
|
||||
protected void init(int layout) {
|
||||
view = View.inflate(getActivity(), layout, null);
|
||||
|
||||
// unbinder = ButterKnife.bind(this, view);
|
||||
|
||||
if (flag) {
|
||||
//简化findViewById
|
||||
try {
|
||||
Class<?> clazz = this.getClass();
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
int id = Utils.getId(field.getName());
|
||||
if (id != -1) {
|
||||
Utils.log("reflect name = " + field.getName());
|
||||
field.setAccessible(true);
|
||||
Class<?> fieldType = field.getType();
|
||||
Object injectedValue = fieldType.cast(view.findViewById(id));
|
||||
field.set(this, injectedValue);
|
||||
field.setAccessible(false);
|
||||
}
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
ButterKnife.bind(this, view);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void init(int layout) {
|
||||
init(layout, true);
|
||||
ButterKnife.bind(this, view);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -101,7 +68,6 @@ public class BaseFragment extends Fragment implements OnCallBackListener {
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
EventBus.getDefault().unregister(this);
|
||||
// unbinder.unbind();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user