效果图
布局
<?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=".MainActivity">
<com.bw.zhoukao2.wight.MyBoView
android:id="@+id/myview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingLeft="10dp"
android:paddingRight="10dp"
tools:context=".LoginActivity">
<EditText
android:id="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@drawable/edit_bg"
android:hint="请输入账号"
android:inputType="phone"
android:paddingLeft="5dp" />
<EditText
android:id="@+id/user_pass"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@drawable/edit_bg"
android:hint="请输入密码"
android:inputType="phone"
android:paddingLeft="5dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="保存密码" />
<TextView
android:id="@+id/tv_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="立即注册" />
</RelativeLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@drawable/btn_bg"
android:text="登录"
android:textColor="#ffffff" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingLeft="10dp"
android:paddingRight="10dp"
tools:context=".LoginActivity">
<EditText
android:id="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@drawable/edit_bg"
android:hint="请输入账号"
android:inputType="phone"
android:paddingLeft="5dp" />
<EditText
android:id="@+id/user_pass"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@drawable/edit_bg"
android:hint="请输入密码"
android:inputType="textPassword"
android:paddingLeft="5dp" />
<EditText
android:id="@+id/user_pass2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@drawable/edit_bg"
android:hint="请确认密码"
android:inputType="textPassword"
android:paddingLeft="5dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/tv_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="已有账号,立即登录" />
</RelativeLayout>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@drawable/btn_bg"
android:text="注册"
android:textColor="#ffffff" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".ZhanActivity">
<com.hjm.bottomtabbar.BottomTabBar
android:id="@+id/bottom_tab_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_all"
android:layout_width="match_parent"
android:layout_height="50dp">
<EditText
android:id="@+id/ed_text"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/btn_seach"
android:background="@drawable/edit_bg" />
<Button
android:id="@+id/btn_seach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="搜索" />
</RelativeLayout>
<com.bw.zhoukao2.wight.LiuLayoutView
android:id="@+id/layout_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp" />
<RelativeLayout
android:layout_below="@id/layout_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00">
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0ff">
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_bg"
android:padding="2dp"
android:layout_margin="3dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/title_id"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_below="@+id/image" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@+id/title_id">
<TextView
android:id="@+id/title_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#d43c3c" />
<TextView
android:id="@+id/title_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
流式布局...注意
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal"
>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layout_all"
>
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:layout_weight="1"
android:background="@drawable/edit_bg"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_txt"
android:layout_centerInParent="true"
android:textSize="20sp"
/>
</RelativeLayout>
商品展示布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edit_bg"
android:padding="2dp"
android:layout_margin="3dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/title_id"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_below="@+id/image" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@+id/title_id">
<TextView
android:id="@+id/title_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#d43c3c" />
<TextView
android:id="@+id/title_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp"/>
<solid android:color="@color/colorPrimaryDark"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#999999" />
</shape>
MainActivity
package com.bw.zhoukao2;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.bw.zhoukao2.wight.MyBoView;
public class MainActivity extends AppCompatActivity {
MyBoView myview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myview=findViewById(R.id.myview);
//获取宽和高
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
//平移
ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(myview, "translationY", 0, height-200);
ObjectAnimator objectAnimator1= ObjectAnimator.ofFloat(myview, "translationX",0,width-200);
//渐变
//ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(myview, "alpha", 1, 0.8f, 0, 0.5f, 1);
AnimatorSet animset = new AnimatorSet();
animset.play(objectAnimator).with(objectAnimator1);
animset.setDuration(6000);
animset.start();
// 动画监听
animset.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
//走完的时候跳转页面
@Override
public void onAnimationEnd(Animator animator) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
myview.setColor(Color.RED);
}
},1000);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
myview.setColor(Color.BLUE);
}
},2000);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
myview.setColor(Color.BLACK);
}
},5000);
}
}
LoginActivity
package com.bw.zhoukao2;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.bw.zhoukao2.mvp.bean.LoginBean;
import com.bw.zhoukao2.mvp.model.UserModelIml;
import com.bw.zhoukao2.mvp.presenter.UserPresenterIml;
import com.bw.zhoukao2.mvp.view.UserView;
import com.google.gson.Gson;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, UserView {
private SharedPreferences sp;
private EditText mUserName, mUserPass;
private UserPresenterIml userPresenterIml;
private CheckBox mCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
findViewById(R.id.tv_register).setOnClickListener(this);
findViewById(R.id.btn_login).setOnClickListener(this);
mUserName = (EditText) findViewById(R.id.user_name);
mUserPass = (EditText) findViewById(R.id.user_pass);
mCheckBox = (CheckBox) findViewById(R.id.checkbox);
sp = getSharedPreferences("user", MODE_PRIVATE);
userPresenterIml = new UserPresenterIml(new UserModelIml(), this);
}
@Override
protected void onResume() {
super.onResume();
String userName = sp.getString("user_name", null);
String userPass = sp.getString("user_pass", null);
if (!TextUtils.isEmpty(userName)) {
mUserName.setText(userName);
}
if (!TextUtils.isEmpty(userPass)) {
mUserPass.setText(userPass);
mCheckBox.setChecked(true);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_register://跳转到注册
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
break;
case R.id.btn_login://点击登录
doLogin();
break;
}
}
//登录
private void doLogin() {
String userName = mUserName.getText().toString().trim();
String userPass = mUserPass.getText().toString().trim();
if (TextUtils.isEmpty(userName)) {
toast("请输入您的账号");
return;
}
if (TextUtils.isEmpty(userPass)) {
toast("请输入您的密码");
return;
}
boolean isCheckBox = mCheckBox.isChecked();
if (isCheckBox) {//保存
sp.edit().putString("user_name", userName)
.putString("user_pass", userPass).commit();
} else {//清空
sp.edit().putString("user_name", "")
.putString("user_pass", "").commit();
}
//走登录
userPresenterIml.doLogin(userName, userPass);
}
private void toast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void sueccss(String data) {
LoginBean bean = new Gson().fromJson(data, LoginBean.class);
sp.edit().putString("user_name", bean.getResult().getPhone())
.putString("user_nickName", bean.getResult().getNickName())
.putString("user_pictrue", bean.getResult().getHeadPic())
.commit();
//跳到Fragmwnt主页面
startActivity(new Intent(LoginActivity.this,ZhanActivity.class));
}
@Override
public void fail(String error) {
toast(error);
}
}
RegisterActivity
package com.bw.zhoukao2;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.bw.zhoukao2.mvp.bean.UserBean;
import com.bw.zhoukao2.mvp.model.UserModelIml;
import com.bw.zhoukao2.mvp.presenter.UserPresenterIml;
import com.bw.zhoukao2.mvp.view.UserView;
import com.google.gson.Gson;
//注册页面
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, UserView {
private EditText mUserName, mUserPass, mUserPass2;
private UserPresenterIml mUserPresenterIml;
private SharedPreferences sp;
private String userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
findViewById(R.id.btn_register).setOnClickListener(this);
findViewById(R.id.tv_login).setOnClickListener(this);
mUserName = (EditText) findViewById(R.id.user_name);
mUserPass = (EditText) findViewById(R.id.user_pass);
mUserPass2 = (EditText) findViewById(R.id.user_pass2);
mUserPresenterIml = new UserPresenterIml(new UserModelIml(), this);
sp = getSharedPreferences("user", MODE_PRIVATE);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_register://点击注册
doRegister();
break;
case R.id.tv_login://已有账号,立即登录
finish();
break;
}
}
//注册
private void doRegister() {
userName = mUserName.getText().toString().trim();
String userPass = mUserPass.getText().toString().trim();
String userPass2 = mUserPass2.getText().toString().trim();
if (TextUtils.isEmpty(userName)) {
toast("请输入您的账号");
return;
}
if (TextUtils.isEmpty(userPass)) {
toast("请输入您的密码");
return;
}
if (TextUtils.isEmpty(userPass2)) {
toast("请确认您的密码");
return;
}
if (!userPass.equals(userPass2)) {
toast("请检查您两次密码是否一致");
return;
}
//走注册
mUserPresenterIml.doRegister(userName, userPass);
}
private void toast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void sueccss(String data) {
UserBean bean = new Gson().fromJson(data, UserBean.class);
toast(bean.getMessage());
sp.edit().putString("user_name", userName).commit();
if (bean.getStatus().equals("0000")) {
finish();
}
}
@Override
public void fail(String error) {
toast(error);
}
@Override
protected void onDestroy() {
super.onDestroy();
mUserPresenterIml.destory();
}
}
ZhanActivity
package com.bw.zhoukao2;
import android.animation.ObjectAnimator;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bw.zhoukao2.fragments.Fragment1;
import com.bw.zhoukao2.fragments.Fragment2;
import com.bw.zhoukao2.fragments.Fragment3;
import com.hjm.bottomtabbar.BottomTabBar;
public class ZhanActivity extends AppCompatActivity {
private BottomTabBar mb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhan);
//初始化组件
mb=(BottomTabBar)findViewById(R.id.bottom_tab_bar);
mb.init(getSupportFragmentManager())
.setImgSize(50,50)
.setFontSize(20)
.setTabPadding(4,6,10)
.setChangeColor(Color.RED,Color.DKGRAY)
.addTabItem("首页",null, Fragment1.class)
.addTabItem("列表",null, Fragment2.class)
.addTabItem("我的",null, Fragment3.class)
.isShowDivider(false)
.setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {
@Override
public void onTabChange(int position, String name) {
}
});
}
}
utils
package com.bw.zhoukao2.mvp.utils;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 14:14
*/
public class OkHttpUtils {
private int HTTP_SUCCESS = 1000;
private int HTTP_FAIL = 1001;
private HttpListener mHttpListener;
public OkHttpUtils get(String url) {
doHttp(url, 0, null);
return this;
}
public OkHttpUtils post(String url, FormBody.Builder bodyBuilder) {
doHttp(url, 1, bodyBuilder);
return this;
}
//网络请求
private void doHttp(String url, int type, FormBody.Builder bodyBuilder) {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
Log.i("intercept", request.url().host());
return chain.proceed(request);
}
}).build();
Request.Builder btilder = new Request.Builder();
if (type == 0) {
btilder.get();
} else {
btilder.post(bodyBuilder.build());
}
btilder.url(url);
Request request = btilder.build();
final Message message = Message.obtain();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
message.obj = e.getMessage();
message.what = HTTP_FAIL;
handler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
message.obj = response.body().string();
message.what = HTTP_SUCCESS;
handler.sendMessage(message);
}
});
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == HTTP_SUCCESS) {
String data = (String) msg.obj;
mHttpListener.success(data);
} else {
String error = (String) msg.obj;
mHttpListener.fail(error);
}
}
};
//传递接口
public void result(HttpListener mHttpListener) {
this.mHttpListener = mHttpListener;
}
public interface HttpListener {
void success(String data);
void fail(String error);
}
}
view
package com.bw.zhoukao2.mvp.view;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 13:44
*/
public interface UserView {
void sueccss(String data);
void fail(String error);
}
wight
package com.bw.zhoukao2.wight;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bw.zhoukao2.R;
import java.util.ArrayList;
import java.util.List;
public class LiuLayoutView extends RelativeLayout {
private LinearLayout mLayoutV;
private LinearLayout mLayoutH;
private Context context;
public LiuLayoutView(Context context) {
super(context);
init(context);
}
public LiuLayoutView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
//初始化视图
private void init(Context context) {
this.context = context;
//第一步,创建垂直的layout
View view = View.inflate(context, R.layout.layout_v, null);
mLayoutV = (LinearLayout) view.findViewById(R.id.layout_all);
addView(view);
}
//传递数据
private List<String> mListContent=new ArrayList<>();
public void setList(List<String> mList) {
mListContent=mList;
mLayoutV.removeAllViews();
int len = 0;
//第二步,创建水平的
mLayoutH = (LinearLayout) View.inflate(context, R.layout.layout_h, null);
mLayoutV.addView(mLayoutH);
for (int a = 0; a < mList.size(); a++) {
String data = mList.get(a);
len += data.length();
if (len > 22) {
mLayoutH = (LinearLayout) View.inflate(context, R.layout.layout_h, null);
mLayoutV.addView(mLayoutH);
len = 0;
}
//第三步,创建展示数据的View
final View mLayoutText = View.inflate(context, R.layout.layout_text, null);
final TextView textView = (TextView) mLayoutText.findViewById(R.id.tv_txt);
textView.setText(mList.get(a));
mLayoutH.addView(mLayoutText);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLayoutText.getLayoutParams();
params.weight = 1;
params.topMargin = 10;
params.rightMargin = 10;
params.leftMargin = 10;
mLayoutText.setLayoutParams(params);
mLayoutText.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
String con=textView.getText().toString();//获取到内容
mListContent.remove(con);
setList(mListContent);
return true;
}
});
}
}
}
package com.bw.zhoukao2.wight;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/21 20:03
*/
public class MyBoView extends View {
Paint pa1;
public MyBoView(Context context) {
super(context);
init(context);
}
public MyBoView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
pa1 = new Paint();
pa1.setColor(Color.WHITE);
pa1.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(100,100,100,pa1);
}
//传递颜色,改变颜色
public void setColor(int color){
pa1.setColor(color);
invalidate();
}
}
model
package com.bw.zhoukao2.mvp.model;
import android.widget.CheckBox;
import javax.security.auth.callback.Callback;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 13:45
*/
public interface UserModel {
interface CallBackListener {
void success(String data);
void fail(String error);
}
//登录
void doLogin(String phone, String pwd, CallBackListener listener);
//注册
void doRegister(String phone, String pwd, CallBackListener listener);
// 商品展示
void getData(String keyword,int page,CallBackListener listener);
}
package com.bw.zhoukao2.mvp.model;
import com.bw.zhoukao2.mvp.utils.OkHttpUtils;
import okhttp3.FormBody;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 13:48
*/
public class UserModelIml implements UserModel {
@Override
public void doLogin(String phone, String pwd, final CallBackListener listener) {
//登录
String registerUrl = "http://172.17.8.100/small/user/v1/login";
FormBody.Builder builder = new FormBody.Builder();
builder.add("phone", phone);
builder.add("pwd", pwd);
new OkHttpUtils().post(registerUrl, builder).result(new OkHttpUtils.HttpListener() {
@Override
public void success(String data) {
listener.success(data);
}
@Override
public void fail(String error) {
listener.fail(error);
}
});
}
@Override
public void doRegister(String phone, String pwd, final CallBackListener listener) {
//注册
String registerUrl = "http://172.17.8.100/small/user/v1/register";
FormBody.Builder builder = new FormBody.Builder();
builder.add("phone", phone);
builder.add("pwd", pwd);
new OkHttpUtils().post(registerUrl, builder).result(new OkHttpUtils.HttpListener() {
@Override
public void success(String data) {
listener.success(data);
}
@Override
public void fail(String error) {
listener.fail(error);
}
});
}
@Override
public void getData(String keyword, int page, final CallBackListener listener) {
String url="http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword="+keyword+"&page="+page+"&count=10";
new OkHttpUtils().get(url).result(new OkHttpUtils.HttpListener() {
@Override
public void success(String data) {
listener.success(data);
}
@Override
public void fail(String error) {
listener.fail(error);
}
});
}
}
presenter
package com.bw.zhoukao2.mvp.presenter;
import javax.security.auth.callback.Callback;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 13:49
*/
public interface UserPresenter {
//登录
void doLogin(String phone, String pwd);
//注册
void doRegister(String phone, String pwd);
void doData(String keyWord,int page);
}
package com.bw.zhoukao2.mvp.presenter;
import com.bw.zhoukao2.mvp.model.UserModel;
import com.bw.zhoukao2.mvp.view.UserView;
import javax.security.auth.callback.Callback;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 13:50
*/
public class UserPresenterIml implements UserPresenter, UserModel.CallBackListener {
UserModel userModel;
UserView userView;
public UserPresenterIml(UserModel userModel,UserView userView){
this.userModel=userModel;
this.userView=userView;
}
@Override
public void doLogin(String phone, String pwd) {
userModel.doLogin(phone,pwd,this);
}
@Override
public void doRegister(String phone, String pwd) {
userModel.doRegister(phone,pwd,this);
}
@Override
public void doData(String keyWord, int page) {
userModel.getData(keyWord,page,this);
}
@Override
public void success(String data) {
userView.sueccss(data);
}
@Override
public void fail(String error) {
userView.fail(error);
}
public void destory(){
if (userModel!=null){
userModel=null;
}
if (userView!=null){
userView=null;
}
System.gc();
}
}
bean
package com.bw.zhoukao2.mvp.bean;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 14:33
*/
public class LoginBean {
/**
* message : 登录成功
* result : {"headPic":"http://172.17.8.100/images/small/head_pic/2018-11-21/20181121100733.jpg","nickName":"OP_8mY65","phone":"16619958760","sessionId":"154276714558512","sex":1,"userId":12}
* status : 0000
*/
private String message;
private ResultBean result;
private String status;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public static class ResultBean {
/**
* headPic : http://172.17.8.100/images/small/head_pic/2018-11-21/20181121100733.jpg
* nickName : OP_8mY65
* phone : 16619958760
* sessionId : 154276714558512
* sex : 1
* userId : 12
*/
private String headPic;
private String nickName;
private String phone;
private String sessionId;
private int sex;
private int userId;
public String getHeadPic() {
return headPic;
}
public void setHeadPic(String headPic) {
this.headPic = headPic;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
}
package com.bw.zhoukao2.mvp.bean;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 14:35
*/
public class UserBean {
private String message;
private String status;
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;
}
}
package com.bw.zhoukao2.mvp.bean;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/19 09:02
*/
public class ShopBean {
/**
* message : 查询成功
* result : [{"commodityId":98,"commodityName":"秋新款印花图案宽松女款连帽卫衣","masterPic":"http://172.17.8.100/images/small/commodity/nz/wy/6/1.jpg","price":99,"saleNum":0},{"commodityId":95,"commodityName":"秋冬加绒拼接色 不规则套头 假两件休闲短款保暖女装卫衣上衣","masterPic":"http://172.17.8.100/images/small/commodity/nz/wy/3/1.jpg","price":99,"saleNum":0},{"commodityId":97,"commodityName":"秋季新款韩版女装卫衣秋冬装春秋休闲宽松字母套头连帽加绒长袖卫衣","masterPic":"http://172.17.8.100/images/small/commodity/nz/wy/5/1.jpg","price":139,"saleNum":0},{"commodityId":94,"commodityName":"秋冬新款宽松套头绣花纯色连帽加绒加厚卫衣女装外套","masterPic":"http://172.17.8.100/images/small/commodity/nz/wy/2/1.jpg","price":129,"saleNum":0},{"commodityId":99,"commodityName":"字母绣花宽松女款卫衣","masterPic":"http://172.17.8.100/images/small/commodity/nz/wy/7/1.jpg","price":179,"saleNum":0},{"commodityId":96,"commodityName":"秋季新款韩版女装卫衣秋装学院风街头刺绣基础百搭连帽套头宽松女款卫衣","masterPic":"http://172.17.8.100/images/small/commodity/nz/wy/4/1.jpg","price":129,"saleNum":0},{"commodityId":93,"commodityName":"秋季新款韩版女装秋装甜美通勤字母印花宽松长袖女款卫衣女","masterPic":"http://172.17.8.100/images/small/commodity/nz/wy/1/1.jpg","price":94,"saleNum":0}]
* status : 0000
*/
private String message;
private String status;
private List<ResultBean> result;
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<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
/**
* commodityId : 98
* commodityName : 秋新款印花图案宽松女款连帽卫衣
* masterPic : http://172.17.8.100/images/small/commodity/nz/wy/6/1.jpg
* price : 99
* saleNum : 0
*/
private int commodityId;
private String commodityName;
private String masterPic;
private int price;
private int saleNum;
public int getCommodityId() {
return commodityId;
}
public void setCommodityId(int 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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getSaleNum() {
return saleNum;
}
public void setSaleNum(int saleNum) {
this.saleNum = saleNum;
}
}
}
fragments
package com.bw.zhoukao2.fragments;
import android.inputmethodservice.Keyboard;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.bw.zhoukao2.R;
import com.bw.zhoukao2.mvp.adapter.ShopAdapter;
import com.bw.zhoukao2.mvp.bean.ShopBean;
import com.bw.zhoukao2.mvp.model.UserModel;
import com.bw.zhoukao2.mvp.model.UserModelIml;
import com.bw.zhoukao2.mvp.presenter.UserPresenterIml;
import com.bw.zhoukao2.mvp.view.UserView;
import com.bw.zhoukao2.wight.LiuLayoutView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 20:32
*/
public class Fragment1 extends Fragment implements UserView {
private LiuLayoutView mLiuLayoutView;
private EditText mEdText;
private ShopAdapter shopAdapter;
UserPresenterIml userPresenterIml;
private List<String> mList = new ArrayList<>();
private Button btn_seach;
private RecyclerView recyclerview;
int page=1;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
mEdText = (EditText) view.findViewById(R.id.ed_text);
mLiuLayoutView = (LiuLayoutView) view.findViewById(R.id.layout_view);
btn_seach=(Button) view. findViewById(R.id.btn_seach);
recyclerview= (RecyclerView)view.findViewById(R.id.recyclerview);
userPresenterIml=new UserPresenterIml(new UserModelIml(),this);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btn_seach.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = mEdText.getText().toString().trim();
if (!TextUtils.isEmpty(content)) {
if (mList.contains(content)) {
mList.remove(content);
}
mList.add(0, content);
mLiuLayoutView.setList(mList);
}
}
});
//设置适配器
shopAdapter= new ShopAdapter(getActivity());
//添加布局管理
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerview.setLayoutManager(gridLayoutManager);
recyclerview.setAdapter( shopAdapter);
userPresenterIml.doData("手机",page);
}
@Override
public void sueccss(String data) {
ShopBean bean = new Gson().fromJson(data, ShopBean.class);
shopAdapter.setData(bean.getResult());
}
@Override
public void fail(String error) {
userPresenterIml.fail(error);
}
}
package com.bw.zhoukao2.fragments;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bw.zhoukao2.R;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 20:32
*/
public class Fragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2, container, false);
return view;
}
}
package com.bw.zhoukao2.fragments;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bw.zhoukao2.R;
/**
* 作者:$yangxiangrong
* <p>
* 2019/3/22 20:32
*/
public class Fragment3 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment3, container, false);
return view;
}
}
adapter
package com.bw.zhoukao2.mvp.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bw.zhoukao2.R;
import com.bw.zhoukao2.mvp.bean.ShopBean;
import java.util.ArrayList;
import java.util.List;
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyVh> {
private Context context;
private List<ShopBean.ResultBean> data=new ArrayList<>();
public ShopAdapter(Context context){
this.context=context;
}
@NonNull
@Override
public MyVh onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=View.inflate(context,R.layout.shop_item,null);
MyVh myVh=new MyVh(view);
return myVh;
}
@Override
public void onBindViewHolder(@NonNull MyVh myVh, int i) {
myVh.title_id.setText(data.get(i).getCommodityName());
myVh.title_price.setText("¥"+data.get(i).getPrice()+"");
myVh. title_num.setText("已售" +data.get(i).getSaleNum()+ "件");
Glide.with(context).load(data.get(i).getMasterPic()).into(myVh.imag);
}
@Override
public int getItemCount() {
return data.size();
}
//传递数据d的方法
public void setData(List<ShopBean.ResultBean> data) {
this.data = data;
notifyDataSetChanged();
}
public class MyVh extends RecyclerView.ViewHolder {
ImageView imag;
TextView title_id,title_price,title_num;
public MyVh(@NonNull View itemView) {
super(itemView);
imag=itemView.findViewById(R.id.image);
title_id=itemView.findViewById(R.id.title_id);
title_price=itemView.findViewById(R.id.title_price);
title_num=itemView.findViewById(R.id.title_num);
}
}
}