MVP&Retrofit&RXjava

本文介绍了一个基于Android平台的应用案例,通过调用百度音乐API获取榜单数据,并使用RecyclerView组件展示歌曲列表。文章详细展示了依赖库配置、API接口定义、OkHttp与Retrofit网络请求、MVVM架构应用及UI布局设计等关键技术细节。

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

==================================依赖==================================================

compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okio:okio:1.13.0'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.facebook.fresco:fresco:0.9.0+'
compile files('libs/gson-2.2.4.jar')
compile files('libs/universal-image-loader-1.9.3-with-sources.jar')
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'io.reactivex:rxandroid:1.1.0'

=================================API===========================================

public class Api {
    // //http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0

    public  static final  String api_man="http://tingapi.ting.baidu.com/v1/restserver/";

}

===================================ApiService=====================================

public interface ApiService {

//ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0

    @GET("ting")
    Observable<RecyBean> path(@Query("method") String method,@Query("type") int type,@Query("size") int size,@Query("offset") int offest);


}

====================================================================================

public class OkHttpUtils {
    private Handler handler = new Handler();
    public Handler getHandler(){
        return handler;
    }
    //单例
    private static OkHttpUtils okHttpUtils = new OkHttpUtils();
    private OkHttpUtils(){};
    public static OkHttpUtils getInstance(){
        return okHttpUtils;
    }

    private OkHttpClient client;
    private void initOkHttpClient(){
        if(client == null){
            LoggingInterceptor log=new LoggingInterceptor();
            client = new OkHttpClient.Builder().addInterceptor(log).build();
        }
    }

    //公用的get请求方法  完成的功能不确定
    public void doGet(String url, Callback callback){
        initOkHttpClient();
        Request request = new Request.Builder().addHeader("User-Agent","").url(url).build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }
   
}

====================================================================================

public abstract class OnUiCallback implements Callback{
    private Handler handler = OkHttpUtils.getInstance().getHandler();
    public abstract void onFailed(Call call, IOException e);
    public abstract void onSuccess(String result) throws IOException;

    @Override
    public void onFailure(final Call call, final IOException e) {
        //该方式  存在问题  网络请求也跑到了主线程   待解决
        //该方法就是把  线程posthandler所在的线程
        handler.post(new Runnable() {
            @Override
            public void run() {
                onFailed(call, e);
            }
        });

    }

    @Override
    public void onResponse(final Call call, final Response response) throws IOException {
        final String result = response.body().string();
        //该方式  存在问题  网络请求也跑到了主线程   待解决
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    onSuccess(result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

====================================================================================

public interface IRecyView {
    void showrecy(RecyBean rb);
    int getID();
}

====================================================================================

public interface FinishListener {

    void  onScuess(RecyBean bean);

}

====================================================================================

public interface IRecyModel {
    void recy(int type, FinishListener finishListener);
}

====================================================================================

public class RecyModel implements  IRecyModel{



    @Override
    public void recy(int type, final FinishListener listener) {
        LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
        OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build();


        Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.api_man).addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient)
                .build();

        ApiService apiService = retrofit.create(ApiService.class);

        Observable<RecyBean> observable = apiService.path("baidu.ting.billboard.billList",type,10,0);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RecyBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RecyBean bean) {


                        listener.onScuess(bean);
                    }
                });


    }
}

====================================================================================

public class RecyPresenter implements FinishListener{
    IRecyView iRecyView;
    IRecyModel model;
    Context context;

    public RecyPresenter(IRecyView iRecyView, Context context) {
        this.iRecyView = iRecyView;
        this.context = context;
        model=new RecyModel();

    }
    //请求数据的方法
    public void Showrecy() {
        int type=iRecyView.getID();
        model.recy(type,this);
    }

    @Override
    public void onScuess(RecyBean bean) {
        iRecyView.showrecy(bean);
    }
}

====================================================================================

