public class OneFragment extends Fragment implements XView,XListView.IXListViewListener {
private String mUrl = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private XListView xListView;
private int index = 1;
private List<Data.PostsBean> lists = new ArrayList<>();
private XPresenterImpl presenter;
private MyAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
initView(view);
//实例化
presenter=new XPresenterImpl(this);
xListView.setXListViewListener(this);
xListView.setPullLoadEnable(true);
adapter = new MyAdapter(lists,getActivity());
xListView.setAdapter(adapter);
presenter.startRequest(mUrl,index);
return view;
}
private void initView(View view) {
xListView = view.findViewById(R.id.xlist2);
}
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
lists.clear();
index=1;
presenter.startRequest(mUrl,index);
}
},1000);
}
@Override
public void onLoadMore() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
index++;
presenter.startRequest(mUrl,index);
}
},1000);
}
@Override
public void success(List<Data.PostsBean> data) {
lists.addAll(data);
adapter.notifyDataSetChanged();
stop();
}
private void stop() {
xListView.stopLoadMore();
xListView.stopRefresh();
xListView.setRefreshTime("刚刚");
}
@Override
public void error(String error) {
Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
}
}
布局
<com.example.daylibrary.maxwin.maxwin.maxwin.view.XListView
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id="@+id/xlist2"/>