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

时间:2016-09-26 08:29:12

标签: java android listview android-recyclerview android-button

我的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;
    }
}

3 个答案:

答案 0 :(得分:2)

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

每当您创建适配器时,

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;
        } 
    }

<强> YourAdapter

@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); 
 }

<强> ItemFilter 改变这个陈述

Build builddata = new Build(buildList.get(i).isSelected(), buildList.get(i).getName(),buildList.get(i).getImages());

答案 1 :(得分:0)

选中此项以使您的可绘制状态(选中,激活等):

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

答案 2 :(得分:0)

转到布局R.layout.build_list_row并将背景设置为您要执行此操作的按钮,并使用选择器drawable并设置加号和勾选图像,如下所示:

<?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>