新建一个类继承Application 实现里面的onCreate方法
import org.xutils.x; /** * Created by KeLei on 2016/2/2. */ public class LYZApplication extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(true); // 是否输出debug日志 } }在AndroidManiFest中添加这个类
初始化成功
Fragment注解的方式
@ContentView(R.layout.fragment_first)
public class FirstFragment extends Fragment {
private MyAdapter adapter;
private List
private List
private List
@ViewInject(R.id.btn_test)
Button btn_test;
@ViewInject(R.id.listView)
ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return x.view().inject(this, inflater, container);
}
ViewHolder注解的方式
public class MyAdapter extends BaseAdapter {
private Context context;
private List
private LayoutInflater mInflater;
private ImageOptions options;
public ViewHolder holder;
public MyAdapter(Context context, List
this.context = context;
this.list = list;
this.mInflater = LayoutInflater.from(context);
options = new ImageOptions.Builder().setLoadingDrawableId(R.drawable.ic_launcher).setLoadingDrawableId(R.drawable.ic_launcher).setUseMemCache(true).setCircular(true).build();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.itemone, null);
holder = new ViewHolder();
x.view().inject(holder, convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Person bean = list.get(position);
holder.tv_name.setText(bean.getName());
x.image().bind(holder.iv_image, bean.getImgUrl(), options);
return convertView;
}
class ViewHolder {
@ViewInject(R.id.tv_name)
private TextView tv_name;
@ViewInject(R.id.iv_image)
private ImageView iv_image;
}
XUtils的网络请求方法和一些网络请求框架的用法非常类似,我进行了一些简单的封装。
封装一共有3个文件,分别是网络请求工具类XUtil,请求响应数据的解析类,还有一个就是一个请求成功的一个接口回调类。
public class XUtil {
/**
* 发送get请求 * @param
*/
public static
RequestParams params = new RequestParams(url);
if (null != map) {
for (Map.Entry
params.addQueryStringParameter(entry.getKey(), entry.getValue());
}
}
Cancelable cancelable = x.http().get(params, callback);
return cancelable;
}
/**
* 发送post请求 * @param
*/
public static
RequestParams params = new RequestParams(url);
if (null != map) {
for (Map.Entry
params.addParameter(entry.getKey(), entry.getValue());
}
}
Cancelable cancelable = x.http().post(params, callback);
return cancelable;
}
/**
* 上传文件 * @param
*/
public static
RequestParams params = new RequestParams(url);
if (null != map) {
for (Map.Entry
params.addParameter(entry.getKey(), entry.getValue());
}
}
params.setMultipart(true);
Cancelable cancelable = x.http().get(params, callback);
return cancelable;
}
/**
* 下载文件 * @param
*/
public static
RequestParams params = new RequestParams(url);//设置断点续传params.setAutoResume(true);params.setSaveFilePath(filepath);Cancelable cancelable = x.http().get(params, callback);return cancelable; }}
第二个类
public class JsonResponseParser implements ResponseParser {
//检查服务器返回的响应头信息
@Override
public void checkResponse(UriRequest request) throws Throwable { }
/** * 转换result为resultType类型的对象 * * @param resultType 返回值类型(可能带有泛型信息) * @param resultClass 返回值类型 * @param result字符串数据 * @return * @throws Throwable */
@Override
public Object parse(Type resultType, Class> resultClass, String result) throws
Throwable {return new Gson().fromJson(result, resultClass); }}
}
第三个类
public class MyCallBack
@Override
public void onSuccess(ResultType result) {
//可以根据公司的需求进行统一的请求成功的逻辑处理
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
//可以根据公司的需求进行统一的请求网络失败的逻辑处理 }
@Override public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}}
}
}