开篇还是一如既往的强调重点.
导依赖,导依赖,导依赖.网络权限,网络权限,网络权限.
1.MVP框架搭建
Model层实现类:
public class MainModelIml implements MainModel {
@Override
public void doCommodity(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() {
listener.fail();
}
});
}
//获取商品详情
@Override
public void doShopDetail(String id, final CallBackListener listener) {
String url = "http://172.17.8.100/small/commodity/v1/findCommodityDetailsById?commodityId="+id;
new OkHttpUtils().get(url).result(new OkHttpUtils.HttpListener() {
@Override
public void success(String data) {
listener.success(data);
}
@Override
public void fail() {
listener.fail();
}
});
}
}
2.OkHttp网络请求工具类的封装
public class OkHttpUtils {
private int HTTP_SUCCESS=1000;
private int HTTP_FAIL=1001;
private HttpListener mHttpListener;
public OkHttpUtils(HttpListener httpListener) {
}
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 i,FormBody.Builder bodyBuilder) {
OkHttpClient client = new OkHttpClient();
Request.Builder builder = new Request.Builder();
builder.url(url);
if (i == 0) {//Get请求
builder.get();
} else {
RequestBody body=bodyBuilder.build();
builder.post(body);
}
Request request = builder.build();
final Message message=Message.obtain();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
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{
mHttpListener.fail();
}
}
};
//通过方法传递接口
public void result(HttpListener mHttpListener){
this.mHttpListener=mHttpListener;
}
public interface HttpListener{
void success(String data);
void fail();
}
}
3.Adapter
public class CommodityAdapter extends RecyclerView.Adapter<CommodityAdapter.MyViewHolder> {
private Context context;
private List<JsonBean.ResultBean> list = new ArrayList<>();
public CommodityAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public CommodityAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.recycler_item, null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull CommodityAdapter.MyViewHolder myViewHolder, final int i) {
myViewHolder.mTitle.setText(list.get(i).getCommodityName());
myViewHolder.mPrice.setText("¥"+list.get(i).getPrice() + "");
myViewHolder.mNum.setText("已售" + list.get(i).getSaleNum() + "件");
Glide.with(context).load(list.get(i).getMasterPic()).into(myViewHolder.mImage);
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(context, CommodityDetailsActivity.class);
intent.putExtra("id",list.get(i).getCommodityId());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
//传递数据
public void setList(List<JsonBean.ResultBean> list) {
this.list = list;
notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView mTitle, mPrice, mNum;
ImageView mImage;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (ImageView) itemView.findViewById(R.id.image);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mPrice = (TextView) itemView.findViewById(R.id.tv_price);
mNum = (TextView) itemView.findViewById(R.id.tv_num);
}
}
}
4.MainActivity
public class MainActivity extends AppCompatActivity implements MainView {
private XRecyclerView mXRecyclerView;
private EditText mEdSeach;
private MainPresenterIml mMainPresenterIml;
private int page = 1;
private CommodityAdapter mCommodityAdapter;
private RelativeLayout mLayoutShopNull;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayoutShopNull = (RelativeLayout) findViewById(R.id.layout_shop_null);
mEdSeach = (EditText) findViewById(R.id.ed_seach);
mXRecyclerView = (XRecyclerView) findViewById(R.id.xrecyclerview);
mCommodityAdapter = new CommodityAdapter(this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
mXRecyclerView.setLayoutManager(gridLayoutManager);
mXRecyclerView.setAdapter(mCommodityAdapter);
mMainPresenterIml = new MainPresenterIml(new MainModelIml(), this);
findViewById(R.id.btn_seach).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//搜索商品
seach();
}
});
//默认值
mMainPresenterIml.doCommodity("手机", page);
}
//搜索商品 手机
private void seach() {
String content = mEdSeach.getText().toString().trim();
if (TextUtils.isEmpty(content)) {
Toast.makeText(this, "请您输入要搜索的商品", Toast.LENGTH_LONG).show();
return;
}
mMainPresenterIml.doCommodity(content, page);
}
@Override
public void success(String json) {
//获取数据,封装
JsonBean bean = new Gson().fromJson(json, JsonBean.class);
if (bean.getResult().size() == 0) {
mLayoutShopNull.setVisibility(View.VISIBLE);
} else {
mLayoutShopNull.setVisibility(View.GONE);
mCommodityAdapter.setList(bean.getResult());
}
}
@Override
public void fail() {
}
}
5.展示数据页面
public class CommodityDetailsActivity extends AppCompatActivity implements MainView {
private ViewPager mImage;
private TextView mPrice, mNum, mTitle;
private String[] picTrueArr;
private ImagePageAdapter mImagePageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_commodity_details);
int id = getIntent().getIntExtra("id", 103);
MainPresenterIml mainPresenterIml = new MainPresenterIml(new MainModelIml(), this);
mainPresenterIml.doShopDetail(id + "");
mImage = (ViewPager) findViewById(R.id.viewpage);
mPrice = (TextView) findViewById(R.id.tv_price);
mNum = (TextView) findViewById(R.id.tv_num);
mTitle = (TextView) findViewById(R.id.tv_title);
mImagePageAdapter = new ImagePageAdapter();
mImage.setAdapter(mImagePageAdapter);
}
@Override
public void success(String json) {
try {
ShopDetailBean bean = new Gson().fromJson(json, ShopDetailBean.class);
mTitle.setText(bean.getResult().getCommodityName());
mPrice.setText("¥" + bean.getResult().getPrice());
mNum.setText("已售" + bean.getResult().getSaleNum() + "件");
String picTrue = bean.getResult().getPicture();
picTrueArr = picTrue.split(",");
mImagePageAdapter.notifyDataSetChanged();
//Glide.with(this).load(bean.getResult().getPicture())
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void fail() {
}
private class ImagePageAdapter extends PagerAdapter {
@Override
public int getCount() {
if (picTrueArr == null) {
return 0;
}
return picTrueArr.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
// super.instantiateItem(container, position);
ImageView imageView = new ImageView(CommodityDetailsActivity.this);
Glide.with(CommodityDetailsActivity.this).load(picTrueArr[position]).into(imageView);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
// super.destroyItem(container, position, object);
container.removeView((View) object);
}
}
}