public class MainActivity extends AppCompatActivity implements IRecyView, View.OnClickListener {
    private XRecyclerView mXrv;
    private RecyPresenter presenter;
    private ImageView mBack;
    private TextView mRegeHeader;
    private TextView mBj;
    private RelativeLayout mRl1;
    private CheckBox mCheckboxAll;
    private Button mButton;
    private RelativeLayout mRel;

    int type = 21;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        setContentView(R.layout.activity_main);
        initView();
        presenter = new RecyPresenter(this, this);
        presenter.Showrecy();
    }

    private void initView() {
        mXrv = (XRecyclerView) findViewById(R.id.xrv);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mXrv.setLayoutManager(manager);
        mXrv.setLoadingMoreEnabled(true);

        mBack = (ImageView) findViewById(R.id.back);
        mRegeHeader = (TextView) findViewById(R.id.header_rege);
        mBj = (TextView) findViewById(R.id.bj);
        mBj.setOnClickListener(this);
        mRl1 = (RelativeLayout) findViewById(R.id.rl1);
        mCheckboxAll = (CheckBox) findViewById(R.id.checkboxAll);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(this);
        mRel = (RelativeLayout) findViewById(R.id.rel);
    }

    @Override
    public void showrecy(RecyBean rb) {
        MyAdapter adapter = new MyAdapter(MainActivity.this, rb);
        TextView tv = new TextView(this);
        adapter.addHeader(tv);
        mXrv.setAdapter(adapter);

        adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, "1112", Toast.LENGTH_SHORT).show();

            }
        });

        mXrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                type++;
                presenter.Showrecy();
                mXrv.refreshComplete();


            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    @Override
    public int getID() {
        return type;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bj:
                // TODO 17/11/08
                String text = mBj.getText().toString();
                if (text.equals("编辑")) {
                    mBj.setText("完成");
                    mRel.setVisibility(View.VISIBLE);
//                    bool = true;
//                    adapter = new MyAdapter(this, list2, bool);
//                    mXrv.setAdapter(adapter);
                } else {
                    mBj.setText("编辑");
                    mRel.setVisibility(View.INVISIBLE);
//                    bool = false;
//                    adapter = new MyAdapter(this, list2, bool);
//                    mXrv.setAdapter(adapter);
                }
                break;
            case R.id.button:
                // TODO 17/11/08
                break;
            default:
                break;
        }
    }
}

=====================================activity_main.xml================================

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <RelativeLayout
        android:id="@+id/rl1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/blue">
        <ImageView
            android:id="@+id/back"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/back"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"/>
        <TextView
            android:id="@+id/header_rege"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/back"
            android:text="百度热歌榜"
            android:textColor="#ffffffff"
            android:textSize="20sp"
            android:layout_marginLeft="90dp"
            android:layout_marginTop="10dp"/>
        <TextView
            android:id="@+id/bj"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编辑"
            android:textColor="#ffffffff"
            android:textSize="20sp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            />
    </RelativeLayout>

    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/xrv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/rl1"
        >
    </com.jcodecraeer.xrecyclerview.XRecyclerView>
    <RelativeLayout
        android:id="@+id/rel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#ffffff"
        android:visibility="visible"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/checkboxAll"
            android:text="全选"
            android:layout_alignBaseline="@+id/button"
            android:layout_alignBottom="@+id/button"
            />
        <Button
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"
            android:id="@+id/button" />
    </RelativeLayout>

</RelativeLayout>

================================recy_header1.xml===================================

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/rl">



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="130dp"

        >
        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/header_iv"
            android:layout_width="200px"
            android:layout_height="200px"
            android:src="@mipmap/ic_launcher"
            />

        <TextView
            android:id="@+id/header_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/header_iv"
            android:text="标题"
            android:textSize="20sp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"/>
        <TextView
            android:id="@+id/header_author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/header_iv"
            android:text="最近更新:"
            android:layout_marginLeft="10dp"
            android:layout_below="@+id/header_title"/>
        <TextView
            android:id="@+id/header_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/header_title"
            android:layout_toRightOf="@+id/header_author"
            android:text="名字"
            android:layout_marginLeft="10dp"
            />

        <TextView
            android:id="@+id/header_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="内容"
            android:layout_below="@+id/header_name"
            android:layout_toRightOf="@+id/header_iv"
            android:layout_marginLeft="10dp"/>

    </RelativeLayout>


