//activity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwie.test.dierzhou.LoginActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:layout_centerInParent="true"/> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#000000"/> <EditText android:layout_marginTop="50dp" android:id="@+id/login_mobile" android:layout_width="300dp" android:layout_height="50dp" android:singleLine="true" android:layout_gravity="center_horizontal"/> <EditText android:layout_marginTop="50dp" android:id="@+id/login_password" android:layout_width="300dp" android:layout_height="50dp" android:singleLine="true" android:layout_gravity="center_horizontal" android:password="true"/> <LinearLayout android:layout_marginTop="50dp" android:layout_width="300dp" android:layout_height="50dp" android:layout_gravity="center_horizontal"> <RelativeLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:layout_centerInParent="true"/> </RelativeLayout> <RelativeLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> <Button android:id="@+id/reg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:layout_centerInParent="true"/> </RelativeLayout> </LinearLayout> </LinearLayout>
//activity_reg
<?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" tools:context="com.bwie.test.dierzhou.LoginActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:id="@+id/icon_back" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/icon_back" android:layout_centerVertical="true" android:layout_marginLeft="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:layout_centerInParent="true"/> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#000000"/> <EditText android:layout_marginTop="50dp" android:id="@+id/reg_mobile" android:layout_width="300dp" android:layout_height="50dp" android:singleLine="true" android:layout_gravity="center_horizontal"/> <EditText android:layout_marginTop="50dp" android:id="@+id/reg_password" android:layout_width="300dp" android:layout_height="50dp" android:singleLine="true" android:layout_gravity="center_horizontal" android:password="true"/> <Button android:id="@+id/reg" android:layout_marginTop="50dp" android:layout_width="300dp" android:layout_height="50dp" android:text="立即注册" android:gravity="center" android:layout_gravity="center_horizontal"/> </LinearLayout>
//activity_good_list
<?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" tools:context="com.bwie.test.dierzhou.GoodListActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:id="@+id/icon_back" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/icon_back" android:layout_centerVertical="true" android:layout_marginLeft="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商品列表" android:layout_centerInParent="true"/> </RelativeLayout> <com.androidkun.PullToRefreshRecyclerView android:id="@+id/refresh_recy" android:layout_width="match_parent" android:layout_height="match_parent"> </com.androidkun.PullToRefreshRecyclerView> </LinearLayout>
//recy_item_layout
<?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="120dp"> <ImageView android:id="@+id/image" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_vertical"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:gravity="center_vertical"/> <TextView android:id="@+id/price" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_vertical"/> <TextView android:id="@+id/bargainPrice" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center_vertical"/> </LinearLayout> </LinearLayout>
//LoginActivity主界面
public class LoginActivity extends AppCompatActivity implements LoginPreCallBack { private EditText mobileEdit; private EditText passwordEdit; private Button loginBtn; private Button regBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final LoginPresenter presenter = new LoginPresenter(this); mobileEdit = (EditText) findViewById(R.id.login_mobile); passwordEdit = (EditText) findViewById(R.id.login_password); loginBtn = (Button) findViewById(R.id.login); regBtn = (Button) findViewById(R.id.reg); loginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String mobile = mobileEdit.getText().toString(); String password = passwordEdit.getText().toString(); Map<String, String> params = new HashMap<>(); params.put("mobile", mobile); params.put("password", password); presenter.postLoginData(Api.LOGIN_URL, params); } }); regBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(LoginActivity.this, RegActivity.class); startActivity(intent); } }); } @Override public void onLoginPreResponseSuccess(final LoginBean loginBean) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(LoginActivity.this, loginBean.getMsg(), Toast.LENGTH_SHORT).show(); if("0".equals(loginBean.getCode())){ Intent intent = new Intent(LoginActivity.this, GoodListActivity.class); startActivity(intent); } } }); } @Override public void onLoginPreResponseError() { } }
//RegActivity注册界面
public class RegActivity extends AppCompatActivity implements RegPreCallBack { private EditText mobileEdit; private EditText passwordEdit; private ImageView backImage; private Button regBtn; private RegPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reg); presenter = new RegPresenter(this); backImage = (ImageView) findViewById(R.id.icon_back); mobileEdit = (EditText) findViewById(R.id.reg_mobile); passwordEdit = (EditText) findViewById(R.id.reg_password); regBtn = (Button) findViewById(R.id.reg); backImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); regBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String mobile = mobileEdit.getText().toString(); String password = passwordEdit.getText().toString(); Map<String, String> params = new HashMap<>(); params.put("mobile", mobile); params.put("password", password); presenter.postRegData(Api.REG_URL, params); } }); } @Override public void onRegPreResponseSuccess(final RegBean regBean) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(RegActivity.this, regBean.getMsg(), Toast.LENGTH_SHORT).show(); finish(); if("0".equals(regBean.getCode())){ finish(); } } }); } @Override public void onRegPreResponseError() { } }
//GoodListActivity
public class GoodListActivity extends AppCompatActivity implements GoodPreCallBack, PullToRefreshListener { private GoodPresenter presenter; private PullToRefreshRecyclerView recyclerView; private int pageNum = 1; private RecyAdapter adapter; private List<GoodBean.DataBean> dataBeans = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_good_list); presenter = new GoodPresenter(this); findViewById(R.id.icon_back).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); recyclerView = (PullToRefreshRecyclerView) findViewById(R.id.refresh_recy); Map<String, String> params = new HashMap<>(); params.put("pscid", "39"); params.put("page", String.valueOf(pageNum)); presenter.postGoodData(Api.GOOD_LIST, params); recyclerView.setLayoutManager(new LinearLayoutManager(GoodListActivity.this, LinearLayoutManager.VERTICAL, false)); //是否开启下拉刷新功能 recyclerView.setPullRefreshEnabled(true); //是否开启上拉加载功能 recyclerView.setLoadingMoreEnabled(true); //设置是否显示上次刷新的时间 recyclerView.displayLastRefreshTime(true); //设置刷新回调 recyclerView.setPullToRefreshListener(this); } @Override public void onGoodPreResponseSuccess(final GoodBean goodBean) { runOnUiThread(new Runnable() { @Override public void run() { //Toast.makeText(GoodListActivity.this, goodBean.getMsg(), Toast.LENGTH_SHORT).show(); List<GoodBean.DataBean> list = goodBean.getData(); if("1".equals(goodBean.getPage())){ dataBeans.addAll(0, list); recyclerView.setRefreshComplete(); }else{ dataBeans.addAll(list); recyclerView.setLoadMoreComplete(); } setAdapter(); } }); } private void setAdapter() { if(adapter == null){ adapter = new RecyAdapter(GoodListActivity.this, dataBeans); recyclerView.setAdapter(adapter); }else{ adapter.notifyDataSetChanged(); } } @Override public void onGoodPreResponseError() { } @Override public void onRefresh() { Map<String, String> params = new HashMap<>(); params.put("pscid", "39"); params.put("page", "1"); presenter.postGoodData(Api.GOOD_LIST, params); } @Override public void onLoadMore() { pageNum++; Map<String, String> params = new HashMap<>(); params.put("pscid", "39"); params.put("page", String.valueOf(pageNum)); presenter.postGoodData(Api.GOOD_LIST, params); } }
//RecyAdapter
public class RecyAdapter extends RecyclerView.Adapter<RecyItemViewHolder>{ private Context context; private List<GoodBean.DataBean> dataBeans; public RecyAdapter(Context context, List<GoodBean.DataBean> dataBeans) { this.context = context; this.dataBeans = dataBeans; } @Override public RecyItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(context).inflate(R.layout.recy_item_layout, parent, false); RecyItemViewHolder holder = new RecyItemViewHolder(inflate); return holder; } @Override public void onBindViewHolder(RecyItemViewHolder holder, int position) { Glide.with(context) .load(dataBeans.get(position).getImages().split("\\|")[0]) .placeholder(R.mipmap.ic_launcher) .error(R.mipmap.ic_launcher) .into(holder.imageView); holder.title.setText(dataBeans.get(position).getTitle()); holder.price.setText("原价:¥"+dataBeans.get(position).getPrice()); holder.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); holder.bargainPrice.setText("优惠价:¥"+dataBeans.get(position).getBargainPrice()); holder.bargainPrice.setTextColor(Color.RED); } @Override public int getItemCount() { return dataBeans.size(); } }
holder文件夹下//RecyItemViewHolder
public class RecyItemViewHolder extends RecyclerView.ViewHolder{ public TextView title; public TextView price; public TextView bargainPrice; public ImageView imageView; public RecyItemViewHolder(View itemView) { super(itemView); imageView = itemView.findViewById(R.id.image); title = itemView.findViewById(R.id.title); price = itemView.findViewById(R.id.price); bargainPrice = itemView.findViewById(R.id.bargainPrice); } }
model文件夹下//GoodModel
public class GoodModel { private GoodModelCallBack callBack; public GoodModel(GoodModelCallBack callBack) { this.callBack = callBack; } public void postGoodData(String url, Map<String, String> params){ OkHttpUtil.post(url, params, new Callback() { @Override public void onFailure(Call call, IOException e) { callBack.onGoodModelResponseError(); } @Override public void onResponse(Call call, Response response) throws IOException { Gson gson = new Gson(); GoodBean goodBean = gson.fromJson(response.body().string(), GoodBean.class); callBack.onGoodModelResponseSuccess(goodBean); } }); } }
//LoginModel
public class LoginModel { private LoginModelCallBack callBack; public LoginModel(LoginModelCallBack callBack) { this.callBack = callBack; } public void postLoginData(String url, Map<String, String> params){ OkHttpUtil.post(url, params, new Callback() { @Override public void onFailure(Call call, IOException e) { callBack.onLoginModelResponseError(); } @Override public void onResponse(Call call, Response response) throws IOException { Gson gson = new Gson(); LoginBean loginBean = gson.fromJson(response.body().string(), LoginBean.class); callBack.onLoginModelResponseSuccess(loginBean); } }); } }
//RegModel
public class RegModel { private RegModelCallBack callBack; public RegModel(RegModelCallBack callBack) { this.callBack = callBack; } public void postRegData(String url, Map<String, String> params){ OkHttpUtil.post(url, params, new Callback() { @Override public void onFailure(Call call, IOException e) { callBack.onRegModelResponseError(); } @Override public void onResponse(Call call, Response response) throws IOException { Gson gson = new Gson(); RegBean regBean = gson.fromJson(response.body().string(), RegBean.class); callBack.onRegModelResponseSuccess(regBean); } }); } }
model接口//GoodModelCallBack
public interface GoodModelCallBack { void onGoodModelResponseSuccess(GoodBean goodBean); void onGoodModelResponseError(); }
//LoginModelCallBack
public interface LoginModelCallBack { void onLoginModelResponseSuccess(LoginBean loginBean); void onLoginModelResponseError(); }
//RegModelCallBack
public interface RegModelCallBack { void onRegModelResponseSuccess(RegBean regBean); void onRegModelResponseError(); }
presenter文件夹下//GoodPresenter
public class GoodPresenter implements GoodModelCallBack { private GoodPreCallBack goodPreCallBack; private GoodModel goodModel; public GoodPresenter(GoodPreCallBack goodPreCallBack) { this.goodPreCallBack = goodPreCallBack; this.goodModel = new GoodModel(this); } public void postGoodData(String url, Map<String, String> params){ goodModel.postGoodData(url, params); } @Override public void onGoodModelResponseSuccess(GoodBean goodBean) { goodPreCallBack.onGoodPreResponseSuccess(goodBean); } @Override public void onGoodModelResponseError() { goodPreCallBack.onGoodPreResponseError(); } }
//LoginPresenter
public class LoginPresenter implements LoginModelCallBack { private LoginPreCallBack loginPreCallBack; private LoginModel loginModel; public LoginPresenter(LoginPreCallBack loginPreCallBack) { this.loginPreCallBack = loginPreCallBack; this.loginModel = new LoginModel(this); } public void postLoginData(String url, Map<String, String> params){ loginModel.postLoginData(url, params); } @Override public void onLoginModelResponseSuccess(LoginBean loginBean) { loginPreCallBack.onLoginPreResponseSuccess(loginBean); } @Override public void onLoginModelResponseError() { loginPreCallBack.onLoginPreResponseError(); } }
//RegPresenter
public class RegPresenter implements RegModelCallBack { private RegPreCallBack regPreCallBack; private RegModel regModel; public RegPresenter(RegPreCallBack regPreCallBack) { this.regPreCallBack = regPreCallBack; this.regModel = new RegModel(this); } public void postRegData(String url, Map<String, String> params){ regModel.postRegData(url, params); } @Override public void onRegModelResponseSuccess(RegBean regBean) { regPreCallBack.onRegPreResponseSuccess(regBean); } @Override public void onRegModelResponseError() { regPreCallBack.onRegPreResponseError(); } }
presenter接口//GoodPreCallBack
public interface GoodPreCallBack { void onGoodPreResponseSuccess(GoodBean goodBean); void onGoodPreResponseError(); }
//LoginPreCallBack
public interface LoginPreCallBack { void onLoginPreResponseSuccess(LoginBean loginBean); void onLoginPreResponseError(); }
//RegPreCallBack
public interface RegPreCallBack { void onRegPreResponseSuccess(RegBean regBean); void onRegPreResponseError(); }
//Api
public class Api { public static final String LOGIN_URL = "http://120.27.23.105/user/login"; public static final String REG_URL = "http://120.27.23.105/user/reg"; public static final String GOOD_LIST = "http://120.27.23.105/product/getProducts"; }
//添加依赖
minSdkVersion 17
compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.squareup.okhttp3:okhttp:3.6.0' compile 'com.squareup.okio:okio:1.11.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.androidkun:pulltorefreshrecyclerview:1.0.9' compile 'com.android.support.test:runner:0.5' compile 'com.android.support.test.espresso:espresso-core:2.2.2'
//添加权限
<uses-permission android:name="android.permission.INTERNET"/> <application <activity android:name=".LoginActivity"> </activity> <activity android:name=".GoodListActivity"/> <activity android:name=".RegActivity"/>
//GoodBean
public class GoodBean {
private String msg; private String code; private String page; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } }
//LoginBean
public class LoginBean { private String msg; private String code; private DataBean data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class DataBean { private Object age; private String appkey; private String appsecret; private String createtime; private Object email; private Object fans; private Object follow; private Object gender; private Object icon; private Object latitude; private Object longitude; private String mobile; private Object money; private Object nickname; private String password; private Object praiseNum; private String token; private int uid; private Object userId; private String username; public Object getAge() { return age; } public void setAge(Object age) { this.age = age; } public String getAppkey() { return appkey; } public void setAppkey(String appkey) { this.appkey = appkey; } public String getAppsecret() { return appsecret; } public void setAppsecret(String appsecret) { this.appsecret = appsecret; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public Object getEmail() { return email; } public void setEmail(Object email) { this.email = email; } public Object getFans() { return fans; } public void setFans(Object fans) { this.fans = fans; } public Object getFollow() { return follow; } public void setFollow(Object follow) { this.follow = follow; } public Object getGender() { return gender; } public void setGender(Object gender) { this.gender = gender; } public Object getIcon() { return icon; } public void setIcon(Object icon) { this.icon = icon; } public Object getLatitude() { return latitude; } public void setLatitude(Object latitude) { this.latitude = latitude; } public Object getLongitude() { return longitude; } public void setLongitude(Object longitude) { this.longitude = longitude; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public Object getMoney() { return money; } public void setMoney(Object money) { this.money = money; } public Object getNickname() { return nickname; } public void setNickname(Object nickname) { this.nickname = nickname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Object getPraiseNum() { return praiseNum; } public void setPraiseNum(Object praiseNum) { this.praiseNum = praiseNum; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public Object getUserId() { return userId; } public void setUserId(Object userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } }
//RegBean
public class RegBean { private String msg; private String code; private String data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getData() { return data; } public void setData(String data) { this.data = data; } }