2016-09-26 168 views
1

我有RecyclerView填充ListView。在每個ListView有一個Button,它增加了列表。如何更改按鈕圖像像切換按鈕RecyclerView按鈕以編程方式點擊


這裏如果沒有按它

enter image description here


這裏是被按下時,該按鈕的外觀按鈕的外觀,

enter image description here


我該如何保留but噸每個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; 
    } 
} 

回答

1

這將是如果你使用選擇作爲布爾變量到你的POJO類中,它將有助於適配器根據狀態設置圖像。實際上,每當你上下滾動顯示不可見的項目時,都會從內存中刪除,而且我們已經根據按鈕事件更新了列表,所以第二次它將獲取所需狀態並再次顯示以前的狀態圖像。

BuildCustomAdapter通方面一直當你創建一個適配器,背景會幫助你

Context context; 
    public BuildCustomAdapter(List<Build> buildList,Context context) { 
      this.buildList = buildList; 
      this.context = context; 
     } 

生成。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()); 
+0

經過這段代碼,你不需要使用'''build = new Build(R.drawable.ak,「Aksahy Kumar」);''' –

+0

嗨Abdul Rizwan在我的情況下我沒有把' public build布爾選擇,字符串名稱){ //this.images = images; this.selected =選中; this.name = name; }'在活動類'build = new Build(R.drawable.ak,「Aksahy Kumar」); buildList.add(build);' – Joy

+0

我已更新我的代碼。只是看看。 –

0

轉到您佈局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> 
+0

這種方法是合適的,但我必須做編程! – Joy

+0

在你的方案設置選擇器與XML是考慮作爲一種最好的辦法..但如果要切換編程比你可以執行使用如果其他條件,我不會建議 –

+0

好吧沒問題布爾漢丁Rashid! – Joy