从活动而不是从适配器更改recyclerview中所有textviews的背景

时间:2018-09-20 13:05:53

标签: android-recyclerview

我有一个带有textview的recyclerview,单击的textview周围有一个背景。我在活动中也有一个按钮,该按钮应该为recyclerview中的所有项目放一个背景,即显示所有选中的内容。输入我的活动和适配器代码。请帮助我

我的活动代码

set_all_days.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CommonUtility.selectedDays.clear();
            CommonUtility.selectedDays.addAll(daysList);
            //loadDays();


        }
    });

我的适配器

public  class DaysAdapter<T> extends RecyclerView.Adapter<DaysAdapter.ViewHolder> {
private ArrayList<String> list=new ArrayList<>();
private Context mContext;
private RecyclerViewClickListener mListener;

public DaysAdapter(ArrayList<String> data, Context context, RecyclerViewClickListener listener) {
    list = data;
    //languageListFiltered = filteredData;
    mContext = context;
    mListener = listener;
}

@Override
public void onBindViewHolder(DaysAdapter.ViewHolder viewHolder, final int position) {
    viewHolder.titleTextView.setText(list.get(position));
   // viewHolder.lang_radio.setChecked(position == mSelectedItem);
}

@Override
public int getItemCount() {
    return list.size();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    final View view = inflater.inflate(R.layout.days_list_item, viewGroup, false);
    return new ViewHolder(view);
}

class ViewHolder extends RecyclerView.ViewHolder {

    public TextView titleTextView;

    public ViewHolder(final View inflate) {
        super(inflate);
        titleTextView = (TextView)inflate.findViewById(R.id.text_day);
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!CommonUtility.selectedDays.contains(titleTextView.getText().toString()))
                {
                    titleTextView.setBackground(mContext.getResources().getDrawable(R.drawable.green_circle));
                    titleTextView.setTextColor(mContext.getResources().getColor(R.color.white));
                    CommonUtility.selectedDays.add(titleTextView.getText().toString());
                    Log.d("added",titleTextView.getText().toString());
                }
                else
                {
                    int index=CommonUtility.selectedDays.indexOf(titleTextView.getText().toString());
                    CommonUtility.selectedDays.remove(index);
                    titleTextView.setBackground(null);
                    Log.d("removed",titleTextView.getText().toString());
                    titleTextView.setTextColor(mContext.getResources().getColor(R.color.black));

                }


            }
        };


        itemView.setOnClickListener(clickListener);
        titleTextView.setOnClickListener(clickListener);
    }
}

}

0 个答案:

没有答案