显示界面


















网格布局条目显示样式












列表布局



















主界面

publicclassMainActivityextendsAppCompatActivityimplementsShopSearchViewAPI,View.OnClickListener{

privateImageViewcutImg;
privateButtonbtnSearch;
privateListlist=newArrayList<>();
privateXRecyclerViewxR;
privateEditTexteditKey;
privateintflag=1;
privateMyAdaptermyAdapter;
privateinti=1;
privateStringstring="手机";
privateStringname;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加载控件
initView();
getData("手机","1");

//设置可上拉
xR.setPullRefreshEnabled(true);
xR.setLoadingMoreEnabled(true);
//设置上拉下拉样式
xR.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
xR.setLaodingMoreProgressStyle(ProgressStyle.BallClipRotate);

clickListener();

xR.setLoadingListener(newXRecyclerView.LoadingListener(){
@Override
publicvoidonRefresh(){
i=1;
list.clear();
getData(string,""+i);
xR.refreshComplete();
}

@Override
publicvoidonLoadMore(){
i++;
getData(string,""+i);
xR.loadMoreComplete();
}
});


}

publicvoidgetData(Stringkey,Stringpage){
ShopSearchPresentershopSearchPresenter=newShopSearchPresenter(this,this);
shopSearchPresenter.getGoodsData("http://120.27.23.105/product/searchProducts",key,page);
}

privatevoidclickListener(){
cutImg.setOnClickListener(this);
btnSearch.setOnClickListener(this);
}

privatevoidinitView(){
cutImg=(ImageView)findViewById(R.id.cutImg);
btnSearch=(Button)findViewById(R.id.btnSearch);
xR=(XRecyclerView)findViewById(R.id.xrecyclerview);
editKey=(EditText)findViewById(R.id.editKey);
}

@Override
publicvoidgetSuccess(Objecto){
GoodsBeano1=(GoodsBean)o;
Listdata=o1.getData();
list.addAll(data);
setMyAdapter(flag);
}

@Override
publicvoidgetFailed(Exceptione){

}

@Override
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.cutImg:
if(flag==1){
cutImg.setImageResource(R.drawable.lv_icon);
flag=2;
}else{
cutImg.setImageResource(R.drawable.grid_icon);
flag=1;
}
setMyAdapter(flag);
break;
caseR.id.btnSearch:
list.clear();
name=editKey.getText().toString();
string=name;
getData(string,"1");
break;
}
}

publicvoidsetMyAdapter(intf){
if(f==1){
//线性布局管理器VERTICAL默认样式/竖向显示第三个参数是数据是否到过来显示
LinearLayoutManagermanager=newLinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
//添加布局管理器
xR.setLayoutManager(manager);
myAdapter=newMyAdapter(list,this,f);
xR.setAdapter(myAdapter);
}elseif(f==2){
//线性布局管理器VERTICAL默认样式/竖向显示第三个参数是数据是否到过来显示
GridLayoutManagergridLayoutManager=newGridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
//添加布局管理器
xR.setLayoutManager(gridLayoutManager);
myAdapter=newMyAdapter(list,this,f);
xR.setAdapter(myAdapter);
}
}
}

MyAdapter

