public class InfoFragment extends BaseFragment {
private PullToRefreshListView ptrListView;
private InfoAdapter adapter;
private int page;
private final int count = 10;
private String bannnerUrl = "http://www.zhaoapi.cn/quarter/getAd";
private String newsUrl = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?";
/*@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}*/
@Override
protected int getLayoutResId() {
return R.layout.fragment_info;
}
@Override
protected void initData() {
requestNews();
requetBanner();
}
@Override
protected void initView(View view) {
page = 1;
ptrListView = view.findViewById(R.id.contents);
adapter = new InfoAdapter(getActivity());
ptrListView.setAdapter(adapter);
ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
ptrListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page = 1;
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
requestNews();
}
});
}
private void requetBanner() {
NetUtil.getInstance().getRequest3(bannnerUrl, BannerResponse.class, new NetUtil.NetCallback<BannerResponse>() {
@Override
public void onSuccess(BannerResponse o) {
adapter.setBannerDatas(o.getData());
}
@Override
public void onFail(String msg) {
Toast.makeText(getActivity(), "请求Banner失败", Toast.LENGTH_SHORT).show();
}
});
}
private void requestNews() {
NetUtil.getInstance().getRequest3(newsUrl + "page=" + page + "&count=" + count, NewsResponse.class, new NetUtil.NetCallback<NewsResponse>() {
@Override
public void onSuccess(NewsResponse o) {
if (page == 1) {
adapter.setNewDatas(o.getResult());
} else {
adapter.addNewDatas(o.getResult());
}
page++;
ptrListView.onRefreshComplete();
if (o.getResult().size() < count) {
ptrListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
} else {
ptrListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
}
}
@Override
public void onFail(String msg) {
ptrListView.onRefreshComplete();
Toast.makeText(getActivity(), "请求新闻失败", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
adapter.stop();
}
}
在这里插入代码片
标题视图
最新推荐文章于 2024-09-08 08:03:51 发布