gpt4 book ai didi

java - 如何更改按钮图像,如切换按钮 RecyclerView 按钮以编程方式单击

转载 作者:行者123 更新时间:2023-11-29 19:32:36 25 4
gpt4 key购买 nike

我有一个 RecyclerView,其中填充了 ListView。在每个 ListView 上都有一个 Button,用于添加列表。


这是按钮未按下时的样子

enter image description here


这是按下按钮时的样子,

enter image description here


如何让每个 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;
}
}

最佳答案

如果你在 pojo 类中使用 selected 作为 boolean 变量会更好,它会帮助适配器根据状态设置图像。实际上,每当你向上和向下滚动时,不可见的项目就会从内存中移除,并且我们已经根据按钮事件更新了我们的列表,所以第二次它会获取所需的状态并再次显示之前的状态图像。

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());

关于java - 如何更改按钮图像,如切换按钮 RecyclerView 按钮以编程方式单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39697983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com