重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本文实例为大家分享了XListView实现刷新加载的具体代码,供大家参考,具体内容如下
成都创新互联服务项目包括疏附网站建设、疏附网站制作、疏附网页制作以及疏附网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,疏附网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到疏附省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
MainActivity.java
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager = findViewById(R.id.viewpager); group = findViewById(R.id.group); li = new ArrayList<>(); li.add(new Afragment()); li.add(new Bfragment()); li.add(new Cfragment()); li.add(new Dfragment()); pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return li.get(position); } @Override public int getCount() { return li.size(); } }); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i){ case R.id.r1: pager.setCurrentItem(0); break; case R.id.r2: pager.setCurrentItem(1); break; case R.id.r3: pager.setCurrentItem(2); break; case R.id.r4: pager.setCurrentItem(3); break; } } }); }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
Afragment.java
public class Afragment extends Fragment{ private int page=1; private XListView listView; private MyBaseAdapter adapter; ResponseBean responseBean; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(getActivity(), R.layout.afrag, null); listView = view.findViewById(R.id.xlv); adapter = new MyBaseAdapter(getLayoutInflater()); listView.setAdapter(adapter); listView.setPullLoadEnable(true); listView.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { page=0; loadData(page); } @Override public void onLoadMore() { loadData(page); } }); loadData(page); return view; } private String url = "http://apis.juhe.cn/cook/query?key=3ec004200a6a2f4cf4774e480c006375&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn="; private void loadData(int page){ String urlString=url+page; new AsyncTask>(){ @Override protected List doInBackground(String... strings) { try { URL url = new URL(strings[0]); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setReadTimeout(5000); urlConnection.setConnectTimeout(5000); int responseCode = urlConnection.getResponseCode(); if (responseCode==200){ String result=String2(urlConnection.getInputStream()); responseBean = new Gson().fromJson(result, ResponseBean.class); }else { } return responseBean == null ?null:responseBean.getResult().getData(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(List dataItems) { super.onPostExecute(dataItems); if (dataItems == null) { Toast.makeText(getActivity(), "请求数据错误", Toast.LENGTH_LONG).show(); return; } updateData(dataItems); loadCompleted(); } }.execute(urlString); } private void updateData(List dataItems){ if (page==0){ adapter.setDatas(dataItems); }else { adapter.addDatas(dataItems); } } private void loadCompleted(){ page++; listView.stopRefresh(); listView.stopLoadMore(); } private String String2(InputStream stream){ StringBuilder sb = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); try { for (String tmp=reader.readLine();tmp!=null;tmp=reader.readLine()){ sb.append(tmp); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } }
MyBaseAdapter.java
public class MyBaseAdapter extends BaseAdapter{ private Listdatas; private LayoutInflater layoutInflater; public MyBaseAdapter(LayoutInflater layoutInflater) { this.layoutInflater = layoutInflater; datas=new ArrayList<>(); } public void setDatas(List datas) { datas.clear(); if (datas!=null){ datas.addAll(datas); } notifyDataSetChanged(); } public void addDatas(List dataItems) { if (dataItems!=null){ datas.addAll(dataItems); notifyDataSetChanged(); } } public final int VIEW_TYPE_COUNT=2; public final int IMAGE_TEXT_TYPE=0; public final int TEXT_TYPE=1; @Override public int getItemViewType(int position) { if (position % 2==0){ return IMAGE_TEXT_TYPE; }else { return TEXT_TYPE; } } @Override public int getViewTypeCount() { return VIEW_TYPE_COUNT; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int i) { return datas.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if (getItemViewType(i)==IMAGE_TEXT_TYPE){ ViewHolder viewHolder=null; if (view==null){ view = layoutInflater.inflate(R.layout.item, viewGroup,false); viewHolder = new ViewHolder(view); }else { viewHolder = (ViewHolder) view.getTag(); } viewHolder.bindData((DataItem) getItem(i)); }else { ViewHolder2 viewHolder2; if (view==null){ view = layoutInflater.inflate(R.layout.item_text, viewGroup, false); viewHolder2 = new ViewHolder2(); viewHolder2.textView = view.findViewById(R.id.text); view.setTag(viewHolder2); }else { viewHolder2 = (ViewHolder2) view.getTag(); } viewHolder2.textView.setText(datas.get(i).getTitle()); } return view; } public class ViewHolder{ private ImageView icon; private TextView title; private TextView date; public ViewHolder(View view) { icon = view.findViewById(R.id.icon); title = view.findViewById(R.id.title); date = view.findViewById(R.id.date); view.setTag(this); } public void bindData(DataItem item) { title.setText(item.getTitle()); date.setText(item.getId()); ImageLoader.getInstance().displayImage(item.getFirst(),icon , ImageloaderConfigs.getDefult(icon.getContext())); } } public class ViewHolder2{ TextView textView; } }
ImageLoaderConfigs.java
public class ImageloaderConfigs { public static ImageLoaderConfiguration getImageLoader(Context context){ ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context) .memoryCacheSizePercentage(13) .diskCacheSize(50*1024*1024) .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) .writeDebugLogs() .build(); return configuration; } public static DisplayImageOptions getDefult(Context context){ DisplayImageOptions imageOptions = new DisplayImageOptions.Builder() .cacheInMemory(true) .cacheOnDisk(true) .bitmapConfig(Bitmap.Config.RGB_565) .imageScaleType(ImageScaleType.EXACTLY_STRETCHED) .displayer(new CircleBitmapDisplayer()) .build(); return imageOptions; } }
App.java
public class App extends Application{ @Override public void onCreate() { super.onCreate(); ImageLoader.getInstance().init(ImageloaderConfigs.getImageLoader(this)); } }
ResponsBean.java
public class ResponseBean { private int resultcode; private String reason; private Result result; public int getResultcode() { return resultcode; } public String getReason() { return reason; } public Result getResult() { return result; } public class Result{ private Listdata; public List getData() { return data; } } }
DataItem.java
public class DataItem { private String id; private String title; private Listalbums; public String getId() { return id; } public String getTitle() { return title; } public List getAlbums() { return albums; } public String getFirst(){ return albums==null || albums.size()==0? "" : albums.get(0); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。