MVP+XRecyclerView上下拉刷新+切换

本文详细介绍了Android应用中布局文件的设计,包括按钮、编辑框、复选框、单选按钮等组件的使用,并展示了如何通过M层进行网络请求获取数据,以及P层处理数据展示的流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局文件

<?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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值