1.Model层
1》定义bean类
public void Shop(){
public double bargainPrice; public String createtime; public String detailUrl; public String images; public int itemtype; public int pid; public double price; public int pscid; public int salenum; public int sellerid; public String subhead; public String title; @Override public String toString() { return "Shop{" + "bargainPrice=" + bargainPrice + ", createtime='" + createtime + '\'' + ", detailUrl='" + detailUrl + '\'' + ", images='" + images + '\'' + ", itemtype=" + itemtype + ", pid=" + pid + ", price=" + price + ", pscid=" + pscid + ", salenum=" + salenum + ", sellerid=" + sellerid + ", subhead='" + subhead + '\'' + ", title='" + title + '\'' + '}'; } }
2》RootDate定义资源
public class RootData { public int status;//网络状态 public String message;//请求日志信息 public List<Shop> data;//定义bean的数据 @Override public String toString() { return "RootData{" + "status=" + status + ", message='" + message + '\'' + ", data=" + data + '}'; } }3》定义 ITaskCallBack接口
public interface ITaskCallBack { public void pre();//网络初始化 public void update();//网络请求中 public void success(RootData data);//请求返回数据 public void failed(String msg);//数据处理失败 }4》定义IShopTask接口(对数据进行增删改查)
public interface IShopTask { //获取商铺 列表 public void getShopList(String url,ITaskCallBack callBack); //获取商铺信息 public void getShopInfo(String url,ITaskCallBack callBack); //修改商铺信息 public void updataShopInfo(String url,ITaskCallBack callBack);5》请求数据类
public class ShopTask implements IShopTask { @Override public void getShopList(String url, final ITaskCallBack callBack) { StringRequest request=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Gson gson=new Gson(); RootData data = gson.fromJson(response, RootData.class); callBack.success(data); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { callBack.failed(error.getMessage()); } }); VolleyUtil.getVolley().addToQueue(request); } @Override public void getShopInfo(String url, ITaskCallBack callBack) { } @Override public void updataShopInfo(String url, ITaskCallBack callBack) { } }2.Presrenter层
1》定义IShopPresenter接口
public interface IShopPresenter { void getList(); }
2》定义ShopPresenter
public class ShopPresenter implements IShopPresenter, ITaskCallBack{ //注入M层 IShopTask shopTask; //注入V层 IShopView shopView; public ShopPresenter(MainActivity activity){ shopTask = new ShopTask(); shopView=activity; } @Override public void pre() { } @Override public void update() { } @Override public void success(RootData data) { Log.i("shop",data.toString()); shopView.showData(data); } @Override public void failed(String msg) { } @Override public void getList() { String path="https://www.zhaoapi.cn/product/getProducts?pscid="; int page=1; String url=path+page; shopTask.getShopList(url,this); } }3.View层
1》定义IShopView接口
public interface IShopView { public void showData(RootData data); }2》activity
public class MainActivity extends AppCompatActivity implements IShopView{ ShopPresenter presenter; XListView xListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xListView=findViewById(R.id.xlistview); presenter=new ShopPresenter(this); presenter.getList(); } @Override public void showData(RootData data) { /* xListView.setPullRefreshEnable(true); xListView.setPullLoadEnable(true); xListView.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { } @Override public void onLoadMore() { } });*/ ShopAdapter shopAdapter=new ShopAdapter(this); shopAdapter.addData(data.data); xListView.setAdapter(shopAdapter); } }3》适配器
public class ShopAdapter extends BaseAdapter{ private Context context; private List<Shop> list; public ShopAdapter(Context context) { this.context = context; list=new ArrayList<>(); } public void addData(List<Shop> shops) { this.list.addAll(shops); notifyDataSetChanged(); } @Override public int getCount() { return list.size(); } @Override public Shop 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) { ViewHolder holder; if (view==null){ view=View.inflate(context, R.layout.listview,null); holder=new ViewHolder(); holder.title=view.findViewById(R.id.title); holder.bargainPrice=view.findViewById(R.id.bargainPrice); holder.image=view.findViewById(R.id.image); view.setTag(holder); }else { holder= (ViewHolder) view.getTag(); } Shop shop=getItem(i); holder.title.setText(shop.title); holder.bargainPrice.setText(shop.bargainPrice+""); String images=shop.images; String pic_uri=images.split("\\|")[1]; com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(pic_uri,holder.image, App.getOptions()); return view; } class ViewHolder{ TextView title,bargainPrice; ImageView image; } } 4.App
public class App extends Application{ public static Context context; @Override public void onCreate() { super.onCreate(); context=this; //初始化 initImageLoder(); } private void initImageLoder() { File cacheDir = StorageUtils.getCacheDirectory(context); //缓存文件夹路径 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(480, 800) // default = device screen dimensions 内存缓存文件的最大长宽 .diskCacheExtraOptions(480, 800, null) // 本地缓存的详细信息(缓存的最大长宽),最好不要设置这个 .threadPoolSize(3) // default 线程池内加载的数量 .threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级 .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //可以通过自己的内存缓存实现 .memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值 .diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值 .diskCacheFileCount(100) // 可以缓存的文件数量 //default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密 .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .writeDebugLogs() // 打印debug log .build(); //开始构建 ImageLoader.getInstance().init(config); } public static DisplayImageOptions getOptions(){ DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_launcher_background) // 设置图片下载期间显示的图片 .showImageForEmptyUri(R.drawable.ic_launcher_background) // 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.ic_launcher_background) // 设置图片加载或解码过程中发生错误显示的图片 .resetViewBeforeLoading(false) // default 设置图片在加载前是否重置、复位 .delayBeforeLoading(0) // 下载前的延迟时间 .cacheInMemory(true) // default 设置下载的图片是否缓存在内存中 .cacheOnDisk(true) // default 设置下载的图片是否缓存在SD卡中 .bitmapConfig(Bitmap.Config.ARGB_8888) // default 设置图片的解码类型 .displayer(new RoundedBitmapDisplayer(10)) // default 还可以设置圆角图片new RoundedBitmapDisplayer(20) .handler(new Handler()) // default .build(); return options; } }5.先导volley第三框架
用在小build中
implementation project(path: ':volley')VolleyUtil
public class VolleyUtil { private static volatile VolleyUtil util;//单例 private static RequestQueue queue;//请求队列 private static Context context;//上下文环境 /** * 构造方法 */ public VolleyUtil() { context=App.context; queue= Volley.newRequestQueue(context); } /** * 获取单例 * @return */ public static VolleyUtil getVolley(){ if (util==null){//第一重检测 synchronized (VolleyUtil.class){ if (util==null){ util=new VolleyUtil(); } } } return util; } /** * 添加请求到队列,定义泛型方法 * @param request * @param <T> */ public <T> void addToQueue(Request<T> request){ queue.add(request); } }
6.xml
<TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="标题" android:textSize="18dp" android:gravity="center" android:layout_marginTop="10dp" /> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="2dp" android:layout_below="@+id/title" android:background="#000" android:layout_marginTop="5dp" /> <view.XListView android:id="@+id/xlistview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/textview" ></view.XListView>展示条目数据布局
<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="16dp" android:layout_marginTop="14dp" android:text="TextView" android:maxEms="15" android:textSize="16dp" android:textColor="#000" /> <TextView android:id="@+id/bargainPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignStart="@+id/title" android:layout_marginTop="60dp" android:text="TextView" /> <ImageView android:id="@+id/image" android:layout_width="80dp" android:layout_height="50dp" android:layout_alignBottom="@+id/bargainPrice" android:layout_alignParentEnd="true" android:layout_marginEnd="16dp" android:src="@drawable/ic_launcher_background" />