MVP+Fresco+RXJava+Retrofit+全选+反选

//加依赖

compile 'io.reactivex:rxandroid:1.1.0'
    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 'com.google.code.gson:gson:2.8.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile 'com.squareup.okio:okio:1.13.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.facebook.fresco:fresco:0.12.0'



加网络权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>


Api类

public class Api {
    public static final String PATH = "http://tingapi.ting.baidu.com/";
}

 

ApiService

public interface ApiService {

    @GET("v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=30&offset=0")
    Observable<MyBean> getdatas();
}

IView

public interface IView {
    void showList(List<MyBean.SongListBean> list);
    void showError(String e);
}


IModel

public interface IModel {
    void RequestData(String url,OnRequestListener onRequestListener);
}


ListModel

public class ListModel implements IModel {
    @Override
    public void RequestData(String url, final OnRequestListener onRequestListener) {
        //添加拦截器
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new LoggingInterceptor())
                .build();
        //retrofit网络请求
        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient)
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        //结合rxjava
        Observable<MyBean> getdatas = apiService.getdatas();
        getdatas.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MyBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        onRequestListener.OnError(e.getMessage().toString());
                    }

                    @Override
                    public void onNext(MyBean myBean) {
                        List<MyBean.SongListBean> song_list = myBean.getSong_list();
                        onRequestListener.OnSuccess(song_list);
                    }
                });
    }
}




public interface OnRequestListener {
    void OnSuccess(List<MyBean.SongListBean> list);
    void OnError(String e);
}


public interface IPresenter {
    void loadList(String url);
}



public class ListPresenter implements IPresenter {

    private IView iView;
    private IModel iModel;

    public ListPresenter(IView iView) {
        this.iView = iView;
        iModel = new ListModel();
    }

    //model层与view层交互
    @Override
    public void loadList(String url) {
        iModel.RequestData(url, new OnRequestListener() {
            @Override
            public void OnSuccess(List<MyBean.SongListBean> list) {
                iView.showList(list);
            }

            @Override
            public void OnError(String e) {
                iView.showError(e);
            }
        });
    }
}



public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<MyBean.SongListBean> list;
    private Context context;
    private MainActivity ac;
    int visible = View.GONE;

    public MyAdapter(List<MyBean.SongListBean> list, Context context) {
        this.list = list;
        this.context = context;
        ac = (MainActivity) context;
    }

    public int getVisible() {
        return visible;
    }

    public void setVisible(int visible) {
        this.visible = visible;
    }

    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //设置布局
        MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.item, parent, false));
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
        //赋值
        final MyBean.SongListBean sb = list.get(position);
        holder.tv.setText(sb.getAuthor() + "--" + sb.getTitle());
        Uri uri = Uri.parse(sb.getPic_small());
        holder.img.setImageURI(uri);
        //调整checkbox
        holder.ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sb.setCheck(isChecked);
                if (isChecked) {
                    ac.setCb(isAllChecked());
                } else {
                    ac.setCb(false);
                }
            }
        });
        holder.ck.setChecked(sb.isCheck());
        //设置checkbox的显示隐藏
        //int visible = View.VISIBLE;
        holder.ck.setVisibility(visible);
    }

    //全选
    private boolean isAllChecked() {
        for (int i = 0; i < list.size(); i++) {
            MyBean.SongListBean dataBean = list.get(i);
            if (!dataBean.isCheck()) {
                return false;
            }
        }
        return true;
    }

    //全选
    public void ckall() {
        for (int i = 0; i < list.size(); i++) {
            MyBean.SongListBean dataBean = list.get(i);
            dataBean.setCheck(true);
            notifyDataSetChanged();
        }
    }

    //取消全选
    public void cknull() {
        for (int i = 0; i < list.size(); i++) {
            MyBean.SongListBean dataBean = list.get(i);
            dataBean.setCheck(false);
            notifyDataSetChanged();
        }
    }

    //删除
    public void ckdelete() {
        for (int i = 0; i < list.size(); i++) {
            MyBean.SongListBean dataBean = list.get(i);
            if (dataBean.isCheck()) {
                list.remove(dataBean);
                notifyDataSetChanged();
            }

        }
    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    //viewholder
    class MyViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView img;
        TextView tv;
        CheckBox ck;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.item_tv);
            img = (SimpleDraweeView) itemView.findViewById(R.id.item_img);
            ck = (CheckBox) itemView.findViewById(R.id.item_ck);
        }
    }
}



