使用无休止的RecyclerView作为日历

问题描述 投票:0回答:1

我目前是Android开发的新手,正在构建我的第一个应用程序。我在尝试实现“无止境回收者视图”的过程中遇到了一些棘手的问题。

我正在(当前)使用回收站视图,只是为了获取当前日期并将其显示在日历输出中。确实有两件事使我感到沮丧。其中没有太多的文档说明如何使用无数据库的无休止的回收器视图(我确实打算,但现在暂时不这样做),并且不知道在何处/如何使其无休止。我已经实现的recyclerView已经在工作,我只需要不断地加载它即可。我能找到的唯一示例本质上要求我使用数据库,或者在onCreate方法中创建一个巨大的方法。再次,我也尝试在home类中实现自己的setOnScrollListener,但是我无法使其正常工作。

下面是我脑海中所期望的结果。Link to UI picture

到目前为止是我的代码。理想情况下,我希望将滚动监听器保留在RecyclerAdapter中,因为稍后我可能会在应用程序的其他方面重用它。谢谢你。

家庭班

public class Home extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

private static final String TAG = "Home Class" ;

//Recycler View Variables and myCalendarClass Variables
private myCalendarClass myCalendarObj;
private int weeksInView = 1; //will need to make this update dynamicallly based on when a user toggles the view
private ArrayList<Calendar> calendarArrayList = new ArrayList<>();
private ArrayList<Integer> iconList = new ArrayList<>();
private ArrayList<Integer> eventCounterList = new ArrayList<>();

//recycler view dynamic loading variables
private RecyclerView calendarRecyclerView;
private boolean isLoading = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    initializeNavMenuButtons();
    setViewSpinner();

    this.myCalendarObj = new myCalendarClass();
    setAllCalendarFields(this.myCalendarObj);

    getImagesAndEventCounters();

    //Example of the order to call the methods when a user switches the view
        //setCalendarItrToCurrentSunday();
        //setCalendarArrayList();
        //getImagesAndEventCounters();
}

        //Recycler view code

//gets images and events to feed into the recycler view
private void getImagesAndEventCounters() {
    Log.d(TAG, "initImageBitMaps: called");

    ArrayList<Calendar> weekView = getCalendarArrayList();

    int daysInView = this.weeksInView * 7;

    System.out.println("Days in view " + daysInView + " Weeks In View " + this.weeksInView);

    for (int i = 0; i < daysInView; i++) {

        calendarArrayList.add(weekView.get(i));

        iconList.add(R.id.fitnessIcon);
        eventCounterList.add(R.string.XEventsDefault);

        iconList.add(R.id.educationIcon);
        eventCounterList.add(R.string.XEventsDefault);

        iconList.add(R.id.workIcon);
        eventCounterList.add(R.string.XEventsDefault);

        iconList.add(R.id.personalIcon);
        eventCounterList.add(R.string.XEventsDefault);

        initRecyclerView();
    }


}

//passes data to the parent recycler view and sets the view
private void initRecyclerView() {

         //For one recyclerView
            this.parentLayoutManager = new GridLayoutManager(this, 7);
            //GridLayoutManager layoutManager =  parentLayoutManager;
            RecyclerView recyclerView = findViewById(R.id.calendarRecyclerView);
            recyclerView.setLayoutManager(parentLayoutManager);
            RecyclerViewAdapter adapter = new RecyclerViewAdapter(this,iconList,eventCounterList,weeksInView,calendarArrayList);
            recyclerView.setAdapter(adapter);

    //ViewSpinner(Drop Down Menu)
private void setViewSpinner(){
    Spinner viewSpinner = findViewById(R.id.ViewDropDownButton);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.Calendar_View_List, android.R.layout.simple_spinner_item);

    viewSpinner.setAdapter(adapter);
    viewSpinner.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String text = parent.getItemAtPosition(position).toString();

    myCalendarClass myCalendarObj;

    if(text.equals("Week View")){
        myCalendarObj = new myCalendarClass("Week View");
        setAllCalendarFields(myCalendarObj);
        getImagesAndEventCounters();
        this.myCalendarObj = myCalendarObj;
    }
    else if (text.equals("Biweekly View")){
        myCalendarObj = new myCalendarClass("Biweekly View");
        setAllCalendarFields(myCalendarObj);
        getImagesAndEventCounters();
        this.myCalendarObj = myCalendarObj;
    }
    else if (text.equals("Month View")){
        myCalendarObj = new myCalendarClass("Month View");
        setAllCalendarFields(myCalendarObj);
        getImagesAndEventCounters();
        this.myCalendarObj = myCalendarObj;
    }


    }

@Override
public void onNothingSelected(AdapterView<?> parent) {

}


  //setters
//sets all the local variables needed from myCalendarClass
private void setAllCalendarFields(myCalendarClass c){
    this.myCalendarObj = c;
    this.calendarArrayList = c.getCalendarArrayList();
    this.weeksInView = c.getWeeksInView();
}

//getters
//returns the calendar arraylist
private ArrayList<Calendar> getCalendarArrayList(){
    return this.calendarArrayList;
}

