Repeat background on element with scroll RecycleAdapter and scroll

我是研究僧i 提交于 2019-12-11 07:29:24

问题


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.


回答1:


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

    }

}


来源:https://stackoverflow.com/questions/38662607/repeat-background-on-element-with-scroll-recycleadapter-and-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!