activity_main
<?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:orientation="vertical"
tools:context="com.baway.www.chenzhenyan20170619.MainActivity">
<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>
</LinearLayout>
recycle_item
<?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
package com.baway.www.chenzhenyan20170619;
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;
}
}
MainActivity
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();
}
}
MyDecoration
public class MyDecoration extends RecyclerView.ItemDecoration {
private Context context;
public MyDecoration(Context context){
this.context = context;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
int count = parent.getChildCount();
for (int i = 0; i < count; i ++){
View view = parent.getChildAt(i);
int left = view.getLeft();
int right = view.getRight();
int top = view.getBottom()-10;
int bottom = view.getBottom();
Paint paint = new Paint();
paint.setColor(Color.GREEN);
c.drawRect(left,top,right,bottom,paint);
}
}
}
MyItemClick
public interface MyItemClick {
void myItemOnClick(View v, int position);
void myItemLongOnClick(View v,int position);
}
MyrRecyclerAdapter
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);
}
}
}