publicclassMyAdapterextendsRecyclerView.Adapter{
privateListlist;
privateContextcontext;
privateintflag=1;
privateViewinflate;

publicMyAdapter(Listlist,Contextcontext,intflag){
this.list=list;
this.context=context;
this.flag=flag;
}

@Override
publicMyAdapter.MyViewHolderonCreateViewHolder(ViewGroupparent,intviewType){
if(flag==1){
inflate=LayoutInflater.from(context).inflate(R.layout.lvitem,parent,false);
}elseif(flag==2){
inflate=LayoutInflater.from(context).inflate(R.layout.griditem,parent,false);
}

MyViewHoldermyViewHolder=newMyViewHolder(inflate);

returnmyViewHolder;
}

@Override
publicvoidonBindViewHolder(MyAdapter.MyViewHolderholder,intposition){

Stringimages=list.get(position).getImages();
String[]split=images.split("\\|");
Glide.with(context).load(split[0]).into(holder.icon);
holder.title.setText(list.get(position).getTitle());
holder.bargainPrice.setText("折扣价:"+list.get(position).getBargainPrice()+"");
holder.price.setText("原价:"+list.get(position).getPrice());
holder.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

}

@Override
publicintgetItemCount(){
returnlist!=null?list.size():0;
}

classMyViewHolderextendsRecyclerView.ViewHolder{

privateImageViewicon;
privateTextViewtitle;
privateTextViewbargainPrice;
privateTextViewprice;

publicMyViewHolder(ViewitemView){
super(itemView);

icon=(ImageView)itemView.findViewById(R.id.GoodsIcon);
title=(TextView)itemView.findViewById(R.id.title);
bargainPrice=(TextView)itemView.findViewById(R.id.bargainPrice);
price=(TextView)itemView.findViewById(R.id.price);

}
}

}

ShopSearchModle

publicclassShopSearchModle{


publicvoidgetData(Stringurl,Mapmap,finalShopSearchPresenterAPIshopSearchPresenterAPI){
HttpUtils.getInstance().get(url,map,newCallBack(){
@Override
publicvoidonSuccess(Objecto){
shopSearchPresenterAPI.success(o);
}

@Override
publicvoidonFailed(Exceptione){
shopSearchPresenterAPI.failed(e);
}
},GoodsBean.class);
}


}

ShopSearchPresenter

publicclassShopSearchPresenter{

privateShopSearchViewAPIshopSearchViewAPI;
privateContextcontext;
privatefinalShopSearchModleshopSearchModle;

publicShopSearchPresenter(ShopSearchViewAPIshopSearchViewAPI,Contextcontext){
this.shopSearchViewAPI=shopSearchViewAPI;
this.context=context;
shopSearchModle=newShopSearchModle();
}

publicvoidgetGoodsData(Stringurl,Stringkeywords,Stringpage){
Mapmap=newHashMap<>();
map.put("keywords",keywords);
map.put("page",page);
shopSearchModle.getData(url,map,newShopSearchPresenterAPI(){
@Override
publicvoidsuccess(Objecto){
shopSearchViewAPI.getSuccess(o);
}

@Override
publicvoidfailed(Exceptione){
shopSearchViewAPI.getFailed(e);
}
});
}


}

ShopSearchPresenterAPI

publicinterfaceShopSearchPresenterAPI{

voidsuccess(Objecto);

voidfailed(Exceptione);


}

重点内容

packagewangxuewei.bwie.com.wangxuewei1510c2071219;

/**
*Createdbyjimon2017/12/19.
*/

publicinterfaceShopSearchViewAPI{

voidgetSuccess(Objecto);

voidgetFailed(Exceptione);


}

HttpUtils

