中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

宁晋县| 陆川县| 万荣县| 蕲春县| 南陵县| 余姚市| 万全县| 巴楚县| 全椒县| 平安县| 潍坊市| 皮山县| 内黄县| 疏勒县| 广平县| 左贡县| 宣武区| 依兰县| 双城市| 宝应县| 泰来县| 甘南县| 安平县| 商都县| 大同市| 措勤县| 益阳市| 江阴市| 长治市| 仁化县| 黄山市| 安康市| 漳浦县| 翁牛特旗| 丁青县| 京山县| 镇赉县| 大化| 巢湖市| 措勤县| 寻甸|