1activity主页面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".view.activity.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/linear_bottom"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:background="#999999"
android:layout_height="48dp">
<CheckBox
android:id="@+id/select_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="@+id/price_all"
android:text="¥100"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
2.activity主页面
package com.example.mycheckbox.view.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.example.mycheckbox.R;
import com.example.mycheckbox.model.NewsBean;
import com.example.mycheckbox.presenter.MainPresenter;
import com.example.mycheckbox.view.adapter.MyAdapter;
import com.example.mycheckbox.view.interfaces.IMainView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaseActivity implements IMainView<NewsBean>,View.OnClickListener {
private RecyclerView recyclerView;
private MainPresenter mainPresenter;
private int page=1;
private LinearLayoutManager linearLayoutManager;
private MyAdapter myAdapter;
private CheckBox select_all;
private TextView price_all;
@Override
protected int bindLayout() {
return R.layout.activity_main;
}
@Override
protected void initView() {
recyclerView = findViewById(R.id.recyclerView);
select_all = findViewById(R.id.select_all);
price_all = findViewById(R.id.price_all);
select_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
}
@Override
protected void initData() {
mainPresenter = new MainPresenter();
mainPresenter.loadDataNew(page);
mainPresenter.setView(this);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
myAdapter = new MyAdapter(this);
recyclerView.setAdapter(myAdapter);
myAdapter.setCheckBox(select_all);
myAdapter.setText(price_all);
}
@Override
public void onSuccess(NewsBean newsBean) {
List<NewsBean.ResultsBean> results = newsBean.getResults();
myAdapter.setData((ArrayList<NewsBean.ResultsBean>) results);
}
@Override
public void onErr(String err) {
}
@Override
protected void onDestroy() {
super.onDestroy();
mainPresenter.datachView();
}
@Override
public void onClick(View v) {
}
}
3.多选框以及条目布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="16dp"
tools:context=".view.activity.MainActivity">
<CheckBox
android:id="@+id/select_xuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="48dp"
android:textSize="18sp"
android:layout_toRightOf="@+id/select_xuan"
android:layout_centerInParent="true"
/>
<ImageView
android:id="@+id/image"
android:src="@mipmap/ic_launcher"
android:layout_width="100dp"
android:layout_height="100dp"
android:visibility="gone"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
4.adapter适配器
package com.example.mycheckbox.view.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.mycheckbox.R;
import com.example.mycheckbox.model.NewsBean;
import com.example.mycheckbox.view.activity.MainActivity;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: 薄帅北
* @Date: 2019/3/6 14:46:34
* @Description:
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
private ArrayList<NewsBean.ResultsBean> mList=new ArrayList<>();
private CheckBox mCheckBox;
private TextView priceAll;
private float num = 0;
public MyAdapter(Context context) {
this.mContext=context;
}
public void setData(ArrayList<NewsBean.ResultsBean> list ){
this.mList= list;
notifyDataSetChanged();
}
@NonNull
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.activity_check, null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder myViewHolder, final int i) {
if (mList.get(i)!=null){
myViewHolder.name.setText(i+""+mList.get(i).getTitle()+"");
Glide.with(mContext).load(mList.get(i).getUrl()).into(myViewHolder.image);
}
Log.e("myMessage","==="+"i"+i);
myViewHolder.select_xuan.setChecked(mList.get(i).isChecked());
myViewHolder.select_xuan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mList.get(i).setChecked(isChecked);
boolean selectAll = isSelectAll();
mCheckBox.setChecked(selectAll);
float price = mList.get(i).getPrice();
if (isChecked){
num+=price;
}else{
num-=price;
}
priceAll.setText(num+"");
}
});
}
private boolean isSelectAll() {
for (int i = 0; i < mList.size(); i++) {
boolean checked = mList.get(i).isChecked();
if (!checked){
return false;
}
}
return true;
}
@Override
public int getItemCount() {
return mList.size();
}
//全选反选
public void setCheckBox(CheckBox select_all) {
this.mCheckBox=select_all;
mCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBoxb=(CheckBox) v;
boolean checked = checkBoxb.isChecked();
selectAll(checked);
}
});
}
//全选反选
private void selectAll(boolean checked) {
for (int i = 0; i <mList.size() ; i++) {
NewsBean.ResultsBean resultsBean=mList.get(i);
resultsBean.setChecked(checked);
}
notifyDataSetChanged();
}
public void setText(TextView price_all) {
this.priceAll=price_all;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public ImageView image;
public CheckBox select_xuan;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
image = itemView.findViewById(R.id.image);
select_xuan = itemView.findViewById(R.id.select_xuan);
}
}
}