</RelativeLayout>

==================================recy_item1.xml======================================

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/rl">



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="130dp"

        >
        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/header_iv"
            android:layout_width="200px"
            android:layout_height="200px"
            android:src="@mipmap/ic_launcher"
            />

        <TextView
            android:id="@+id/header_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/header_iv"
            android:text="标题"
            android:textSize="20sp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"/>
        <TextView
            android:id="@+id/header_author"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/header_iv"
            android:text="最近更新:"
            android:layout_marginLeft="10dp"
            android:layout_below="@+id/header_title"/>
        <TextView
            android:id="@+id/header_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/header_title"
            android:layout_toRightOf="@+id/header_author"
            android:text="名字"
            android:layout_marginLeft="10dp"
            />

        <TextView
            android:id="@+id/header_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="内容"
            android:layout_below="@+id/header_name"
            android:layout_toRightOf="@+id/header_iv"
            android:layout_marginLeft="10dp"/>

    </RelativeLayout>


</RelativeLayout>

==================================recy_item11.xml======================================

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:layout_alignTop="@+id/title1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <TextView
        android:id="@+id/title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/iv1"
        android:text="标题"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="40dp"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/author1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/iv1"
        android:text="作者"
        android:layout_marginLeft="10dp"
        android:layout_below="@+id/title1"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/name1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title1"
        android:layout_toLeftOf="@+id/author1"
        android:text="名字"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        />
    <ImageView
        android:id="@+id/iv1"
        android:layout_width="180px"
        android:layout_height="180px"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentRight="true"/>
</RelativeLayout>


资源下载链接为: https://pan.quark.cn/s/abbae039bf2a 无锡平芯微半导体科技有限公司生产的A1SHB三极管(全称PW2301A)是一款P沟道增强型MOSFET,具备低内阻、高重复雪崩耐受能力以及高效电源切换设计等优势。其技术规格如下:最大漏源电压(VDS)为-20V,最大连续漏极电流(ID)为-3A,可在此条件下稳定工作;栅源电压(VGS)最大值为±12V,能承受正反向电压;脉冲漏极电流(IDM)可达-10A,适合处理短暂高电流脉冲;最大功率耗散(PD)为1W,可防止器件过热。A1SHB采用3引脚SOT23-3封装,小型化设计利于空间受限的应用场景。热特性方面,结到环境的热阻(RθJA)为125℃/W,即每增加1W功率损耗,结温上升125℃,提示设计电路时需考虑散热。 A1SHB的电气性能出色,开关特性优异。开关测试电路及波形图(图1、图2)展示了不同条件下的开关性能,包括开关上升时间(tr)、下降时间(tf)、开启时间(ton)和关闭时间(toff),这些参数对评估MOSFET在高频开关应用中的效率至关重要。图4呈现了漏极电流(ID)与漏源电压(VDS)的关系,图5描绘了输出特性曲线,反映不同栅源电压下漏极电流的变化。图6至图10进一步揭示性能特征:转移特性(图7)显示栅极电压(Vgs)对漏极电流的影响;漏源开态电阻(RDS(ON))随Vgs变化的曲线(图8、图9)展现不同控制电压下的阻抗;图10可能涉及电容特性,对开关操作的响应速度和稳定性有重要影响。 A1SHB三极管(PW2301A)是高性能P沟道MOSFET,适用于低内阻、高效率电源切换及其他多种应用。用户在设计电路时,需充分考虑其电气参数、封装尺寸及热管理,以确保器件的可靠性和长期稳定性。无锡平芯微半导体科技有限公司提供的技术支持和代理商服务,可为用户在产品选型和应用过程中提供有
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值