PullTo下拉重新整理上拉載入+多條目檢視專項練習一()
阿新 • • 發佈:2018-12-12
PullTo下拉重新整理上拉載入
private int type = 0; private int page = 0; List<NewsBean.NewslistBean> list; private PullToRefreshListView listView; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void getDataForNet() { //獲取網路工具類 HttpUtils httpUtils = HttpUtils.getHttpUtils(); //獲取網路介面 httpUtils.get(HttpConfig.news_url + "&page=" + page); //監聽 httpUtils.setOnHttpLoadListener(new HttpUtils.HttpLoadListener() { @Override public void loadSuccess(String json) { Gson gson = new Gson(); NewsBean newsBean = gson.fromJson(json, NewsBean.class); switch (type) { case 0: list = newsBean.getNewslist(); adapter = new MyAdapter(MainActivity.this, list); listView.setAdapter(adapter); break; case 1: list = newsBean.getNewslist(); adapter = new MyAdapter(MainActivity.this, list); listView.setAdapter(adapter); listView.onRefreshComplete(); break; case 2: List<NewsBean.NewslistBean> list1 = newsBean.getNewslist(); list.addAll(list1); adapter.notifyDataSetChanged(); listView.onRefreshComplete(); break; } } @Override public void loadError(String error) { } }); } private void initViews() { listView = findViewById(R.id.putolistview); //設定重新整理模式 listView.setMode(PullToRefreshBase.Mode.BOTH); listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { type = 1; page = 1; getDataForNet(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { type = 2; page++; getDataForNet(); } }); }
多條目檢視(無圖單張)
public View getView(int position, View convertView, ViewGroup parent) { //獲取條目型別 int i = getItemViewType(position); switch (i) { case 0: ViewHolder holder= null; if (convertView == null) { convertView = View.inflate(context, R.layout.itemlayout1, null); holder = new ViewHolder(); holder.text = convertView.findViewById(R.id.textview1); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text.setText(list.get(position).getTitle()); break; case 1: ViewHolder1 holder1 = null; if (convertView == null) { convertView = View.inflate(context, R.layout.itemlayout2, null); holder1 = new ViewHolder1(); holder1.text1 = convertView.findViewById(R.id.textview2); holder1.image1 = convertView.findViewById(R.id.imageview2); convertView.setTag(holder1); } else { holder1 = (ViewHolder1) convertView.getTag(); } ImageLoader.getInstance().displayImage(list.get(position).getPicUrl(), holder1.image1, MyApp.getOptions()); holder1.text1.setText(list.get(position).getTitle()); break; } return convertView; } class ViewHolder { public TextView text; } class ViewHolder1 { public TextView text1; public ImageView image1; } @Override public int getItemViewType(int position) { //定義區域性變數 int type = 0; if (position % 2 == 0) { type = 0; } if (position % 2 == 1) type = 1; } return type; }
多條目檢視(多張)
public View getView(int position, View convertView, ViewGroup parent) { //獲取條目型別 int i = getItemViewType(position); switch (i) { case 0: ViewHolder holder; if (convertView == null) { convertView = View.inflate(context, R.layout.item_pic1, null); holder = new ViewHolder(); holder.text1 = convertView.findViewById(R.id.item_title1); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text1.setText(list.get(position).getTitle()); break; case 1: ViewHolder2 holder2 = null; if (convertView == null) { convertView = View.inflate(context, R.layout.item_pic2, null); holder2 = new ViewHolder2(); holder2.text2 = convertView.findViewById(R.id.item_title2); holder2.img_01 = convertView.findViewById(R.id.item_pic21); convertView.setTag(holder2); } else { holder2 = (ViewHolder2) convertView.getTag(); } holder2.text2.setText(list.get(position).getTitle()); Glide.with(context).load(list.get(position).getThumbnail_pic_s()).into(holder2.img_01); break; case 2: ViewHolder3 holder3 = null; if (convertView == null) { convertView = View.inflate(context, R.layout.item_pic3, null); holder3 = new ViewHolder3(); holder3.text3 = convertView.findViewById(R.id.item_title3); holder3.img_01 = convertView.findViewById(R.id.item_pic31); holder3.img_02 = convertView.findViewById(R.id.item_pic32); convertView.setTag(holder3); } else { holder3 = (ViewHolder3) convertView.getTag(); } holder3.text3.setText(list.get(position).getTitle()); Glide.with(context).load(list.get(position).getThumbnail_pic_s()).into(holder3.img_01); Glide.with(context).load(list.get(position).getThumbnail_pic_s()).into(holder3.img_02); break; } return convertView; } class ViewHolder { private TextView text1; } class ViewHolder2 { private TextView text2; private ImageView img_01; } class ViewHolder3 { private TextView text3; private ImageView img_01, img_02; } @Override public int getViewTypeCount() { return 3; } @Override public int getItemViewType(int position) { //獲取條目資料 NewsBean.ResultBean.DataBean dataBean = list.get(position); //資料中取出照片 String thumbnail_pic_s = dataBean.getThumbnail_pic_s(); String thumbnail_pic_s02 = dataBean.getThumbnail_pic_s02(); String thumbnail_pic_s03 = dataBean.getThumbnail_pic_s03(); //定義區域性變數 int type = 0; if (!TextUtils.isEmpty(thumbnail_pic_s)) { type = 0; } if (!TextUtils.isEmpty(thumbnail_pic_s02)) { type = 1; } if (!TextUtils.isEmpty(thumbnail_pic_s03)) { type = 2; } return type; }