1

I have customrecycleAdapter and when I click on element i would change color of row, but if I scroll view background change position or appear on other element. I have read about Receycle of view, but so, What is workoround for this problem?

my adapter is:

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




}

So I click on element, and corretly setBackground, but if I scroll, I see another element with background setted and if I scroll up my element, that i selected, haven't background.

LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89
  • This is a benefit of separation of model and view. When you click, a controller should update the model (i.e. index in an array), then the updated model should trigger an update to the view layer (i.e. adapter) which can then use the model (color value) to render the proper background color. – FishStix Jul 29 '16 at 17:00

1 Answers1

0

RecyclerView reuse the view to paint the next elements of your list which will be shown, so the viewset with new data but continue having the old styles because of that you see other element of your list with the new colour.

You should create an array storing if this element is checked or not and them check if was checked when onBindViewHolder() is called

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

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

    }

}
Miguel Benitez
  • 2,322
  • 10
  • 22