工具类:
package com.example.littleup.util;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Authenticator;
import com.android.volley.toolbox.StringRequest;
import com.example.littleup.MyApp;
import java.util.Map;
public class VolleyHttp {
private static VolleyHttp volleyHttp=new VolleyHttp();
//饿汉式
public static VolleyHttp getInstance(){
return volleyHttp;
}
//请求数据
public void getVolley(String url, final VolleyCallBack callBack){
StringRequest stringRequest= new StringRequest(StringRequest.Method.GET, url, new Response.Listener() {
@Override
public void onResponse(String response) {
callBack.success(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callBack.faile(error);
}
});
stringRequest.setTag("volley");
MyApp.getVolley().add(stringRequest);
}
public interface VolleyCallBack{
void success(String names);
void faile(VolleyError error);
}
}
MyApp:
package com.example.littleup;
import android.app.Application;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class MyApp extends Application {
private static RequestQueue requestQueue;
@Override
public void onCreate() {
super.onCreate();
requestQueue = Volley.newRequestQueue(getApplicationContext());
Thread.setDefaultUncaughtExceptionHandler(CrashHandler.getInatance(this));
}
public static RequestQueue getVolley(){
return requestQueue;
}
}
mvp契约类:
ContractInteface:
package com.example.littleup.showmvp;
public interface ContractInteface {
//view
interface IShowView{
void getPreDa(String data);
}
//model
interface IShowmodel{
void getModelData(String keyword,IShowmodel.ModelCallBack callBack);
interface ModelCallBack{
void onSuccess(String name);
void onFaile();
}
}
//presenter
interface IShowPresenter{
void showshow(String keyword);
void attch(IShowView view);
void detach();
}
}
MyModel:
package com.example.littleup.showmvp;
import com.android.volley.VolleyError;
import com.example.littleup.util.VolleyHttp;
import java.net.URLEncoder;
public class MyModel implements ContractInteface.IShowmodel {
String showUrl="http://172.17.8.100/small/commodity/v1/findCommodityByKeyword";
@Override
public void getModelData(String keyword,final ModelCallBack callBack) {
VolleyHttp.getInstance().getVolley(showUrl+"?keyword="+URLEncoder.encode(keyword)+"&page="+1+"&count="+10, new VolleyHttp.VolleyCallBack() {
@Override
public void success(String names) {
callBack.onSuccess(names);
}
@Override
public void faile(VolleyError error) {
callBack.onFaile();
}
});
}
}
MyPresenter:
package com.example.littleup.showmvp;
public class MyPresenter implements ContractInteface.IShowPresenter {
ContractInteface.IShowView iShowView;
MyModel myModel;
@Override
public void showshow(String keyword) {
myModel.getModelData(keyword,new ContractInteface.IShowmodel.ModelCallBack() {
@Override
public void onSuccess(String name) {
iShowView.getPreDa(name);
}
@Override
public void onFaile() {
}
});
}
@Override
public void attch(ContractInteface.IShowView view) {
this.iShowView=view;
myModel=new MyModel();
}
@Override
public void detach() {
if (iShowView!=null){
iShowView=null;
}
if (myModel!=null){
myModel=null;
}
System.gc();
}
}
适配器:
package com.example.littleup.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.littleup.R;
import com.example.littleup.bean.News;
import com.example.littleup.bean.Product;
import com.example.littleup.bean.ShowBean;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter
List
Context context;
AdaClick adaClick;
public MyAdapter(List
this.resultBeans = resultBeans;
this.context = context;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = LayoutInflater.from(context).inflate(R.layout.grid, null);
return new Holder(inflate);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, final int i) {
holder.textViewa.setText(resultBeans.get(i).getCommodityName());
holder.textViewb.setText(resultBeans.get(i).getPrice());
Glide.with(context).load(resultBeans.get(i).getMasterPic()).into(holder.imageView);
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adaClick.toAa(i);
}
});
}
@Override
public int getItemCount() {
return resultBeans.size();
}
public class Holder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textViewa,textViewb;
public Holder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.view_image);
textViewa=itemView.findViewById(R.id.view_texta);
textViewb=itemView.findViewById(R.id.view_textb);
}
}
public void setAdaClick(AdaClick adaClick) {
this.adaClick = adaClick;
}
public interface AdaClick{
void toAa(int position);
}
}
bean类:
showbean:
package com.example.littleup.bean;
import java.util.List;
public class ShowBean {
private List
public ShowBean(List
this.result = result;
}
public List
return result;
}
public void setResult(List
this.result = result;
}
@Override
public String toString() {
return "ShowBean{" +
"result=" + result +
'}';
}
}
product:
package com.example.littleup.bean;
import java.util.List;
public class Product {
/**
* result : [{"commodityId":101,"commodityName":"vivo X23幻彩版 多套餐 全面屏拍照美颜超大广角摄影水滴屏智能4G 正品vivox23手机","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/sj/2/1.jpg","price":2798,"saleNum":0},{"commodityId":112,"commodityName":"黑色重力支架 车载手机架汽车用导航支架车上支撑出风口汽车内多功能通用型金属车载支架","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/sjpj/6/1.jpg","price":35,"saleNum":0},{"commodityId":109,"commodityName":"新款奢华镶钻镜面iphone xs max手机壳苹果7plusl软边时尚保护套","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/sjpj/3/1.jpg","price":89,"saleNum":0},{"commodityId":104,"commodityName":"OPPO R17 全网通 8G+128G 美拍补光灯+美容补水仪套餐 全面屏AI智慧美颜双摄拍照手机","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/sj/5/1.jpg","price":3799,"saleNum":0},{"commodityId":119,"commodityName":"斯泰克 吃鸡神器手游键盘 快捷射击辅助按键四指手机游戏 绝地求生刺激战场游戏手柄合金款 苹果安卓通用 扳机射击按键(2件装)","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/yyyl/6/1.jpg","price":39,"saleNum":0},{"commodityId":100,"commodityName":"【壳膜线套餐】 苹果 iPhone XS 256G 全网通手机","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/sj/1/1.jpg","price":10069,"saleNum":0},{"commodityId":111,"commodityName":"三合一充电器数据线苹果二合一拖安卓手机多用功能多头车载苹果安卓一拖三数据线Type-C铝合金编织线 土豪金","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/sjpj/5/1.jpg","price":39,"saleNum":0},{"commodityId":106,"commodityName":"三星Galaxy S9+ 6GB+128GB版曲屏手机/指纹识别手机/拍照手机","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/sj/7/1.jpg","price":6199,"saleNum":0},{"commodityId":103,"commodityName":"【送自拍杆】魅族 魅蓝 E3 64G 全面屏 全网通手机","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/sj/4/1.jpg","price":1638,"saleNum":0},{"commodityId":114,"commodityName":"羽博 可悬挂无线蓝牙便携音响 布面工艺方形户外超重低音蓝牙音响车载手机迷你蓝牙音箱","masterPic":"http://172.17.8.100/images/small/commodity/sjsm/yyyl/1/1.jpg","price":49,"saleNum":0}]
* message : 查询成功
* status : 0000
*/
private String message;
private String status;
private List
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List
return result;
}
public void setResult(List
this.result = result;
}
public static class ResultBean {
/**
* commodityId : 101
* commodityName : vivo X23幻彩版 多套餐 全面屏拍照美颜超大广角摄影水滴屏智能4G 正品vivox23手机
* masterPic : http://172.17.8.100/images/small/commodity/sjsm/sj/2/1.jpg
* price : 2798
* saleNum : 0
*/
private String commodityId;
private String commodityName;
private String masterPic;
private String price;
private String saleNum;
public String getCommodityId() {
return commodityId;
}
public void setCommodityId(String commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getMasterPic() {
return masterPic;
}
public void setMasterPic(String masterPic) {
this.masterPic = masterPic;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getSaleNum() {
return saleNum;
}
public void setSaleNum(String saleNum) {
this.saleNum = saleNum;
}
}
}
news:
package com.example.littleup.bean;
/**
* Time:2019/4/25
* Author:秦超蒙
* Description:
*/
public class News {
private String commodityName;
private String masterPic;
private String price;
public News(String commodityName, String masterPic, String price) {
this.commodityName = commodityName;
this.masterPic = masterPic;
this.price = price;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public String getMasterPic() {
return masterPic;
}
public void setMasterPic(String masterPic) {
this.masterPic = masterPic;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public String toString() {
return "News{" +
"commodityName='" + commodityName + '\'' +
", masterPic='" + masterPic + '\'' +
", price='" + price + '\'' +
'}';
}
}
布局:
activity_main:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
android:layout_height="wrap_content"
android:orientation="horizontal"
>
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:hint="@string/app_shu"
/>
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="@string/app_cha"
android:layout_marginTop="20dp"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
>
android:layout_width="match_parent"
android:layout_height="match_parent"
>
activity_show:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ShowActivity"
android:orientation="vertical"
>
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:src="@mipmap/ic_launcher"
/>
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
/>
grid:
android:layout_height="match_parent"
android:orientation="vertical"
>
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:src="@mipmap/ic_launcher"
/>
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="20sp"
/>
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="20sp"
/>
MyLiu:
package com.example.littleup;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class MyLiu extends ViewGroup {
public MyLiu(Context context) {
super(context);
}
public MyLiu(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLiu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 控制子控件的摆放
// 先获取容器的宽度
int width = r - l;
int x = 0;
int y = 0;
int row = 1;
int childCount = getChildCount();
for (int i = 0; i width){
x=childAt.getMeasuredWidth();
row++;
}
y=row*childAt.getMeasuredHeight();
childAt.layout(x-childAt.getMeasuredWidth(),y-childAt.getMeasuredHeight(),x,y);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取测量模式
int widthmode = MeasureSpec.getMode(widthMeasureSpec);
int widthsize = MeasureSpec.getSize(widthMeasureSpec);
int heightmode = MeasureSpec.getMode(heightMeasureSpec);
int heightsize = MeasureSpec.getSize(heightMeasureSpec);
// 定义两个变量,保存测量后的宽度和高度
int measureWidth = 0;
int measureHeight = 0;
//判断测量模式
if (widthmode==MeasureSpec.EXACTLY&&heightmode==MeasureSpec.EXACTLY){
//精确 赋值
measureWidth=widthsize;
measureHeight=heightsize;
}else {
measureWidth=widthsize;
int childCount = getChildCount();
int currentLineWidth=0; // 当前行已经使用了多宽
int row = 1;
int childWidth = 0;
int childHeight = 0;
for (int i = 0; i measureWidth){
currentLineWidth=childWidth;
row++;
}
measureHeight=row*childHeight;
}
}
setMeasuredDimension(measureWidth,measureHeight);
}
}
MyView:
package com.example.littleup;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
// 宽
private int width;
// 高
private int height;
// 半透明画笔
private Paint alphaLinePaint;
// 线条画笔
private Paint linePaint;
// 横坐标画笔
private Paint textPaint;
//点的坐标
private Paint pointPaint;
//点的坐标
private Paint pointPaint1;
// 空提示
private Paint textEmptyPaint;
private Context context;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
init();
}
private void init() {
//画笔 X轴
linePaint = new Paint();
linePaint.setStyle(Paint.Style.FILL);
linePaint.setColor(context.getResources().getColor(
android.R.color.holo_red_dark));
linePaint.setAntiAlias(true);
linePaint.setStrokeWidth((float) 5.0);
//文字画笔
textPaint = new Paint();
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(context.getResources().getColor(
android.R.color.holo_red_dark));
textPaint.setAntiAlias(true);
textPaint.setTextSize(30);
//文字画笔
pointPaint = new Paint();
pointPaint.setTextAlign(Paint.Align.CENTER);
pointPaint.setStyle(Paint.Style.FILL);
pointPaint.setColor(context.getResources().getColor(
android.R.color.holo_blue_light));
pointPaint.setAntiAlias(true);
pointPaint.setTextSize(20);
//文字画笔1
pointPaint1= new Paint();
pointPaint1.setTextAlign(Paint.Align.CENTER);
pointPaint1.setStyle(Paint.Style.FILL);
pointPaint1.setColor(context.getResources().getColor(
android.R.color.holo_blue_light));
pointPaint1.setAntiAlias(true);
pointPaint1.setTextSize(20);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY);
setMeasuredDimension(widthSpec, heightSpec);
}
protected float[][] points = new float[][]{{1,4}, {2,50}, {3,11}, {4,38}, {5,9},{6,52}, {7,14}, {8,37}, {9,29}, {10,31}};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//平移坐标原点
canvas.translate(50,height-100);
//有多少条数据,把X轴分成多少份,
drawLineXAxis(canvas);
//Y轴画
drawLineYAxis(canvas);
//折线画
drawLinePoints(canvas);
} float temp = points[i][0]%points.length; if(temp==0){ canvas.drawCircle(pointX, pointY, 5, pointPaint); canvas.drawText("("+((int)points[i][0]+","+(int)points[i][1])+")",pointX-20,pointY-20,textPaint); } private void drawLineYAxis(Canvas canvas) { private void drawLineXAxis(Canvas canvas) { showactivity: package com.example.littleup; import android.content.Intent; import com.bumptech.glide.Glide; public class ShowActivity extends AppCompatActivity { private ImageView show_image; @Override mainactivity: package com.example.littleup; import android.content.Intent; import android.view.View; import com.example.littleup.adapter.MyAdapter; import com.example.littleup.bean.Product; import com.example.littleup.showmvp.ContractInteface; public class MainActivity extends AppCompatActivity implements ContractInteface.IShowView { private RecyclerView recy_view; @Override GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2); search.setOnClickListener(new View.OnClickListener() { my_liu.addView(textView); textView.setOnClickListener(new View.OnClickListener() { @Override myAdapter=new MyAdapter(result,MainActivity.this); recy_view.setAdapter(myAdapter); CrashHandler 类: package com.example.littleup; import android.content.Context; volatile private static CrashHandler sCrashHandler; public CrashHandler(Context context) { public static CrashHandler getInatance(Context context) { } //可根据情况选择是否干掉当前的进程 }
float pointX = 0;
float pointY = 0;
private void drawLinePoints(Canvas canvas) {
float pointXTemp = 0;
float pointYTemp = 0;
for(int i =0;i
pointX = 0+(points[i][0])*((width-100)/points.length);
}else{
pointX = 0+(points[i][0]%points.length)*((width-100)/points.length);
}
pointY = 0-(points[i][1]/60)*((height-100));
if( i!=0) {
canvas.drawLine(pointXTemp, pointYTemp, pointX, pointY, linePaint);
}
pointXTemp = pointX;
pointYTemp = pointY;
}
int startX = 0;
int startY = 0;
int spaceing = (height-100)/points.length;
//每次画一小段
for (int i = 0;(startY+spaceing*i)
canvas.drawLine(startX,startY,startX ,startY-spaceing*i,linePaint);
//y轴点
canvas.drawCircle(startX, startY-spaceing*i, 5, linePaint);
//y数字
canvas.drawText(50*i+"",startX-10,startY-spaceing*i,textPaint);
}
}
int startX = 0;
int startY = 0;
int spaceing = (width-100)/points.length;
//每次画一小段
for (int i = 0;(startX+spaceing*i)
canvas.drawCircle(startX+spaceing*i, startY, 10, linePaint);
canvas.drawText(i+0+"",startX+spaceing*i,startY+30,textPaint);
}
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
Intent intent = getIntent();
String cid = intent.getStringExtra("cid");
show_image = findViewById(R.id.show_image);
Glide.with(this).load(cid).into(show_image);
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.littleup.showmvp.MyPresenter;
import com.google.gson.Gson;
import java.util.List;
//生声明P层
ContractInteface.IShowPresenter presenter;
private TextView search;
private EditText edit_text;
private TextView textView;
private MyLiu my_liu;
private MyAdapter myAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
recy_view = findViewById(R.id.recy_view);
search = findViewById(R.id.search);
edit_text = findViewById(R.id.edit_text);
my_liu = findViewById(R.id.my_liu);
recy_view.setLayoutManager(gridLayoutManager);
presenter=new MyPresenter();
presenter.attch(this);
presenter.showshow("板鞋");
@Override
public void onClick(View v) {
textView=new TextView(MainActivity.this);
String s = edit_text.getText().toString();
textView.setText(s+" ");
@Override
public void onClick(View v) {
for (int i = 0; i < textView.length(); i++) {
Toast.makeText(MainActivity.this,textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
String s1 = edit_text.getText().toString();
presenter.showshow(s1);
}
});
}
});
}
public void getPreDa(String data) {
Gson gson=new Gson();
Log.i("sss","请求的数据是:"+data);
Product product = gson.fromJson(data, Product.class);
final List
Log.i("sss","展示的数据是:");
myAdapter.setAdaClick(new MyAdapter.AdaClick() {
@Override
public void toAa(int position) {
String masterPic = result.get(position).getMasterPic();
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
intent.putExtra("cid",masterPic);
startActivity(intent);
}
});
}
}
import android.util.Log;
public class CrashHandler implements Thread.UncaughtExceptionHandler {
Context context;
private static final String TAG = "CrashHandler";
this.context = context;
}
{
if (sCrashHandler == null) {
synchronized (CrashHandler.class) {
if (sCrashHandler == null) {
//使用Application Context
sCrashHandler = new CrashHandler(context.getApplicationContext());
}
}
}
return sCrashHandler;
}
@Override
public void uncaughtException (Thread t, Throwable e){
if (BuildConfig.DEBUG) Log.d(TAG, "uncaughtException:"+t.getName()+" "+e.getMessage());
//这里可以将异常信息保存或上传
//...
android.os.Process.killProcess(android.os.Process.myPid());
}