版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】RecyclerView中如何解决使用CheckBox出现勾选混乱的问题
/upload/information/20200623/125/124600.jpg<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout><?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/id_check_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="CheckBox"/>
</LinearLayout>public
class
MainActivity
extends
AppCompatActivity
{
private
RecyclerView
recyclerView;
private
MyAdapter
myAdapter;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView
=
(RecyclerView)
findViewById(R.id.id_recycler_view);
myAdapter
=
new
MyAdapter();
recyclerView.setLayoutManager(new
LinearLayoutManager(this));
recyclerView.setAdapter(myAdapter);
}
private
class
MyAdapter
extends
RecyclerView.Adapter
{
private
List<String>
content;
@Override
public
RecyclerView.ViewHolder
onCreateViewHolder(ViewGroup
parent,
int
viewType)
{
View
view
=
LayoutInflater.from(MainActivity.this).inflate(R.layout.item_recyclerview,
parent,
false);
return
new
MyViewHolder(view);
}
@Override
public
void
onBindViewHolder(RecyclerView.ViewHolder
holder,
int
position)
{
MyViewHolder
myViewHolder
=
(MyViewHolder)
holder;
myViewHolder.checkBox.setText(content.get(position));
}
@Override
public
int
getItemCount()
{
content
=
new
ArrayList<>();
for
(int
i
=
0;
i
<
100;
i++)
{
content.add("CheckBox"
+
i);
}
return
content.size();
}
}
private
class
MyViewHolder
extends
RecyclerView.ViewHolder
{
private
CheckBox
checkBox;
public
MyViewHolder(View
itemView)
{
super(itemView);
checkBox
=
(CheckBox)
itemView.findViewById(R.id.id_check_box);
}
}
}public
class
MainActivity
extends
AppCompatActivity
{
private
RecyclerView
recyclerView;
private
MyAdapter
myAdapter;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView
=
(RecyclerView)
findViewById(R.id.id_recycler_view);
myAdapter
=
new
MyAdapter();
recyclerView.setLayoutManager(new
LinearLayoutManager(this));
recyclerView.setAdapter(myAdapter);
}
private
class
MyAdapter
extends
RecyclerView.Adapter
{
private
List<String>
content;
private
boolean[]
flag
=
new
boolean[100];//此处添加一个boolean类型的数组
@Override
public
RecyclerView.ViewHolder
onCreateViewHolder(ViewGroup
parent,
int
viewType)
{
View
view
=
LayoutInflater.from(MainActivity.this).inflate(R.layout.item_recyclerview,
parent,
false);
return
new
MyViewHolder(view);
}
@Override
public
void
onBindViewHolder(RecyclerView.ViewHolder
holder,
final
int
position)
{
MyViewHolder
myViewHolder
=
(MyViewHolder)
holder;
myViewHolder.checkBox.setText(content.get(position));
myViewHolder.checkBox.setOnCheckedChangeListener(null);//先设置一次CheckBox的选中监听器,传入参数null
myViewHolder.checkBox.setChecked(flag[position]);//用数组中的值设置CheckBox的选中状态
//再设置一次CheckBox的选中监听器,当CheckBox的选中状态发生改变时,把改变后的状态储存在数组中
myViewHolder.checkBox.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener()
{
@Override
public
void
onCheckedChanged(CompoundButton
compoundButton,
boolean
b)
{
flag[position]
=
b;
}
});
}
@Override
public
int
getItemCount()
{
content
=
new
ArrayList<>();
for
(int
i
=
0;
i
<
100;
i++)
{
content.add("CheckBox"
+
i);
}
return
content.size();
}
}
private
class
MyViewHolder
extends
RecyclerView.ViewHolder
{
private
CheckBox
checkBox;
public
MyViewHolder(View
itemView)
{
super(itemView);
checkBox
=
(CheckBox)
itemView.findViewById(R.id.id_check_box);
}
}
}<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/id_select_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="SelectAll"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>public
class
MainActivity
extends
AppCompatActivity
{
private
CheckBox
selectAll;
private
RecyclerView
recyclerView;
private
MyAdapter
myAdapter;
private
boolean
[]flag;//把flag数组定义为全局变量
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectAll
=
(CheckBox)
findViewById(R.id.id_select_all);
recyclerView
=
(RecyclerView)
findViewById(R.id.id_recycler_view);
flag
=
new
boolean[100];//初始化flag
myAdapter
=
new
MyAdapter();
recyclerView.setLayoutManager(new
LinearLayoutManager(this));
recyclerView.setAdapter(myAdapter);
//设置外面CheckBox的选中监听器,把它的选中状态赋值给其他的所有CheckBox,然后更新RecyclerView的Adapter
selectAll.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener()
{
@Override
public
void
onCheckedChanged(CompoundButton
compoundButton,
boolean
b)
{
for
(int
i
=
0;
i
<
100;
i++)
{
flag[i]
=
b;
}
myAdapter.notifyDataSetChanged();
}
});
}
private
class
MyAdapter
extends
RecyclerView.Adapter
{
private
List<String>
content;
//
private
boolean[]
flag
=
new
boolean[100];
@Override
public
RecyclerView.ViewHolder
onCreateViewHolder(ViewGroup
parent,
int
viewType)
{
View
view
=
LayoutInflater.from(MainActivity.this).inflate(R.layout.item_recyclerview,
parent,
false);
return
new
MyViewHolder(view);
}
@Override
public
void
onBindViewHolder(RecyclerView.ViewHolder
holder,
final
int
position)
{
MyViewHolder
myViewHolder
=
(MyViewHolder)
holder;
myViewHolder.checkBox.setText(content.get(position));
myViewHolder.checkBox.setOnCheckedChangeListener(null);
myViewHolder.checkBox.setChecked(flag[position]);
myViewHolder.checkBox.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener()
{
@Override
public
void
onCheckedChanged(CompoundButton
compoundButton,
boolean
b)
{
flag[position]
=
b;
}
});
}
@Override
public
int
getItemCount()
{
content
=
new
ArrayList<>();
for
(int
i
=
0;
i
<
100;
i++)
{
content.add("CheckBox"
+
i);
}
return
content.size();
}
}
private
class
MyViewHolder
extends
RecyclerView.ViewHolder
{
private
CheckBox
checkBox;
public
MyViewHolder(View
itemView)
{
super(itemView);
checkBox
=
(CheckBox)
itemView.findViewById(R.id.id_check_box);
}
}
}<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/id_check_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="CheckBox"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="delete"/>
</RelativeLayout>private
class
MyAdapter
extends
RecyclerView.Adapter
{
//
private
List<String>
content;
//
private
boolean[]
flag
=
new
boolean[100];
@Override
public
RecyclerView.ViewHolder
onCreateViewHolder(ViewGroup
parent,
int
viewType)
{
View
view
=
LayoutInflater.from(MainActivity.this).inflate(R.layout.item_recyclerview,
parent,
false);
return
new
MyViewHolder(view);
}
@Override
public
void
onBindViewHolder(RecyclerView.ViewHolder
holder,
final
int
position)
{
final
MyViewHolder
myViewHolder
=
(MyViewHolder)
holder;
myViewHolder.checkBox.setText(content.get(position));
myViewHolder.checkBox.setOnCheckedChangeListener(null);
myViewHolder.checkBox.setChecked(flag[position]);
myViewHolder.checkBox.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener()
{
@Override
public
void
onCheckedChanged(CompoundButton
compoundButton,
boolean
b)
{
flag[position]
=
b;
}
});
//设置监听器,当按钮被点击是,删除它所在的item
myViewHolder.button.s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论