XListView上拉載入,下拉重新整理
阿新 • • 發佈:2018-11-30
1.MainActivity頁面
public class MainActivity extends AppCompatActivity { private XListView xListView; private int page; private NewsAdapter adapter; private String path = "http://www.xieast.com/api/news/news.php?page=%d"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); page = 1; //獲取資源ID xListView = findViewById(R.id.listview); //例項化介面卡 adapter = new NewsAdapter(this); xListView.setAdapter(adapter); //設定可以重新整理載入 xListView.setPullLoadEnable(true); xListView.setPullRefreshEnable(true); //設定監聽 xListView.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { page = 1; initData(); } @Override public void onLoadMore() { initData(); } }); initData(); } //得到資料並新增介面卡 public void initData(){ NetUtil.getJson(String.format(path, page), NewsBean.class, new NetUtil.CallBack<NewsBean>() { @Override public void sucess(NewsBean o) { if(page == 1){ adapter.setdata(o.getData()); }else{ adapter.adddata(o.getData()); } page++; //停止重新整理載入 xListView.stopRefresh(); xListView.stopLoadMore(); } }); } }
2.多條目介面卡
public class NewsAdapter extends BaseAdapter { private Context context; private List<NewsBean.DataBean> list; public NewsAdapter(Context context) { this.context = context; list = new ArrayList<>(); } public void setdata(List<NewsBean.DataBean> lists){ list.clear(); list.addAll(lists); notifyDataSetChanged(); } public void adddata(List<NewsBean.DataBean> lists){ list.addAll(lists); notifyDataSetChanged(); } @Override public int getCount() { return list.size(); } @Override public NewsBean.DataBean getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } private final int TYPE_COUNT = 2; private final int TYPE_ONE = 0; private final int TYPE_TWO =1; @Override public int getViewTypeCount() { return TYPE_COUNT; } @Override public int getItemViewType(int position) { if(list.get(position).getThumbnail_pic_s02() == null){ return TYPE_ONE; }else{ return TYPE_TWO; } } @Override public View getView(int position, View convertView, ViewGroup parent) { if(getItemViewType(position) == TYPE_ONE){ ViewHolder holder; if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.item,parent,false); holder = new ViewHolder(); holder.textView = convertView.findViewById(R.id.textView); holder.imageView = convertView.findViewById(R.id.imageView); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(getItem(position).getTitle()); ImageLoader.getInstance().displayImage(getItem(position).getThumbnail_pic_s(),holder.imageView); }else{ NewsHolder holder; if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.items,parent,false); holder = new NewsHolder(); holder.textView = convertView.findViewById(R.id.textView2); holder.imageView = convertView.findViewById(R.id.imageView2); holder.imageView2 = convertView.findViewById(R.id.imageView3); holder.imageView3 = convertView.findViewById(R.id.imageView4); convertView.setTag(holder); }else{ holder = (NewsHolder) convertView.getTag(); } holder.textView.setText(getItem(position).getTitle()); ImageLoader.getInstance().displayImage(getItem(position).getThumbnail_pic_s(),holder.imageView); ImageLoader.getInstance().displayImage(getItem(position).getThumbnail_pic_s02(),holder.imageView2); ImageLoader.getInstance().displayImage(getItem(position).getThumbnail_pic_s03(),holder.imageView3); } return convertView; } class ViewHolder{ TextView textView; ImageView imageView; } class NewsHolder{ TextView textView; ImageView imageView,imageView2,imageView3; } }
使用XListview記得匯入依賴!