首先要导入mXlistview模块(一个Modle)让项目依赖mlistview
MainActivity代码
public class MainActivity extends AppCompatActivity { private XListView xlist; private int type=1;//类型 private int index=1;//下标 private String url="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page="+index; Listlist = new ArrayList<>(); private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到控件 xlist = findViewById(R.id.xlist); //设置上拉下拉 xlist.setPullLoadEnable(true); xlist.setPullRefreshEnable(true); //调用加载数据方法 requestdata();//进页面先加载数据 在加载更多 //为xlist设置监听 xlist.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() {//刷新 下拉 type=1; index=1; url="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1"+index; requestdata(); } @Override public void onLoadMore() {//加载更多 上拉 type=2; //再次执行,页数增加 index++; url="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1"+index; requestdata();//调用执行请求数据 } }); } //请求数据 private void requestdata() { MyTask myTask=new MyTask(); myTask.execute(url); } class MyTask extends AsyncTask { @Override protected String doInBackground(String... strings) { try { URL url=new URL(strings[0]); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); urlConnection.setRequestMethod("GET"); int responseCode = urlConnection.getResponseCode(); if(responseCode==200){ InputStream inputStream = urlConnection.getInputStream(); String s = getdata(inputStream); return s; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if(s!=null){ Gson gson=new Gson(); MyGson myGson = gson.fromJson(s, MyGson.class); if(type==1){ xlist.stopRefresh();//停止刷新 //新建一个时间对象 System.currentTimeMillis()获得系统时间 Date date=new Date(System.currentTimeMillis()); //新建simple SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = sdf.format(date);//转换时间类型 //设置时间 xlist.setRefreshTime(format); list.clear(); }else{ //关闭视图 xlist.stopLoadMore();//停止刷新更多 } list.addAll(myGson.getData()); setAdapter(); } } } private void setAdapter() { if (adapter==null){ adapter= new MyAdapter(MainActivity.this,list); xlist.setAdapter(adapter); }else { adapter.notifyDataSetChanged(); } } private String getdata(InputStream inputStream) { StringBuilder builder=new StringBuilder(); BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream)); String str; try { while((str=reader.readLine())!=null){ builder.append(str); } } catch (Exception e) { e.printStackTrace(); } return builder.toString(); } }
适配器代码
class MyAdapter extends BaseAdapter{ private final int ONE_IMAGE=0; private final int TWO_IMAGE=1; Context context; Listlist; private DisplayImageOptions options; public MyAdapter(Context context, List list) { this.context=context; this.list=list; options = new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时 .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2字节 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 .displayer(new RoundedBitmapDisplayer(100))//设置圆角 30代表半径 自定义 .build(); } @Override public int getItemViewType(int position) { if(position%2==0){ return ONE_IMAGE; }else{ return TWO_IMAGE; } } @Override public int getViewTypeCount() { return 2; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { int type = getItemViewType(i); if(type==ONE_IMAGE){ ViewHolder1 holder1; if(view==null){ view=View.inflate(context,R.layout.item1,null); holder1=new ViewHolder1(); holder1.text01=view.findViewById(R.id.list_text01); view.setTag(holder1); }else{ holder1= (ViewHolder1) view.getTag(); } holder1.text01.setText(list.get(i).getTitle()); return view; }else{ ViewHolder2 holder2; if(view==null){ view=View.inflate(context,R.layout.item2,null); holder2=new ViewHolder2(); holder2.text02=view.findViewById(R.id.list_tex02); holder2.image02=view.findViewById(R.id.list_image02); view.setTag(holder2); }else{ holder2= (ViewHolder2) view.getTag(); } holder2.text02.setText(list.get(i).getTitle()); ImageLoader.getInstance().displayImage(list.get(i).getImg(),holder2.image02,options); } return view; } private class ViewHolder1{ TextView text01; } private class ViewHolder2{ ImageView image02; TextView text02; } }
MyAplaction代码
public class MyAplaction extends Application{ @Override public void onCreate() { super.onCreate(); //自定义sd卡缓存目录 File file=new File(Environment.getExternalStorageDirectory()+"/images"); //初始化,,全局配置applction ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this) .memoryCacheExtraOptions(400, 700)//缓存图片最大的长和宽 .threadPoolSize(3)//线程池的数量 .threadPriority(4) .memoryCacheSize(20*1024*1024)//设置内存缓存区大小 .diskCacheSize(80*1024*1024)//设置sd卡缓存区大小 .diskCache(new UnlimitedDiscCache(file))//自定义sd卡缓存目录 .writeDebugLogs()//打印日志内容 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理 .build(); ImageLoader.getInstance().init(configuration); } }
MyGson代码
(Alt+s把接口数据放进去)
布局代码(MainActivity)
<com.bawei.jane.mxlistview.view.XListView android:id="@+id/xlist" android:layout_width="match_parent" android:layout_height="match_parent">com.bawei.jane.mxlistview.view.XListView>
list_layout1布局
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_text01" android:padding="20dp"/> list_layout2布局
<ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/list_image02" android:layout_marginLeft="20dp" android:layout_marginTop="20dp"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/list_tex02" android:text="aaaa" android:padding="20dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp"/>清单文件
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.com.rikao1_5"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:name=".MyAplaction"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> application> manifest>