回收器适配器

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    private static final String TAG = "RecyclerViewAdapter";

    //variables for creating the recyclerView cards
    private ArrayList<Integer> iconList;
    private ArrayList<Integer> eventCounterList;
    private Context mContext;
    private int itemCount;
    private ArrayList<Calendar> calendarArrayList;
    private ArrayList<String> dayStr;
    private ArrayList<String> dayNum;

    public RecyclerViewAdapter(Context context, ArrayList<Integer> imageList, ArrayList<Integer> eventArray, int weeksInView,ArrayList<Calendar> calendarArrayList){
        this.iconList = imageList;
        this.eventCounterList = eventArray;
        this.mContext = context;
        this.itemCount = weeksInView*7;
        this.calendarArrayList = calendarArrayList;
        setDayStrNDayNum();

    }

    @NonNull
    @Override
    //this is the method that actually inflates each individual layout
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder: called.");

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem,parent,false);

        return new ViewHolder(view);
    }

    @Override
    //this is where we bind the data to each individual list items
    //essentially all the data and stuff is actually attached in this method to each indiviual list item
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: called");

        holder.fitnessIcon.getDrawable();
        holder.educationIcon.getDrawable();
        holder.personalIcon.getDrawable();
        holder.workIcon.getDrawable();

        holder.fitnessEventCounter.setText(eventCounterList.get(position));
        holder.educationEventCounter.setText(eventCounterList.get(position));
        holder.workEventCounter.setText(eventCounterList.get(position));
        holder.personalEventCounter.setText(eventCounterList.get(position));

        holder.dayString.setText(dayStr.get(position));
        holder.dayNum.setText(dayNum.get(position));

    }

    //returns the amount of items we wish to include in the recycler view (not the individual items within a card layout
    // but instead how many cards we wish to include...probably
    @Override
    public int getItemCount() {
        return itemCount;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView fitnessIcon;
        ImageView educationIcon;
        ImageView workIcon;
        ImageView personalIcon;

        TextView dayString;
        TextView dayNum;
        TextView fitnessEventCounter;
        TextView educationEventCounter;
        TextView workEventCounter;
        TextView personalEventCounter;

        public ViewHolder(View itemView){
            super(itemView);

            //icons within the layout_listitem
            fitnessIcon = itemView.findViewById(R.id.fitnessIcon);
            educationIcon = itemView.findViewById(R.id.educationIcon);
            workIcon = itemView.findViewById(R.id.workIcon);
            personalIcon = itemView.findViewById(R.id.personalIcon);

            //text fields within the layout_listitem
            dayNum = itemView.findViewById(R.id.dayNum);
            dayString = itemView.findViewById(R.id.dayStr);
            fitnessEventCounter = itemView.findViewById(R.id.fitnessEventCounter);
            educationEventCounter = itemView.findViewById(R.id.educationEventCounter);
            workEventCounter = itemView.findViewById(R.id.workEventCounter);
            personalEventCounter = itemView.findViewById(R.id.personalEventCounter);


        }



    }

    public void setItemCount(int newItemCount){
        itemCount = newItemCount;
    }

    //sets the dayStr arrays and the dayNum array using CalendarArrayList
    // to pull the day in the month and the string value(monday,tuesday,etc.)
    private void setDayStrNDayNum(){

        Iterator<Calendar> itr = this.calendarArrayList.iterator();

        ArrayList<String> tempDayNum = new ArrayList<>();
        ArrayList<String> tempDayStr = new ArrayList<>();

        int i = 0;
        while(itr.hasNext()){
            String dayInMonth = getDayInMonth(calendarArrayList.get(i));
            String weekDayToString = weekDayToString(calendarArrayList.get(i));

            tempDayNum.add(dayInMonth);
            tempDayStr.add(weekDayToString);
            i++;
            itr.next();
        }//while

        this.dayNum = tempDayNum;
        this.dayStr = tempDayStr;

    }

    //takes in a Calendar Obj, gets the int, and returns it in a String Format
    private String getDayInMonth (Calendar c){
        Integer temp = c.get(Calendar.DAY_OF_MONTH);
        String answer = Integer.toString(temp);
        return answer;
    }

    //takes in a Calendar Obj, gets the weekday digit from 1 to 7, and returns a String
    // 1 being Su for Sunday, 2 Mo for Monday, and etc.
    private String weekDayToString (Calendar x){
        int temp = x.get(Calendar.DAY_OF_WEEK);

        switch(temp){
            case 1:
                return "Su";

            case 2:
                return "Mo";

            case 3:
                return "Tu";

            case 4:
                return "We";

            case 5:
                return "Th";

            case 6:
                return "Fr";

            case 7:
                return "Sa";

        }
        return "null";
    }//weekDayToString

}
java android android-recyclerview
1个回答
0
投票

这里有个主意:在onCreate中创建一些基本列表,其中包含围绕recyclerview当前日期的下1000个日期,并使用以下link了解如何在滚动事件之后找出您在recyclerview上的位置。一旦您靠近边缘,只需在光谱的该末端再添加1000个日期。

请看以下link,了解如何在适配器内部的recyclerview上设置onscrolllistener(基本上,您需要将recyclerview传递给适配器的构造函数)

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.