implementation ‘com.squareup.okhttp3:okhttp:3.7.0’
implementation ‘com.github.bumptech.glide:glide:4.9.0’
implementation ‘com.google.code.gson:gson:2.8.5’
implementation ‘com.android.support:recyclerview-v7:28.0.0’
implementation ‘com.jcodecraeer:xrecyclerview:1.2.0’
1.MyView
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.lzb.dome5.coustm.MyView
android:id="@+id/myView_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
2.public class MainActivity extends AppCompatActivity {
MyView myView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView (){
myView = findViewById(R.id.myView_id);
//通WindowManager 获取到屏幕的款和高
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
float width = wm.getDefaultDisplay().getWidth();
float height = wm.getDefaultDisplay().getHeight();
ObjectAnimator animatorx = ObjectAnimator.ofFloat(myView , "translationX" , 0 , width -200);
ObjectAnimator animatory = ObjectAnimator.ofFloat(myView , "translationY" , 0 , height-200);
animatory.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
//动画执行结束直接跳转下一个界面
Intent intent = new Intent(MainActivity.this , LoginActivity.class);
startActivity(intent);
finish();
}
});
//组合动画执行
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(animatorx).with(animatory);
animatorSet.setDuration(5000);
// animatorSet.start();
}
}
3.MyView
public class MyView extends View {
Paint paint ;
public MyView(Context context) {
super(context);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//实例化画笔
paint = new Paint();
//设置画笔颜色
paint.setColor(Color.RED);
//抗锯齿
paint.setAntiAlias(true);
//填充模式
paint.setStyle(Paint.Style.FILL);
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(100 , 100 , 100 , paint);
}
}
4.登陆
public class LoginActivity extends AppCompatActivity implements View.OnClickListener,ContartIntface.LoginViewIntface{
//生命p层的接口对象
ContartIntface.PersentIntface persentIntface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
persentIntface = new Mypersenter(this);
init();
}
public void init(){
phone = findViewById(R.id.phone_id);
pwd = findViewById(R.id.pwd_id);
logBut = findViewById(R.id.login_id);
regBut = findViewById(R.id.regist_id);
qq = findViewById(R.id.qq_id);
qq.setOnClickListener(this);
logBut.setOnClickListener(this);
regBut.setOnClickListener(this);
if(Build.VERSION.SDK_INT>=23){
String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
ActivityCompat.requestPermissions(this,mPermissionList,123);
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.login_id:
String phoness = phone.getText().toString();
String pwdss = pwd.getText().toString();
persentIntface.pLogin(phoness , pwdss);
break;
case R.id.regist_id:
String phones = phone.getText().toString();
String pwds = pwd.getText().toString();
persentIntface.pRegist(phones , pwds);
break;
case R.id.qq_id:
UMShareAPI umShareAPI = UMShareAPI.get(this);
umShareAPI.getPlatformInfo(this, SHARE_MEDIA.QQ, new UMAuthListener() {
@Override
public void onStart(SHARE_MEDIA share_media) {
//开始登陆
}
@Override
public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
//登陆成功
Toast.makeText(LoginActivity.this , "qq登陆成功" , Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this , ProductActivity.class);
startActivity(intent);
}
@Override
public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
}
@Override
public void onCancel(SHARE_MEDIA share_media, int i) {
}
});
break;
}
}
@Override
public void showReg(String str) {
Toast.makeText(this , str , Toast.LENGTH_LONG).show();
}
@Override
public void showLog(String str) {
Toast.makeText(this , str , Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this , ProductActivity.class);
startActivity(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
}
}
5.P层
public class Mypersenter<T> implements ContartIntface.PersentIntface {
//生命m层的对象
MyModel myModel;
T tt;
public Mypersenter(T t){
myModel = new MyModel();
tt = t;
}
@Override
public void pLogin(String phone, String pwd) {
//先实例化接口
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object o) {
ContartIntface.LoginViewIntface loginViewIntface = (ContartIntface.LoginViewIntface) tt;
loginViewIntface.showLog((String) o);
}
@Override
public void error(String str) {
}
});
//开始去请求
myModel.postRequset("http://172.17.8.100/small/user/v1/login" , phone , pwd);
}
@Override
public void pRegist(String phone, String pwd) {
//先实例化接口
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object o) {
ContartIntface.LoginViewIntface loginViewIntface = (ContartIntface.LoginViewIntface) tt;
loginViewIntface.showReg((String) o);
}
@Override
public void error(String str) {
}
});
//开始去请求
myModel.postRequset("http://172.17.8.100/small/user/v1/register" , phone , pwd);
}
@Override
public void pToRequest(int page ) {
String url = "http://172.17.8.100/small/commodity/v1/findCommodityByKeyword";
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object o) {
Bean bean = (Bean) o;
List<Bean.ResultBean> list = bean.getResult();
ContartIntface.ProductIntface productIntface = (ContartIntface.ProductIntface) tt;
productIntface.showProuduct(list);
}
@Override
public void error(String str) {
}
});
Map<String , String > map = new HashMap<>();
map.put("keyword" , "高");
map.put("page" , page + "");
map.put("count" , "10");
myModel.getRequset(url , map);
}
@Override
public void setContPrames(String cont , int page) {
String url = "http://172.17.8.100/small/commodity/v1/findCommodityByKeyword";
Map<String , String > map = new HashMap<>();
map.put("keyword" , cont);
map.put("page" , page + "");
map.put("count" , "10");
myModel.setMyCallback(new MyModel.MyCallback() {
@Override
public void success(Object o) {
Bean bean = (Bean) o;
List<Bean.ResultBean> list = bean.getResult();
ContartIntface.ProductIntface productIntface = (ContartIntface.ProductIntface) tt;
productIntface.showSechProduct(list);
}
@Override
public void error(String str) {
}
});
myModel.getRequset(url , map);
}
}
6.MyModel
public class MyModel {
MyCallback myCallback;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int type = msg.arg1;
if(type == 1){
String json = (String) msg.obj;
Gson gson = new Gson();
Bean bean = gson.fromJson(json , Bean.class);
myCallback.success(bean);
}else {
String josn = (String) msg.obj;
try {
JSONObject object = new JSONObject(josn);
String m = object.getString("message");
myCallback.success(m);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
public void postRequset(String url , String phone , String pwd){
OkhttpUtile utile = OkhttpUtile.getInstance();
utile.doPost(url, phone, pwd, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = new Message();
message.obj = json;
handler.sendMessage(message);
}
});
}
public void getRequset(String url , Map<String , String> map){
OkhttpUtile utile = OkhttpUtile.getInstance();
utile.doGet(url, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = new Message();
message.obj = json;
message.arg1 = 1;
handler.sendMessage(message);
}
});
}
public void setMyCallback(MyCallback back){
myCallback = back;
}
public interface MyCallback{
public void success(Object o);
public void error(String str);
}
}
7.OkHttpUtile
public class OkhttpUtile {
public static OkhttpUtile utile;
OkHttpClient okHttpClient;
//构造方法私有化
private OkhttpUtile(){
okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.e("tag" ,"url = " + request.url());
Response response = chain.proceed(request);
return response;
}
}).build();
}
public static synchronized OkhttpUtile getInstance(){
if(utile == null){
utile = new OkhttpUtile();
}
return utile;
}
//post
public void doPost(String url , String phone , String pwd , Callback callback){
//FormBody.Builde用来封装参数
FormBody.Builder builder = new FormBody.Builder();
builder.add("phone" , phone);
builder.add("pwd" ,pwd);
//通过FormBody.Builder获取到RequestBody对象
RequestBody body = builder.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
public void doGet(String url , Map<String , String> map , Callback callback){
if(map != null && map.size() > 0){
StringBuilder stringBuilder = new StringBuilder();
for(String key : map.keySet()){
String value = map.get(key);
stringBuilder.append(key)
.append("=")
.append(value)
.append("&");
}
String pstr = stringBuilder.toString();
int index = pstr.lastIndexOf("&");
pstr = pstr.substring(0 , index);
url = url+ "?" + pstr;
}
Request request = new Request.Builder()
.url(url)
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
}
8.public interface ContartIntface {
//view login
public interface LoginViewIntface{
//注册的刷新方法
public void showReg(String str);
//登陆的刷新方法
public void showLog(String str);
}
public interface ProductIntface{
//展示列表
public void showProuduct(Object o);
//展示搜索内容
public void showSechProduct(Object o);
}
//p层的接口对象
public interface PersentIntface{
public void pLogin(String phone , String pwd);
public void pRegist(String phone , String pwd);
public void pToRequest(int page);
public void setContPrames(String cont,int page);
}
}
9.Adapter
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.Hold>{
Context mContext;
List<Bean.ResultBean> list;
public ProductAdapter(Context mContext ,List<Bean.ResultBean> list ){
this.mContext = mContext;
this.list = list;
}
@NonNull
@Override
public Hold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext) .inflate(R.layout.adpter_layout , null);
return new Hold(view);
}
@Override
public void onBindViewHolder(@NonNull Hold hold, int i) {
String url = list.get(i).getMasterPic();
String title = list.get(i).getCommodityName();
hold.textView.setText(title);
Glide.with(mContext).load(url).into(hold.imageView);
}
@Override
public int getItemCount() {
if(list != null){
return list.size();
}
return 0;
}
public class Hold extends RecyclerView.ViewHolder{
public ImageView imageView;
public TextView textView;
public Hold(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imag_id);
textView = itemView.findViewById(R.id.titi_id);
}
}
}