ShowContarct:
public interface ShowContarct {
//model
public interface ShowModel{
public void getModelData(String keyword,ShowModel.ModelCallBack modelCallBack);
public interface ModelCallBack{
public void success(String naem);
public void fail();
}
}
//view
public interface ShowView{
public void getData(String data);
}
//presenter
public interface ShowPresenter{
public void show(String keyword);
public void bind(ShowView showView);
public void unbind();
}
}
ShowModel:
public class ShowModel implements ShowContarct.ShowModel {
public static final String ShowUrl="http://172.17.8.100/small/commodity/v1/findCommodityByKeyword";
@Override
public void getModelData(String keyword, final ModelCallBack modelCallBack) {
Volley.getInstance().VolleyGet(ShowUrl+"?keyword="+URLEncoder.encode(keyword)+"&page="+1+"&count="+10,new Volley.VolleyCallBack(){
@Override
public void success(String names) {
modelCallBack.success(names);
}
@Override
public void error(VolleyError error) {
modelCallBack.fail();
}
});
}
}
ShowPresenter:
public class ShowPresenter implements ShowContarct.ShowPresenter {
ShowContarct.ShowView showView;
ShowModel showModel;
public void show(String keyword) {
showModel.getModelData(keyword,new ShowContarct.ShowModel.ModelCallBack() {
@Override
public void success(String naem) {
showView.getData(naem);
}
@Override
public void fail() {
}
});
}
@Override
public void bind(final ShowContarct.ShowView showView) {
this.showView=showView;
showModel=new ShowModel();
}
@Override
public void unbind() {
if (showView != null) {
showView=null;
}
if (showModel != null) {
showModel=null;
}
System.gc();
}
}
ShowActivity:
public class ShowActivity extends AppCompatActivity {
public ImageView image;
public TextView aname,aprice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
Intent intent=getIntent();
String cid=intent.getStringExtra("cid");
image=findViewById(R.id.show_image);
aname=findViewById(R.id.text_name);
aprice=findViewById(R.id.text_price);
aname.setText(intent.getStringExtra("name"));
aprice.setText(intent.getStringExtra("price"));
ObjectAnimator one= ObjectAnimator.ofFloat(image,"TranslationX",-600f,0f);
ObjectAnimator two= ObjectAnimator.ofFloat(image,"rotation",0f,360f);
AnimatorSet animatorSet=new AnimatorSet();
animatorSet.play(one).with(two);
animatorSet.setDuration(3000);
animatorSet.start();
RequestOptions options=RequestOptions.circleCropTransform();
Glide.with(this).load(cid).apply(options).into(image);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity implements ShowContarct.ShowView {
ShowContarct.ShowPresenter showPresenter;
public RecyclerView recyclerView;
public TextView search;
public EditText editText;
public TextView textView;
public ShowAdapter showAdapter;
public MyLiu myLiu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recy_view);
search=findViewById(R.id.search);
editText=findViewById(R.id.edit_text);
myLiu=findViewById(R.id.my_liusi);
GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
recyclerView.setLayoutManager(gridLayoutManager);
showPresenter=new ShowPresenter();
showPresenter.bind(this);
showPresenter.show("板鞋");
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView=new TextView(MainActivity.this);
String sss=editText.getText().toString();
textView.setText(sss+" ");
myLiu.addView(textView);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,textView.getText().toString(),Toast.LENGTH_SHORT).show();
String aaa=editText.getText().toString();
showPresenter.show(aaa);
}
});
}
});
}
@Override
public void getData(String data) {
Gson gson=new Gson();
Product product=gson.fromJson(data,Product.class);
final List<Product.ResultBean>result=product.getResult();
showAdapter=new ShowAdapter(result,MainActivity.this);
recyclerView.setAdapter(showAdapter);
showAdapter.setAdaClick(new ShowAdapter.AdaClick() {
@Override
public void toAa(int position) {
String masterPic = result.get(position).getMasterPic();
String name = result.get(position).getCommodityName();
String price = result.get(position).getPrice();
Intent intent = new Intent(MainActivity.this, ShowActivity.class);
intent.putExtra("cid",masterPic);
intent.putExtra("name",name);
intent.putExtra("price",price);
startActivity(intent);
}
});
}
}
流式布局:
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-1;
int x=0;
int y=0;
int row=1;
int childCount=getChildCount();
for (int i = 0; i <childCount ; i++) {
View childAt=getChildAt(i);
x+=childAt.getMeasuredWidth();
if (x>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 wm=MeasureSpec.getMode(widthMeasureSpec);
int ws=MeasureSpec.getSize(widthMeasureSpec);
int hm=MeasureSpec.getMode(heightMeasureSpec);
int hs=MeasureSpec.getSize(heightMeasureSpec);
int mw=0;
int mh=0;
if (wm == MeasureSpec.EXACTLY&&hm==MeasureSpec.EXACTLY) {
mw=ws;
mh=hs;
}else {
mw=ws;
int childCount=getChildCount();
int clw=0;
int row=1;
int childwidth=0;
int childheight=0;
for (int i = 0; i <childCount ; i++) {
View child=getChildAt(i);
child.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
childwidth=child.getMeasuredWidth();
childheight=child.getMeasuredHeight();
clw+=childwidth;
if (clw>mw) {
clw=childwidth;
row++;
}
mh=row*childheight;
}
}
setMeasuredDimension(mw,mh);
}
}
布局:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".showmvp.V.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:hint="@string/app_shu"
/>
<TextView
android:id="@+id/search"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="@string/app_cha"
android:layout_marginTop="20dp"
/>
</LinearLayout>
<com.example.lian0426.view.MyLiu
android:id="@+id/my_liusi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
></com.example.lian0426.view.MyLiu>
<android.support.v7.widget.RecyclerView
android:id="@+id/recy_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
折线图:
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}};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//平移坐标原点
canvas.translate(50,height-100);
//有多少条数据,把X轴分成多少份,
drawLineXAxis(canvas);
//Y轴画
drawLineYAxis(canvas);
//折线画
drawLinePoints(canvas);
}
float pointX = 0;
float pointY = 0;
private void drawLinePoints(Canvas canvas) {
float pointXTemp = 0;
float pointYTemp = 0;
for(int i =0;i<points.length;i++){
float temp = points[i][0]%points.length;
if(temp==0){
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));
canvas.drawCircle(pointX, pointY, 5, pointPaint);
canvas.drawText("("+((int)points[i][0]+","+(int)points[i][1])+")",pointX-20,pointY-20,textPaint);
if( i!=0) {
canvas.drawLine(pointXTemp, pointYTemp, pointX, pointY, linePaint);
}
pointXTemp = pointX;
pointYTemp = pointY;
}
}
private void drawLineYAxis(Canvas canvas) {
int startX = 0;
int startY = 0;
int spaceing = (height-100)/points.length;
//每次画一小段
for (int i = 0;(startY+spaceing*i)<height-50;i++){
//y轴线
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);
}
}
private void drawLineXAxis(Canvas canvas) {
int startX = 0;
int startY = 0;
int spaceing = (width-100)/points.length;
//每次画一小段
for (int i = 0;(startX+spaceing*i)<width-10;i++){
canvas.drawLine(startX,startY,startX+spaceing*i ,startY,linePaint);
canvas.drawCircle(startX+spaceing*i, startY, 10, linePaint);
canvas.drawText(i+0+"",startX+spaceing*i,startY+30,textPaint);
}
}
}
折线图布局:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".showmvp.V.ShowActivity">
<ImageView
android:id="@+id/show_image"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="name"/>
<TextView
android:id="@+id/text_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:layout_gravity="center"
android:text="price"/>
<com.example.lian0426.view.MyView
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="700dp"></com.example.lian0426.view.MyView>
</LinearLayout>