publicclassHttpUtils{

privatestaticvolatileHttpUtilsinstance;

privatestaticHandlerhandler=newHandler();

privateHttpUtils(){

}

publicstaticHttpUtilsgetInstance(){
if(instance==null){
synchronized(HttpUtils.class){
if(instance==null){
instance=newHttpUtils();
}
}
}
returninstance;
}

//get请求
publicvoidget(Stringurl,Mapmap,finalCallBackcallBack,finalClassc){
//对url和参数做拼接处理
StringBufferstringBuffer=newStringBuffer();
stringBuffer.append(url);
//判断是否存在?if中是存在
if(stringBuffer.indexOf("?")!=-1){
//判断?是否在最后一位if中是不在最后一位
if(stringBuffer.indexOf("?")!=stringBuffer.length()-1){
stringBuffer.append("&");
}
}else{
stringBuffer.append("?");
}
for(Map.Entryentry:map.entrySet()){
stringBuffer.append(entry.getKey())
.append("=")
.append(entry.getValue())
.append("&");
}
//判断是否存在&if中是存在
if(stringBuffer.indexOf("&")!=-1){
stringBuffer.deleteCharAt(stringBuffer.lastIndexOf("&"));
}


//1:创建OkHttpClient对象
OkHttpClientokHttpClient=newOkHttpClient.Builder().addInterceptor(newLogger()).build();
//2:创建Request对象
finalRequestrequest=newRequest.Builder()
.get()
.url(stringBuffer.toString())
.build();
//3:创建Call对象
Callcall=okHttpClient.newCall(request);
//4:请求网络
call.enqueue(newCallback(){
//请求失败
@Override
publicvoidonFailure(Callcall,finalIOExceptione){
handler.post(newRunnable(){
@Override
publicvoidrun(){
callBack.onFailed(e);
}
});
}

//请求成功
@Override
publicvoidonResponse(Callcall,Responseresponse)throwsIOException{
Stringresult=response.body().string();
//拿到数据解析
finalObjecto=newGson().fromJson(result,c);
//当前是在子线程,回到主线程中
handler.post(newRunnable(){
@Override
publicvoidrun(){
//回调
callBack.onSuccess(o);
}
});
}
});

}

//post请求
publicvoidpost(Stringurl,Mapmap,finalCallBackcallBack,finalClassc){
//1:创建OkHttpClient对象
OkHttpClientokHttpClient=newOkHttpClient();
//2:提供post请求需要的body对象
FormBody.Builderbuilder=newFormBody.Builder();
for(Map.Entryentry:map.entrySet()){
builder.add(entry.getKey(),entry.getValue());
}
FormBodybody=builder.build();
//3:创建Request对象
finalRequestrequest=newRequest.Builder()
.post(body)
.url(url)
.build();
//4:创建Call对象
Callcall=okHttpClient.newCall(request);
//5:请求网络
call.enqueue(newCallback(){
//请求失败
@Override
publicvoidonFailure(Callcall,finalIOExceptione){
handler.post(newRunnable(){
@Override
publicvoidrun(){
callBack.onFailed(e);
}
});
}

//请求成功
@Override
publicvoidonResponse(Callcall,Responseresponse)throwsIOException{
Stringresult=response.body().string();
//拿到数据解析
finalObjecto=newGson().fromJson(result,c);
//当前是在子线程,回到主线程中
handler.post(newRunnable(){
@Override
publicvoidrun(){
//回调
callBack.onSuccess(o);
}
});
}
});
}

}

重点内容

publicinterfaceCallBack{

voidonSuccess(Objecto);

voidonFailed(Exceptione);

}

Logger

publicclassLoggerimplementsInterceptor{
@Override
publicResponseintercept(Chainchain)throwsIOException{
Requestoriginal=chain.request();
HttpUrlurl=original.url().newBuilder()
.addQueryParameter("source","android")
.build();
//添加请求头
Requestrequest=original.newBuilder()
.url(url)
.build();
returnchain.proceed(request);
}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。

热门推荐

1 毛坯房验收经验和常识 看了之后再验房心里有底
2 二手房收房如何交接 二手房收房注意问题
3 专业验收毛坯房的价格 商品房验收合格的标准
4 精装房怎么验收 精装房请验房师有用吗
5 一般要到哪里找验房师 验房师有哪些作用呢
6 请人验房一般是多少钱 验房师费用是多少
7 怎样测量房子面积 建筑面积和使用面积怎么算
8 收房需要注意什么 仔细检查不松懈
9 收房时三书一证一表是什么 主要作用介绍
10 交房时交房税费有哪些 本文为你一一讲解
11 验房都需要验什么 要做哪些准备呢
12 毛坯房验房师有必要请吗 毛坯房装修完如何验收
13 地下室防水工程质量验收规范详解
14 水性涂料、油性涂料区别介绍
15 零基础布艺DIY工坊 教你做超萌猫头鹰钥匙包
16 三棵树漆怎么样?三棵树漆官方网站
17 家庭“装修套餐”中猫腻你知道吗?
18 小空间大浴望 卫浴间装修巧支招