SoFunction
Updated on 2025-04-09

XListView implements network loading pictures and pull-down refresh

The examples in this article share with you the function of XListView to realize network loading pictures and pull-down refreshing for your reference. The specific content is as follows

public class MainActivity extends AppCompatActivity {

 private XListView contents;

 private int page = 0;
 private MyBaseAdapter adapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);

  contents = findViewById();
  adapter = new MyBaseAdapter(getLayoutInflater());
  (adapter);

  //Whether to turn on the pull-down refresh? Pull-up load  //(false);
  (true);

  (new () {
   @Override public void onRefresh() {
    page = 0;
    loadData(page);
    //Load refresh data   }

   @Override public void onLoadMore() {
    loadData(page);
   }
  });

  //As soon as you come in, you will load the first page of data  loadData(page);
 }

 private String url =
   "/cook/query?key=3ec004200a6a2f4cf4774e480c006375&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn=";

 private void loadData(int page) {
  String requestUrlWithPageNum = url + page;

  //Load network data  new AsyncTask<String, Void, List<DataItem>>() {

   @Override protected List<DataItem> doInBackground(String... strings) {
    ResponseBean responseBean = null;
    try {
     URL url = new URL(strings[0]);
     HttpURLConnection urlConnection = (HttpURLConnection) ();
     ("GET");
     (5000);
     (5000);

     int responseCode = ();
     if (responseCode == 200) {
      String str = stream2String(());
      responseBean = new Gson().fromJson(str, );
     } else {
      //
     }
     return responseBean == null ? null : ().getData();
    } catch (MalformedURLException e) {
     ();
    } catch (IOException e) {
     ();
    }

    return null;
   }

   @Override protected void onPostExecute(List<DataItem> dataItems) {
    if (dataItems == null) {
     (, "Request data error", Toast.LENGTH_LONG).show();
     return;
    }

    //Update data    updateData(dataItems);
    loadCompleted();
   }
  }.execute(url);
 }

 private String stream2String(InputStream is) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(is));
  StringBuilder sb = new StringBuilder();
  for (String tmp = (); tmp != null; tmp = ()) {
   (tmp);
  }

  return ();
 }

 private void updateData(List<DataItem> datas) {
  if (page == 0) {
   (datas);
  } else {
   (datas);
  }
 }

 // Completed by Loading/Refreshing private void loadCompleted() {
  //By ListView: Refresh, loading is completed  page++;
  ();
  ();
 }
}

public class MyBaseAdapter extends BaseAdapter {

 private List<DataItem> mDatas;
 protected LayoutInflater mInflater;

 /**
   * Update data
   */
 public void setDatas(List<DataItem> datas) {
  ();
  if (datas != null) {
   (datas);
  }
  notifyDataSetChanged();
 }

 /**
   * Append data
   */
 public void addDatas(List<DataItem> datas) {
  if (datas != null) {
   (datas);
   notifyDataSetChanged();
  }
 }

 public MyBaseAdapter(LayoutInflater mInflater) {
   = mInflater;
  mDatas = new ArrayList<>();
 }

 @Override public int getCount() {
  return ();
 }

 @Override public DataItem getItem(int position) {
  return (position);
 }

 @Override public long getItemId(int position) {
  return position;
 }

 @Override public View getView(int position, View convertView, ViewGroup parent) {
  BaseViewHolder viewHolder = null;

  if (convertView == null) {
   convertView = (, parent, false);
   viewHolder = new BaseViewHolder(convertView);
  } else {
   viewHolder = (BaseViewHolder) ();
  }

  (getItem(position));

  return convertView;
 }

 public class BaseViewHolder {
  private View itemView;

  private ImageView icon;
  private TextView title;
  private TextView date;

  public BaseViewHolder(View itemView) {
    = itemView;
   title = ();
   date = ();
   icon = ();

   (this);
  }

  public void bindData(DataItem dataItem) {
   (());
   (());
   ().displayImage((), icon,
     (()));
  }
 }
}

  public class ImageLoaderConfigs {
 public static ImageLoaderConfiguration getImageLoaderConfiguration(Context context) {
  ImageLoaderConfiguration configuration = new (context)
    //Intrinsic cache extra options, maximum width, height    //.memoryCacheExtraOptions(480, 800) // default = device screen dimensions Maximum length and width of memory cache file    //.diskCacheExtraOptions(480, 800, null) // Details of local cache (maximum length and width of cache), it is best not to set this    //Thread pool configuration    //.taskExecutor()
    //.taskExecutorForCachedImages()
    //.threadPoolSize(3) // default Number of loads in the thread pool    //.threadPriority(Thread.NORM_PRIORITY - 2) // default Set the priority of the current thread    //Task processing priority Fist In Fist Out    //.tasksProcessingOrder() // default
    //Do not cache multiple sizes of an image in memory    //.denyCacheImageMultipleSizesInMemory()
    //Intrinsic cache policy    //.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) // Can be implemented through your own memory cache    //Memory cache size    //.memoryCacheSize(2 * 1024 * 1024) // Maximum value of memory cache    //Intrinsic cache size: occupancy percentage    .memoryCacheSizePercentage(13) // default
    //Disk Cache Policy    //.diskCache(new LruDiskCache()) // default can customize the cache path    //Disk cache size    .diskCacheSize(50 * 1024 * 1024) // Maximum value of 50 Mb sd card (local) cache    //.diskCacheFileCount(100) // Number of files that can be cached    // default is to use HASHCODE to encrypt and name the UIL, and can also be encrypted with MD5 (new Md5FileNameGenerator()).    //.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
    //.imageDownloader(new BaseImageDownloader(context)) // default
    //(new BaseImageDecoder(false)) // default
    //Some configurations when loading specific pictures    .defaultDisplayImageOptions(()) // default
    .writeDebugLogs() // Print debug log    .build();

  return configuration;
 }

 public static DisplayImageOptions getDefaultDisplayImageOptions(Context context) {
  DisplayImageOptions displayImageOptions = new ()
    //Whether to cache    .cacheInMemory(true)
    .cacheOnDisk(true)
    //RGB 565 r red accounts for 5 g green accounts for 6 b blue accounts for 5 -> 2 bytes    //alpha
    //ARGB 4444 4 4 4 4 -> 2 bytes    //ARGB 8888 -> 4 bytes
    //10 * 10 Use rgb565 -> 10*10*2
    .bitmapConfig(.RGB_565)
    //What content is displayed when loading or loading error    .showImageOnLoading(.ic_launcher)
    .showImageOnFail(.ic_launcher)
    //
    .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)

    //Loading effect    //ctrl + p
    .displayer(new CircleBitmapDisplayer())
    .build();

  //ctrl + h
  //BitmapDisplayer;
  return displayImageOptions;
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.