Android Recycler视图:如何在选择一个项目时更改多个项目的颜色

时间:2017-07-18 08:44:23

标签: android android-recyclerview

我在使用recyclerview时遇到问题。把它放在这里的主要原因是我正在制作一个自定义日历。作为其中的一部分,我有两个recyclerviews父母和孩子(嵌套Recyclerviews).Parent用于上市月,Child用于列出相应月份的日期。 我需要从日历中选择多个日期。 This screenshot is the current status I am getting
我想在所选日期之间将所有日期的背景颜色设置为白色。 请帮忙。?

import android.graphics.Color;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.webandcrafts.firebasedemoproject.R;
import com.webandcrafts.firebasedemoproject.model.CalendarModel;
import com.webandcrafts.firebasedemoproject.model.CalendarMonth;

import java.util.Calendar;
import java.util.List;
import java.util.StringTokenizer;



public class CalendarDatesAdapter extends RecyclerView.Adapter<CalendarDatesAdapter.MyViewHolder> {

    private List<CalendarMonth> mCalendarMonthList;
    private int mCurrentDate;
    private int mCurrentMonthIndex;
    private int mCurrentYear;
    private int monthIndex;
    private int dateSelectedCount = 0;

    public CalendarDatesAdapter(List<CalendarMonth> calendarMonthList, int currentDate, int monthIndex) {

        this.mCalendarMonthList = calendarMonthList;
        this.mCurrentDate = currentDate;
        this.monthIndex = monthIndex;


    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.calendar_month_item, parent, false);


        return new MyViewHolder(itemView);

    }

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

        final CalendarMonth calendarMonth = mCalendarMonthList.get(position);
        holder.textViewDate.setText(calendarMonth.getDate());
        holder.textViewAmount.setText(calendarMonth.getAmount());


        holder.linearLayoutDateItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                dateSelectedCount++;

                calendarMonth.setDateSelected(true);
                holder.textViewDate.setTextColor(Color.BLACK);
                holder.textViewAmount.setTextColor(Color.BLACK);
                holder.linearLayoutDateItem.setBackgroundColor(Color.WHITE);
                calendarMonth.setDateSelectedCount(dateSelectedCount);


            }


        });


        if (!calendarMonth.isDateSelected()) {

            if (calendarMonth.isDisabledDate() && monthIndex == 0) {


                holder.textViewDate.setTextColor(Color.GRAY);
                holder.textViewAmount.setTextColor(Color.GRAY);
                holder.linearLayoutDateItem.setClickable(false);


            } else if (calendarMonth.isDisabledDate() == false && monthIndex == 12) {


                if (position >= mCurrentDate) {

                    holder.textViewDate.setTextColor(Color.GRAY);
                    holder.textViewAmount.setTextColor(Color.GRAY);
                    holder.linearLayoutDateItem.setClickable(false);

                }


            } else {

                if (calendarMonth.getDate().equals("") && calendarMonth.getAmount().equals("")) {

                    holder.linearLayoutDateItem.setClickable(false);
                }

            }


        } else if (calendarMonth.isDateSelected()) {

            // If date is selected

            calendarMonth.setDateSelected(true);
            holder.textViewDate.setTextColor(Color.BLACK);
            holder.textViewAmount.setTextColor(Color.BLACK);
            holder.linearLayoutDateItem.setBackgroundColor(Color.WHITE);
            calendarMonth.setDateSelectedCount(dateSelectedCount);


        }


    }


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

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewDate;
        public TextView textViewAmount;
        public LinearLayout linearLayoutDateItem;

        public MyViewHolder(View itemView) {
            super(itemView);
            textViewDate = (TextView) itemView.findViewById(R.id.textViewDate);
            textViewAmount = (TextView) itemView.findViewById(R.id.textViewAmount);
            linearLayoutDateItem = (LinearLayout) itemView.findViewById(R.id.linearLayoutDateItem);


        }
    }

这是我的适配器代码。 我需要在screenshot

中突出显示或设置所选日期范围从18到26的项目的背景颜色

0 个答案:

没有答案