如何更改按钮图像,如切换按钮 RecyclerView 按钮以编程方式单击
How to change button Image like toggle button RecyclerView button click programmatically
我有一个 RecyclerView
,其中填充了 ListView
。在每个 ListView
上都有一个 Button
,向上添加列表。
这是按钮未按下时的样子
这是按下按钮时的样子,
如何让每个 ListView
都恭敬地按下按钮?
这是我的适配器
public class BuildCustomAdapter extends RecyclerView.Adapter<BuildCustomAdapter.MyViewHolder> implements Filterable {
private int previousTotal = 0;
private boolean loading = true;
private List<Build> buildList;
private List<Build> buildListCopy;
private ItemFilter mFilter = new ItemFilter();
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.build_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Build build = buildList.get(position);
holder.imageView.setImageResource(build.getImages());
holder.name.setText(build.getName());
}
@Override
public int getItemCount() {
return buildList.size();
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView name;
public Button button;
public MyViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.ivPerson);
name = (TextView) itemView.findViewById(R.id.tvPersonName);
button = (Button) itemView.findViewById(R.id.addbn);
}
}
private class ItemFilter extends Filter {
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
List<Build> filterList = new ArrayList<Build>();
for (int i = 0; i < buildList.size(); i++) {
if ((buildList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Build builddata = new Build(buildList.get(i).getImages(), buildList.get(i).getName());
filterList.add(builddata);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = buildList.size();
results.values = buildList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
buildList = (ArrayList<Build>) results.values;
notifyDataSetChanged();
}
}
public BuildCustomAdapter(List<Build> buildList) {
this.buildList = buildList;
}
}
Build.class
public class Build {
private String name;
private int images;
public Build(int images, String name) {
this.images = images;
this.name = name;
}
public int getImages() {
return images;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
勾选此项为您的可绘制对象提供状态(选中、激活等):
https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
此外,ListView 有一个 choiceMode 可供您使用。
https://developer.android.com/reference/android/widget/AbsListView.html#attr_android:choiceMode
转到您的布局 R.layout.build_list_row
并将背景设置为您要执行此操作的按钮,并使用可绘制的选择器并设置您的加号和打勾图像,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_tick" android:state_selected="true" />
<item android:drawable="@drawable/ic_plus" />
</selector>
如果你使用 selected 作为布尔变量到你的 pojo class 中会更好,这将有助于适配器根据状态设置图像。实际上,每当你向上和向下滚动时,不可见的项目就会从内存中移除,并且我们已经根据按钮事件更新了我们的列表,所以第二次它会获取所需的状态并再次显示之前的状态图像。
BuildCustomAdapter 每当您创建适配器时始终传递上下文,上下文将帮助您
Context context;
public BuildCustomAdapter(List<Build> buildList,Context context) {
this.buildList = buildList;
this.context = context;
}
Build.java
public class Build {
private String name;
private int images;
private boolean selected;
public Build(boolean selected, String name,int images) {
this.images = images;
this.selected=selected;
this.name = name;
}
public int getImages() {
return images;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setSeleted(boolean selected){
this.selected=selected;
}
public boolean isSelected(){
return selected;
}
}
你的适配器
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Build build = buildList.get(position);
holder.imageView.setImageResources(build.getImages());
holder.name.setText(build.getName());
setImageIntoButton(holder.button,build.isSelected());
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
build.setSelected(!build.isSelected());
setImageIntoButton(holder.button,build.isSelected());
}
});
}
private void setImageIntoButton(Button buttonView,boolean isSelected){
if(isSelected)
buttonView.setBackgroundResource(R.drawable.clicked_image);
else
buttonView.setBackgroundResource(R.drawable.cross_image);
}
项目过滤器
更改此声明
Build builddata = new Build(buildList.get(i).isSelected(), buildList.get(i).getName(),buildList.get(i).getImages());
我有一个 RecyclerView
,其中填充了 ListView
。在每个 ListView
上都有一个 Button
,向上添加列表。
这是按钮未按下时的样子
这是按下按钮时的样子,
如何让每个 ListView
都恭敬地按下按钮?
这是我的适配器
public class BuildCustomAdapter extends RecyclerView.Adapter<BuildCustomAdapter.MyViewHolder> implements Filterable {
private int previousTotal = 0;
private boolean loading = true;
private List<Build> buildList;
private List<Build> buildListCopy;
private ItemFilter mFilter = new ItemFilter();
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.build_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Build build = buildList.get(position);
holder.imageView.setImageResource(build.getImages());
holder.name.setText(build.getName());
}
@Override
public int getItemCount() {
return buildList.size();
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView name;
public Button button;
public MyViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.ivPerson);
name = (TextView) itemView.findViewById(R.id.tvPersonName);
button = (Button) itemView.findViewById(R.id.addbn);
}
}
private class ItemFilter extends Filter {
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
List<Build> filterList = new ArrayList<Build>();
for (int i = 0; i < buildList.size(); i++) {
if ((buildList.get(i).getName().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Build builddata = new Build(buildList.get(i).getImages(), buildList.get(i).getName());
filterList.add(builddata);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = buildList.size();
results.values = buildList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
buildList = (ArrayList<Build>) results.values;
notifyDataSetChanged();
}
}
public BuildCustomAdapter(List<Build> buildList) {
this.buildList = buildList;
}
}
Build.class
public class Build {
private String name;
private int images;
public Build(int images, String name) {
this.images = images;
this.name = name;
}
public int getImages() {
return images;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
勾选此项为您的可绘制对象提供状态(选中、激活等):
https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
此外,ListView 有一个 choiceMode 可供您使用。
https://developer.android.com/reference/android/widget/AbsListView.html#attr_android:choiceMode
转到您的布局 R.layout.build_list_row
并将背景设置为您要执行此操作的按钮,并使用可绘制的选择器并设置您的加号和打勾图像,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_tick" android:state_selected="true" />
<item android:drawable="@drawable/ic_plus" />
</selector>
如果你使用 selected 作为布尔变量到你的 pojo class 中会更好,这将有助于适配器根据状态设置图像。实际上,每当你向上和向下滚动时,不可见的项目就会从内存中移除,并且我们已经根据按钮事件更新了我们的列表,所以第二次它会获取所需的状态并再次显示之前的状态图像。
BuildCustomAdapter 每当您创建适配器时始终传递上下文,上下文将帮助您
Context context;
public BuildCustomAdapter(List<Build> buildList,Context context) {
this.buildList = buildList;
this.context = context;
}
Build.java
public class Build {
private String name;
private int images;
private boolean selected;
public Build(boolean selected, String name,int images) {
this.images = images;
this.selected=selected;
this.name = name;
}
public int getImages() {
return images;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setSeleted(boolean selected){
this.selected=selected;
}
public boolean isSelected(){
return selected;
}
}
你的适配器
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Build build = buildList.get(position);
holder.imageView.setImageResources(build.getImages());
holder.name.setText(build.getName());
setImageIntoButton(holder.button,build.isSelected());
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
build.setSelected(!build.isSelected());
setImageIntoButton(holder.button,build.isSelected());
}
});
}
private void setImageIntoButton(Button buttonView,boolean isSelected){
if(isSelected)
buttonView.setBackgroundResource(R.drawable.clicked_image);
else
buttonView.setBackgroundResource(R.drawable.cross_image);
}
项目过滤器 更改此声明
Build builddata = new Build(buildList.get(i).isSelected(), buildList.get(i).getName(),buildList.get(i).getImages());