温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

XListView实现多条目网络数据刷新加载 网络加载图片

发布时间:2020-10-17 12:14:31 来源:脚本之家 阅读:150 作者:FanRQ_ 栏目:移动开发

本文实例为大家分享了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"?>
 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".ui.MainActivity">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 >
 <android.support.v4.view.ViewPager
  android:id="@+id/viewpager"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="9"
  >
 
 </android.support.v4.view.ViewPager>
 
 <RadioGroup
  android:id="@+id/group"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:orientation="horizontal"
  >
  <RadioButton
  android:id="@+id/r1"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:button="@null"
  android:gravity="center"
  android:layout_weight="1"
  android:text="Afrag"/>
  <RadioButton
  android:id="@+id/r2"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:button="@null"
  android:gravity="center"
  android:layout_weight="1"
  android:text="Bfrag"/>
  <RadioButton
  android:id="@+id/r3"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:button="@null"
  android:gravity="center"
  android:layout_weight="1"
  android:text="Cfrag"/>
  <RadioButton
  android:id="@+id/r4"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:button="@null"
  android:gravity="center"
  android:layout_weight="1"
  android:text="Dfrag"/>
 
 </RadioGroup>
 
 </LinearLayout>
 
</android.support.constraint.ConstraintLayout>

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<String,Void,List<DataItem>>(){
 
  @Override
  protected List<DataItem> 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<DataItem> dataItems) {
   super.onPostExecute(dataItems);
   if (dataItems == null) {
   Toast.makeText(getActivity(), "请求数据错误", Toast.LENGTH_LONG).show();
   return;
   }
   updateData(dataItems);
   loadCompleted();
 
  }
  }.execute(urlString);
 }
 
 private void updateData(List<DataItem> 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 List<DataItem> datas;
 private LayoutInflater layoutInflater;

 public MyBaseAdapter(LayoutInflater layoutInflater) {
 this.layoutInflater = layoutInflater;
 datas=new ArrayList<>();
 }

 public void setDatas(List<DataItem> datas) {
 datas.clear();
 if (datas!=null){
  datas.addAll(datas);
 }
 notifyDataSetChanged();
 }

 public void addDatas(List<DataItem> 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 List<DataItem> data;

 public List<DataItem> getData() {
  return data;
 }
 }
}

DataItem.java

public class DataItem {
 private String id;
 private String title;
 private List<String> albums;

 public String getId() {
 return id;
 }

 public String getTitle() {
 return title;
 }

 public List<String> getAlbums() {
 return albums;
 }
 public String getFirst(){
 return albums==null || albums.size()==0? "" : albums.get(0);
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI