listview修改後 調整了一些小bug
阿新 • • 發佈:2018-12-11
public class NewsFragment extends Fragment { private String urls = “https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=”; private String Sp = “&k=&num=20&page=”; private int page = 1; private String id; private XListView mXListView; private MyAdpter myAdpter; private List<XListBean.Results.DataBean> listAll = new ArrayList<>(); private List<XListBean.Results.DataBean> list = new ArrayList<>();
@Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_news_fragment, container, false); mXListView = view.findViewById(R.id.x_list_view); myAdpter = new MyAdpter(list, getActivity()); mXListView.setAdapter(myAdpter); mXListView.setPullLoadEnable(true); mXListView.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { doHttp(page); } @Override public void onLoadMore() { doLoad(); } }); Bundle bundle = getArguments(); id = bundle.getString("id"); /** * 獲取網路請求 */ return view; } private void doLoad() { page++; doHttp(page); } public void doHttp(int page){ new Helper().get(urls+id+Sp+page).result(new Helper.HttpLisenner() { @Override public void success(String data) { Gson gson = new Gson(); XListBean xListBean = gson.fromJson(data, XListBean.class); list = xListBean.getResult().getData(); listAll.addAll(list); myAdpter.setList(list); mXListView.stopRefresh();//停止重新整理 mXListView.stopLoadMore();//停止載入更多 } }); }
} //這邊在介面卡 每次呼叫的時候重新整理介面卡 public void setList(List<XListBean.Results.DataBean> list) { this.list = list; notifyDataSetChanged(); }