依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
布局
<?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:orientation="vertical"
>
<Button
android:id="@+id/qiehuan"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="@drawable/lv_icon"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="@+id/search"
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/sousuo"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
/>
</LinearLayout>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>
---------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_l"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="原价9999"
/>
<TextView
android:id="@+id/tv_r"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="折扣价99.99"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
NewPresenter.java
package com.bwie.xumengshen1113.presenter;
import com.bwie.xumengshen1113.OkHttpUtils;
import com.bwie.xumengshen1113.bean.AllBean;
import com.bwie.xumengshen1113.bean.IView;
import com.bwie.xumengshen1113.callback.CallBack;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*
* @date:2017-11-13
* @time:09:26
*/
public class NewPresenter {
private IView iView;
public void attchView(IView iView){
this.iView=iView;
}
public void getData(String name, int page){
Map<String, String> map = new HashMap<>();
map.put("keywords",name);
map.put("page",page+"");
//调用Okhttp工具类
OkHttpUtils.getInstance().get("http://120.27.23.105/product/searchProducts", map, new CallBack() {
//成功
@Override
public void onSuccess(Object o) {
AllBean oo = (AllBean) o;
List<AllBean.DataBean> data = oo.getData();
iView.success(data);
}
//失败
@Override
public void onFailed(String err) {
iView.failed(err);
}
}, AllBean.class);
}
//内存泄露问题
public void detachView() {
if (iView != null) {
iView = null;
}
}
}
OkHttpUtils.java
package com.bwie.xumengshen1113;
import android.os.Handler;
import android.util.Log;
import com.bwie.xumengshen1113.callback.CallBack;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
*
*
* @date:2017-11-13
* @time:09:10
*/
public class OkHttpUtils {
private static volatile OkHttpUtils instance;
private static Handler handler=new Handler();
private OkHttpUtils(){
}
//单例模式
public static OkHttpUtils getInstance(){
if(instance==null){
synchronized (OkHttpUtils.class){
if(null==instance){
instance=new OkHttpUtils();
}
}
}
return instance;
}
//请求网络方法
public void get(String url, Map<String,String> map, final CallBack callBack, final Class cls){
StringBuffer sb = new StringBuffer();
sb.append(url).append("?");
for(Map.Entry<String,String> entry:map.entrySet()){
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
sb.deleteCharAt(sb.lastIndexOf("&"));
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Logger())
.build();
Request request = new Request.Builder()
.get()
.url(sb.toString())
.build();
Log.d("TT", "sb: "+sb);
Call call = client.newCall(request);
call.enqueue(new Callback() {
//请求失败
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
callBack.onFailed(e.getMessage());
}
});
}
//请求成功
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
Object o = gson.fromJson(string, cls);
Log.d("TT", "oooooooo: "+o);
callBack.onSuccess(o);
}
});
}
});
}
}
MainActivity.java
package com.bwie.xumengshen1113;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.bwie.xumengshen1113.adapter.RecyclerviewAdapter;
import com.bwie.xumengshen1113.bean.AllBean;
import com.bwie.xumengshen1113.bean.IView;
import com.bwie.xumengshen1113.presenter.NewPresenter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {
private Button qiehuan;
private Button sousuo;
private EditText search;
private XRecyclerView xrv;
ArrayList<AllBean.DataBean> lists = new ArrayList<>();
private RecyclerviewAdapter adapter;
private NewPresenter presenter;
private String name="手机";
private LinearLayoutManager linearLayoutManager;
private GridLayoutManager gridLayoutManager;
private int page=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
initView();
presenter = new NewPresenter();
//适配器
adapter = new RecyclerviewAdapter(this, lists);
linearLayoutManager = new LinearLayoutManager(this);
gridLayoutManager = new GridLayoutManager(this, 2);
xrv.setLayoutManager(linearLayoutManager);
//获取数据
presenter.getData(name,page);
//刷新加载
xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
page=0;
lists.clear();
presenter.getData(name,page);
xrv.refreshComplete();
}
@Override
public void onLoadMore() {
page++;
presenter.getData(name,page);
xrv.refreshComplete();
}
});
//设置适配器
xrv.setAdapter(adapter);
presenter.attchView(this);
//点击事件
qiehuan.setOnClickListener(this);
sousuo.setOnClickListener(this);
}
private void initView() {
qiehuan = (Button) findViewById(R.id.qiehuan);
sousuo = (Button) findViewById(R.id.sousuo);
search = (EditText) findViewById(R.id.search);
xrv = (XRecyclerView) findViewById(R.id.xrv);
}
@Override
public void success(List<AllBean.DataBean> list) {
if (null != list) {
Log.d("TT", "success: "+list);
lists.addAll(list);
adapter.notifyDataSetChanged();
}
}
@Override
public void failed(String err) {
Toast.makeText(this,"请求失败",Toast.LENGTH_SHORT).show();
}
//点击事件操作
@Override
public void onClick(View view) {
switch (view.getId()){
//切换视图显示
case R.id.qiehuan:
if(linearLayoutManager!=null){
xrv.setLayoutManager(gridLayoutManager);
linearLayoutManager=null;
qiehuan.setBackgroundResource(R.drawable.grid_icon);
}else if(gridLayoutManager!=null){
linearLayoutManager= new LinearLayoutManager(MainActivity.this);
xrv.setLayoutManager(linearLayoutManager);
qiehuan.setBackgroundResource(R.drawable.lv_icon);
}
break;
//搜索按钮
case R.id.sousuo:
String trim = search.getText().toString().trim();
name=trim;
lists.clear();
presenter.getData(trim,page);
adapter.notifyDataSetChanged();
break;
default:break;
}
}
//内存泄露问题
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.detachView();
}
}
}
Logger.java
package com.bwie.xumengshen1113;
import java.io.IOException;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
*
*
* @date:2017-11-13
* @time:15:51
*/
public class Logger implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
HttpUrl url=original.url().newBuilder()
.addQueryParameter("source","android")
.build();
//添加请求头
Request request = original.newBuilder()
.url(url)
.build();
return chain.proceed(request);
}
}
RecyclerviewAdapter.java
package com.bwie.xumengshen1113.adapter;
import android.content.Context;
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.bwie.xumengshen1113.R;
import com.bwie.xumengshen1113.bean.AllBean;
import java.util.List;
/**
*
*
* @date:2017-11-13
* @time:09:47
*/
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.ViewHolder>{
private Context context;
private List<AllBean.DataBean> list;
public RecyclerviewAdapter(Context context, List<AllBean.DataBean> list) {
this.context = context;
this.list = list;
}
//初始化ViewHolder
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.listview, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
//绑定ViewHolder
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String images = list.get(position).getImages();
String[] split = images.split("\\!");
Glide.with(context).load(split[0]).into(holder.img);
holder.title.setText(list.get(position).getTitle());
}
@Override
public int getItemCount() {
return list.size();
}
//创建ViewHolder
class ViewHolder extends RecyclerView.ViewHolder{
private final ImageView img;
private final TextView title;
public ViewHolder(View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
title = itemView.findViewById(R.id.tv_title);
}
}
}
public interface IView {
void success(List<AllBean.DataBean> list);
void failed(String err);
}
public interface CallBack {
void onSuccess(Object o);
void onFailed(String err);
}