public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

在清单列表注册

android:name=".app.MyApp"


/**
 * 网络拦截器
 */

public class LoggingInterceptor implements Interceptor {
    private static final String UA = "User-Agent";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request()
                .newBuilder()
                .addHeader(UA, makeUA())
                .build();
        return chain.proceed(request);
    }

    private String makeUA() {
        String s = Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
        return Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
    }

}




public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {

    /**
     * 编辑
     */
    private TextView mTvBj;
    private RecyclerView mRcv;
    /**
     * 全选
     */
    private CheckBox mCkAll;
    /**
     * 删除
     */
    private TextView mDeleteAll;
    private RelativeLayout mAll;
    private MyAdapter myAdapter;
    private int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //presener层
        ListPresenter listPresenter = new ListPresenter(this);
        listPresenter.loadList(Api.PATH);
    }

    @Override
    public void showList(List<MyBean.SongListBean> list) {
        //设置recyclerview展示
        myAdapter = new MyAdapter(list, this);
        mRcv.setAdapter(myAdapter);
        mRcv.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public void showError(String e) {
        Toast.makeText(this, "" + e, Toast.LENGTH_SHORT).show();
        Log.e("哈哈哈哈啊哈哈哈哈", e);
    }

    private void initView() {
        mTvBj = (TextView) findViewById(R.id.tv_bj);
        mTvBj.setOnClickListener(this);
        mRcv = (RecyclerView) findViewById(R.id.rcv);
        mCkAll = (CheckBox) findViewById(R.id.ck_all);
        mDeleteAll = (TextView) findViewById(R.id.delete_all);
        mDeleteAll.setOnClickListener(this);
        mAll = (RelativeLayout) findViewById(R.id.all);
        //全选
        mCkAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCkAll.isChecked()) {
                    mCkAll.setChecked(true);
                    myAdapter.ckall();
                } else {
                    mCkAll.setChecked(false);
                    myAdapter.cknull();
                }
            }
        });
    }

    //全选
    public void setCb(boolean bool) {
        mCkAll.setChecked(bool);
    }

    //点击返回键,若在编辑状态退出编辑状态,再次点击退出应用,若不在编辑状态直接退出应用
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (flag == 1) {
                flag = 0;
                mAll.setVisibility(View.GONE);
                myAdapter.setVisible(View.GONE);
                myAdapter.notifyDataSetChanged();
                mTvBj.setText("编辑");
            } else {
                finish();
                System.exit(0);
            }
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_bj:
                //显示与隐藏
                switch (flag) {
                    case 0:
                        mAll.setVisibility(View.VISIBLE);
                        myAdapter.setVisible(View.VISIBLE);
                        myAdapter.notifyDataSetChanged();
                        mTvBj.setText("完成");
                        flag = 1;
                        break;
                    case 1:
                        mAll.setVisibility(View.GONE);
                        myAdapter.setVisible(View.GONE);
                        myAdapter.notifyDataSetChanged();
                        mTvBj.setText("编辑");
                        flag = 0;
                        break;
                }
                break;
            case R.id.delete_all:
                //删除
                myAdapter.ckdelete();
                break;
        }
    }
}




//主页面布局

<RelativeLayout
        android:background="@android:color/holo_blue_light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher" />
        <TextView
            android:textColor="@android:color/white"
            android:text="我的收藏"
            android:layout_margin="10dp"
            android:layout_centerHorizontal="true"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_bj"
            android:text="编辑"
            android:textStyle="bold"
            android:textColor="@android:color/white"
            android:layout_margin="10dp"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
       android:id="@+id/rcv"
       android:layout_weight="1"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

    <RelativeLayout
        android:id="@+id/all"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/ck_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:text="全选" />

        <TextView
            android:id="@+id/delete_all"
            android:text="删除"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp" />
    </RelativeLayout>




MyAdapter类布局


使用fresco要加

 xmlns:fresco="http://schemas.android.com/apk/res-auto"

 <CheckBox
        android:layout_gravity="center_vertical"
        android:id="@+id/item_ck"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/item_img"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_margin="8dp"
        fresco:failureImage="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/item_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="10dp"
        android:layout_weight="1" />





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值