使用滚动RecycleAdapter重复元素上的​​背景并滚动 [英] Repeat background on element with scroll RecycleAdapter and scroll

查看:57
本文介绍了使用滚动RecycleAdapter重复元素上的​​背景并滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有customrecycleAdapter,当我单击元素时,我会更改行的颜色,但是如果我滚动查看背景更改位置或出现在其他元素上. 我已经阅读过有关Receycle的文章,但是,解决此问题的方法是什么?

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?

我的适配器是:

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




}

因此,我单击了element并正确设置了setBackground,但是如果滚动,则会看到另一个设置了背景的元素,并且如果我向上滚动所选的元素,则没有背景.

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.

推荐答案

RecyclerView重用视图绘制将要显示的列表的下一个元素,因此视图集具有新数据,但由于该原因而继续具有旧样式查看带有新颜色的列表中的其他元素.

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.

您应该创建一个数组,用于存储是否选中此元素,并检查是否在调用onBindViewHolder()时选中了

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

    }

}

这篇关于使用滚动RecycleAdapter重复元素上的​​背景并滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