導入依賴
compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.squareup.okhttp3:okhttp:3.3.0' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
添加權限
<uses-permission android:name="android.permission.INTERNET"/>一、MainActivity的佈局
<?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.bw.lenovo.lianxi_showrecylcrview_two.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#f89" android:layout_gravity="center_horizontal" android:textSize="25sp" android:layout_marginTop="8dp" android:text="商品展示列表"/> <ImageView android:id="@+id/icon_change" android:layout_gravity="right" android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/kind_grid"/> </FrameLayout> <View android:layout_width="match_parent" android:layout_height="1sp" android:background="#000000"/> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </LinearLayout>
二、頁面展示的佈局
<?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="horizontal"> <ImageView android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/icon"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:text="商品名稱" android:textSize="25sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/title"/> <TextView android:text="價格:" android:textColor="#f00" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/price"/> </LinearLayout> </LinearLayout>
三、OkhttpUtils網絡請求
package com.bw.lenovo.lianxi_showrecylcrview_two.Utils; import android.os.Handler; import android.os.Message; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class OkHttpUtils { // private static String goods_url="http://120.27.23.105/product/getProducts?pscid=39&page=1"; private MyHandler myhandelr = new MyHandler(); private static OkHttpUtils okHttpUtils = null; private OnLoadListener onLoadListener; public static OkHttpUtils getInstance() { if (okHttpUtils == null) { okHttpUtils = new OkHttpUtils(); } return okHttpUtils; } public void OKGet(String url, String pscid, String page) { //創建oK對象 OkHttpClient okHttpClient = new OkHttpClient(); //創建請求對象 String url1 = url + "?pscid=" + pscid + "&page=" + page; Request request = new Request.Builder().url(url1).build(); //創建請求隊列 Call call = okHttpClient.newCall(request); //執行 異步請求 call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Message message = myhandelr.obtainMessage(); message.what = 0; message.obj = e.getMessage(); myhandelr.sendMessage(message); } @Override public void onResponse(Call call, Response response) throws IOException { Message message = myhandelr.obtainMessage(); message.what = 1; message.obj = response.body().string(); myhandelr.sendMessage(message); } }); } //處理線程 class MyHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: String error = (String) msg.obj; onLoadListener.LoadError(error); break; case 1: String json = (String) msg.obj; onLoadListener.LoadSuccess(json); break; } } } //定義接口回調的方法 //定義接口 public interface OnLoadListener { //定義方法 void LoadSuccess(String json); void LoadError(String Error); } //定義一個方法,供外部調用 public void SetOnLoadListener(OnLoadListener onLoadListener) { this.onLoadListener = onLoadListener; } }
四、解析數據(創建一個類解析接口數據)
五、適配器
package com.bw.lenovo.lianxi_showrecylcrview_two.Adapter; import android.content.Context; import android.support.v7.widget.LinearLayoutManager; 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.bw.lenovo.lianxi_showrecylcrview_two.Bean.GoodListBean; import com.bw.lenovo.lianxi_showrecylcrview_two.R; import java.util.List; /** * Created by lenovo on 2018/2/17. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Myviewholder> { //把context 和list數據傳過來 private Context context; private List<GoodListBean.DataBean> list; public MyAdapter(Context context, List<GoodListBean.DataBean> list) { this.context = context; this.list = list; } //創建Viewholder時調用 @Override public Myviewholder onCreateViewHolder(ViewGroup parent, int viewType) { //加載佈局 View view = View.inflate(context, R.layout.item_horizontal, null); //實例化viewholder Myviewholder myviewholder = new Myviewholder(view); return myviewholder; } //關聯viewholder時調用 @Override public void onBindViewHolder(Myviewholder holder, int position) { //獲取圖片 String images = list.get(position).getImages(); //將圖片按照|豎槓拆分 String icon_url = images.split("\\|")[0]; Glide.with(context).load(icon_url).into(holder.getIcon()); //賦值 holder.getTitle().setText(list.get(position).getTitle()); holder.getPrice().setText(list.get(position).getPrice() + ""); } @Override public int getItemCount() { return list.size(); } //viewhlder class Myviewholder extends RecyclerView.ViewHolder { private final ImageView icon; private final TextView title; private final TextView price; public Myviewholder(View itemView) { // itemView就是條目的佈局文件 super(itemView); //找到控件,條目裏面的控件 icon = itemView.findViewById(R.id.icon); title = itemView.findViewById(R.id.title); price = itemView.findViewById(R.id.price); } public ImageView getIcon() { return icon; } public TextView getTitle() { return title; } public TextView getPrice() { return price; } } }
六、MainActivity主頁面代碼
package com.bw.lenovo.lianxi_showrecylcrview_two; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.widget.ImageView; import com.bw.lenovo.lianxi_showrecylcrview_two.Adapter.MyAdapter; import com.bw.lenovo.lianxi_showrecylcrview_two.Bean.GoodListBean; import com.bw.lenovo.lianxi_showrecylcrview_two.Utils.OkHttpUtils; import com.google.gson.Gson; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener, OkHttpUtils.OnLoadListener { private static final String TAG = "MainActivity=============="; private static String goods_url = "http://120.27.23.105/product/getProducts"; private RecyclerView recyclerview; private ImageView icon_change; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化界面 initviews(); //初始化數據 OkHttpUtils okHttpUtils = OkHttpUtils.getInstance(); okHttpUtils.OKGet(goods_url, "" + 39, "" + 1); okHttpUtils.SetOnLoadListener(this); } private void initviews() { //根據id找到控件 icon_change = (ImageView) findViewById(R.id.icon_change); recyclerview = (RecyclerView) findViewById(R.id.recyclerview); icon_change.setOnClickListener(this); //設置佈局管理者 recyclerview.setLayoutManager(new LinearLayoutManager(this)); } /** * 開關 * 設置點擊按鈕佈局改變得變量 */ private boolean flag = true; @Override public void onClick(View view) { switch (view.getId()) { case R.id.icon_change: if (flag) { //GridView佈局 recyclerview.setLayoutManager(new GridLayoutManager(this, 2)); } else { //listView佈局 recyclerview.setLayoutManager(new LinearLayoutManager(this)); } flag = !flag; break; } } //接口回調的方法 @Override public void LoadSuccess(String json) { Log.d(TAG, "LoadSuccess() retured:成功================" + json); //gson解析數據 Gson gson = new Gson(); GoodListBean goodListBean = gson.fromJson(json, GoodListBean.class); List<GoodListBean.DataBean> list = goodListBean.getData(); //設置設配器 MyAdapter adapter = new MyAdapter(MainActivity.this, list); recyclerview.setAdapter(adapter); } @Override public void LoadError(String Error) { Log.d(TAG, "LoadError() retured:失敗================" + Error); } }