2016-07-29 72 views
1

我有customrecycleAdapter,當我點擊元素我會改變顏色的行,但如果我滾動視圖背景更改位置或出現在其他元素。 我已經閱讀了關於Receycle的觀點,但是,對於這個問題什麼是workoround?用滾動RecycleAdapter重複元素的背景和滾動

我的適配器是:

public class CustomRecycleAdapter extends RecyclerView.Adapter<CustomRecycleAdapter.ViewHolder> { 
    private JSONArray dataSource; 
    private AdapterCallback mAdapterCallback; 
    public static SparseBooleanArray selectedItems; 

    public CustomRecycleAdapter(JSONArray dataArgs, Context context){ 
     dataSource = dataArgs; 
     this.mAdapterCallback = ((AdapterCallback) context); 


    } 



    public static interface AdapterCallback { 
     void onMethodCallback(View caller, JSONObject obj, JSONArray data, int position); 
    } 


    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     // create a new view 
     View view = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.list_view_activities, parent, false); 

     ViewHolder viewHolder = new ViewHolder(view, new CustomRecycleAdapter.ViewHolder.IMyViewHolderClicks() { 
      public void onClick(View caller, int position) { 
       try { 

        JSONObject itemClicked = dataSource.getJSONObject(position); 
        mAdapterCallback.onMethodCallback(caller, itemClicked, dataSource, position); 
//     Log.e("clicked", itemClicked.toString()); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      }; 
     }); 

     return viewHolder; 

    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     try { 

      JSONObject object = dataSource.getJSONObject(position); 
      holder.textView.setText(object.getString("description")); 





     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 
    @Override 
    public int getItemCount() { 
     return dataSource.length(); 
    } 

    public JSONObject getItem(int position) throws JSONException { 
     return dataSource.getJSONObject(position); 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 

     public IMyViewHolderClicks mListener; 
     public TextView textView; 
     public TextView hours; 

     public ViewHolder(View itemView, IMyViewHolderClicks listener) { 
      super(itemView); 
      mListener = listener; 
      textView = (TextView) itemView.findViewById(R.id.activity); 
      hours = (TextView) itemView.findViewById(R.id.hours); 
      itemView.setOnClickListener(this); 

     } 


     @Override 
     public void onClick(View v) { 
      int position = getAdapterPosition(); 
      Log.e("RecycleAdapterPosition", String.valueOf(getAdapterPosition())); 
//   mListener.onClick(v, position); 
      v.setBackgroundColor(Color.GREEN); 
      v.setSelected(true); 


     } 
     public interface IMyViewHolderClicks { 


      public void onClick(View caller, int position); 
     } 
    } 




} 

所以我點擊元素,並corretly的setBackground,但如果我滾動,我看到背景另一元素設置好的,如果我滾動了我的元素,我選擇的,避風港背景。

+0

這是模型和視圖的分離的益處。點擊時,控制器應更新模型(即數組中的索引),然後更新後的模型應觸發視圖層(即適配器)的更新,然後使用該模型(顏色值)呈現適當的背景顏色。 – FishStix

回答

0

RecyclerView重複使用該視圖來繪製將顯示的列表中的下一個元素,因此具有新數據的視圖集繼續具有舊樣式,因爲您可以使用新顏色查看列表的其他元素。

應創建一個數組,如果該元件被選中或不存儲,並將它們是否被選中時onBindViewHolder()稱爲

@Override 
public void onBindViewHolder(final YourViewHolder holder, final int position) { 

    if(checked[position]){ 
     //YourViewHolder change colour 

    } 

}