依赖库 : compile ‘com.android.support:recyclerview-v7:25.3.1’
一、布局
1.1 main-activity的布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<Button
android:onClick="checkAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:onClick="Invert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反选"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler"
>
</android.support.v7.widget.RecyclerView>
1.2 子类 控件布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="15dp"
android:layout_margin="5dp"
android:background="#AAAAAA"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="20sp"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
二、Bean类
public class Bean {
private String text;
private boolean is;
public Bean(String text, boolean is) {
this.text = text;
this.is = is;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean is() {
return is;
}
public void setIs(boolean is) {
this.is = is;
}
}
三、接口
public interface MyItemClick {
void myItemOnClick(View v, int position);
void myItemLongOnClick(View v,int position);
}
四、适配器
public class MyrRecyclerAdapter extends RecyclerView.Adapter<MyrRecyclerAdapter.MyViewHolder> {
private List<Bean> mList;
private final LayoutInflater mLayoutInflater;
private MyItemClick myItemClick;
public MyrRecyclerAdapter(Context context) {
mLayoutInflater = LayoutInflater.from(context);
}
public void setmList(List<Bean> mList) {
this.mList = mList;
}
public void setMyItemClick(MyItemClick myItemClick) {
this.myItemClick = myItemClick;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = mLayoutInflater.inflate(R.layout.recycle_item, parent, false);
MyViewHolder holder = new MyViewHolder(inflate);
holder.checkBox= (CheckBox) inflate.findViewById(R.id.checkbox);
holder.textView=(TextView) inflate.findViewById(R.id.text);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.textView.setText(mList.get(position).getText());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 1.点击某个条目中的CheckBox如果未选中,则实现单击选中效果(10分)
// 2.点击某个条目中的CheckBox如果已选中,则实现单击反选效果(10分)
if(isChecked){
mList.get(position).setIs(isChecked);
}else {
mList.get(position).setIs(false);
}
}
});
holder.checkBox.setChecked(mList.get(position).is());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myItemClick.myItemOnClick(holder.itemView,holder.getLayoutPosition());
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
myItemClick.myItemLongOnClick(holder.itemView,holder.getLayoutPosition());
return false;
}
});
}
@Override
public int getItemCount() {
return mList==null ? 0:mList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
private TextView textView;
private CheckBox checkBox;
public MyViewHolder(View itemView) {
super(itemView);
}
}
}
五、主方法
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Bean> beans;
private MyrRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initID();
info();
//设置recycler布局类型
LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
adapter = new MyrRecyclerAdapter(this);
adapter.setmList(beans);
adapter.setMyItemClick(new MyItemClick() {
@Override
public void myItemOnClick(View v, int position) {
Toast.makeText(MainActivity.this,position+"",Toast.LENGTH_SHORT).show();
}
@Override
public void myItemLongOnClick(View v, int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage(position+"");
builder.create().show();
}
});
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
// 实现的事条目下的分割线
recyclerView.addItemDecoration(new MyDecoration(this));
}
public void initID(){
recyclerView = (RecyclerView) findViewById(R.id.recycler);
}
public void info(){
beans = new ArrayList<>();
for (int i = 0; i <100 ; i++) {
beans.add(new Bean(i+"",false));
}
}
//点击按钮
public void checkAll(View view){
for (int i = 0; i < beans.size() ; i++) {
beans.get(i).setIs(true);
}
adapter.setmList(beans);
adapter.notifyDataSetChanged();
}
public void Invert(View view){
for (int i = 0; i < beans.size() ; i++) {
if(beans.get(i).is()){
beans.get(i).setIs(false);
}else {
beans.get(i).setIs(true);
}
}
adapter.setmList(beans);
adapter.notifyDataSetChanged();
}
}