布局文件
<?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:padding="5dp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/mButton1" android:layout_width="0dp" android:layout_weight="1" android:padding="10dp" android:layout_height="40dp" /> <EditText android:id="@+id/mEdidText1" android:layout_width="0dp" android:layout_weight="5" android:padding="15dp" android:textSize="18sp" android:layout_height="wrap_content" android:hint="搜索"/> <CheckBox android:id="@+id/mCheckBox1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:button="@null"/> </LinearLayout> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:button="@null" android:padding="10dp" android:textSize="18sp" android:gravity="center" android:text="综合"/> <RadioButton android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:button="@null" android:padding="10dp" android:textSize="18sp" android:gravity="center" android:text="销量"/> <RadioButton android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:button="@null" android:padding="10dp" android:textSize="18sp" android:gravity="center" android:text="价格"/> <RadioButton android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:button="@null" android:padding="10dp" android:textSize="18sp" android:gravity="center" android:text="筛选"/> </RadioGroup> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"> </com.jcodecraeer.xrecyclerview.XRecyclerView> </LinearLayout>
M层
public class MyModel { public static Bean getString(int page, int sort, String keywords) { String url = "http://www.zhaoapi.cn/product/searchProducts?keywords="+keywords+"&page="+page+"&sort="+sort; OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .get() .url(url) .build(); try { Response response = okHttpClient.newCall(request).execute(); if (response.isSuccessful()) { String uriStr = response.body().string(); Gson gson = new Gson(); // Bean dataBean = gson.fromJson(uriStr, Bean.class); Bean bean = gson.fromJson(uriStr, Bean.class); return bean; } } catch (IOException e) { e.printStackTrace(); } return null; } }
P层
public class MyPresenter { LieBiao liebiao; public MyPresenter(LieBiao liebiao) { this.liebiao = liebiao; } public MyPresenter(){ this.liebiao = null; } Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bean bean = (Bean) msg.obj; liebiao.success(bean); } }; public void liebiao(final int page,final int sort,final String keywords) { new Thread(new Runnable() { @Override public void run() { Bean bean = MyModel.getString(page,sort,keywords); Message message = handler.obtainMessage(); message.obj = bean; handler.sendMessage(message); } }).start(); } }