前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >XListView实现多条目网络数据刷新加载 网络加载图片

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

作者头像
砸漏
发布2020-11-04 15:23:16
5110
发布2020-11-04 15:23:16
举报
文章被收录于专栏:恩蓝脚本

本文实例为大家分享了XListView实现刷新加载的具体代码,供大家参考,具体内容如下

MainActivity.java

代码语言:javascript
复制
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

代码语言:javascript
复制
 <?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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(ImageloaderConfigs.getImageLoader(this));
}
}

ResponsBean.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档