[android]代码库
//初始化Xutils
x.Ext.init(this);
public interface DataInterface<T> {
void setdata(List<T> t);
}
public class HttpXutils {
public static <T>void utils(String url,final Class<T> clazz,final DataInterface dataInterface) {
x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Gson gson = new Gson();
List<T> beanList = new ArrayList<T>();
try {
JSONObject resultObject = new JSONObject(result);
Iterator<String> keys = resultObject.keys();
while (keys.hasNext()) {
String next = keys.next();
JSONArray nextArray = resultObject.optJSONArray(next);
for (int i = 0; i < nextArray.length(); i++) {
JSONObject object = nextArray.optJSONObject(i);
T tBean = gson.fromJson(object.toString(), clazz);
beanList.add(tBean);
}
}
} catch (Exception e) {
e.printStackTrace();
}
dataInterface.setdata(beanList);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.d("zzz", "onError " + ex.toString());
}
@Override
public void onCancelled(CancelledException cex) {
Log.d("zzz", "onCancelled");
}
@Override
public void onFinished() {
Log.d("zzz", "onFinished");
}
});
}
}
public class TitleFragment extends Fragment implements DataInterface<JsonBean>,PullToRefreshListView.OnRefreshListener2{
private int homePage=0;
private int endPage=20;
private String titleType;
private PullToRefreshListView pullto;
private boolean flag=false;
private String path;
private NewsAdapter newsAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view1 = View.inflate(getActivity(), R.layout.fragment_title, null);
//接收值
titleType = getArguments().getString("type");
initView(view1);
return view1;
}
//视图加载完成
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
path = "http://c.m.163.com/nc/article/headline/"+titleType+"/"+homePage+"-"+endPage+".html";
HttpXutils.utils(path,JsonBean.class,this);
}
public void initView(View view){
pullto = (PullToRefreshListView) view.findViewById(R.id.pullto);
newsAdapter = new NewsAdapter(getActivity());
pullto.setAdapter(newsAdapter);
pullto.setMode(PullToRefreshBase.Mode.BOTH);
pullto.setOnRefreshListener(this);
}
@Override
public void setdata(final List<JsonBean> t) {
newsAdapter.addData(t,flag);
newsAdapter.notifyDataSetChanged();
pullto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent=new Intent(getActivity(), WebViewActivity.class);
intent.putExtra("url",t.get(i-1).getUrl_3w());
startActivity(intent);
}
});
pullto.onRefreshComplete();
}
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
homePage=0;
endPage=20;
flag=true;
HttpXutils.utils(path,JsonBean.class,this);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
homePage+=20;
endPage+=20;
flag=false;
HttpXutils.utils(path,JsonBean.class,this);
}
}
by: 发表于:2017-09-27 09:59:54 顶(0) | 踩(0) 回复
??
回复评论