3

I am trying to achieve an infinite scroll functionality of finite items in both directions of a using RecyclerView Horizontally.

I have a List of items = 20, Now my RecyclerView shows item, 0,1,2,3....20. with visible count = 6 items

Till now for the infinite trick, I have used this sample, below

  mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        int visibleItemCount = mLayoutManager.getChildCount();
        int totalItemCount = mLayoutManager.getItemCount();
        int firstVisibleItemPosition = mLayoutManager.findFirstVisibleItemPosition();
        if (firstVisibleItemPosition + visibleItemCount >= totalItemCount) {
            //End of list --- Scrolling LEFT TO RIGHT
            recyclerView.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2);
        }
    }

This works great,

eg: 0,1,2...20,0,1,2

INFINITE but only Scrolling from Left to Right

Now when I swipe back from Right to Left, It stops at Oth, which is, I am trying to solve

eg: infinite....19 20 0 1 2 ...infinite

Any citation will be highly appreciated. PS: Please do ask for more context.

Sukhbir
  • 1,410
  • 14
  • 33
Avinash
  • 145
  • 9
  • Will [This](https://stackoverflow.com/questions/31253555/how-do-i-create-a-circular-endless-recyclerview) help? – ADM Jul 27 '18 at 07:50
  • Hi ADM, that does not work in both side infinite for horizontal recyclerview – Avinash Jul 27 '18 at 08:28

1 Answers1

0

i made this calendar that has infinite horizontal scrolling, (its not the smoothest but it does work) maybe you can use it

class Calendar : ReusableMenuActivity(), CalendarAdapter.OnDayClickListener, CalendarDayAdapter.OnButtonClickListener, AdapterView.OnItemSelectedListener{
private lateinit var binding: ActivityCalendarBinding
private val c = CalendarData.getInstance()
var year = c.get(CalendarData.YEAR)
var dateOffset = c.get(CalendarData.MONTH)
private val months = mutableListOf("January","February","March","April","May","June","July","August","September","October","November","December")
private val notifications = mutableListOf(
        NotificationSaveData(2022,1,1, mutableListOf(NotificationData("test1",RED,"belgium","go to doctor", Time_hour_minutes(2,2)), NotificationData("test3",GREEN,"belgium","take a piss", Time_hour_minutes(1,1)),NotificationData("test4",GREEN,"russia","invade", Time_hour_minutes(1,1)),NotificationData("test5",GREEN,"belgium","touch grass", Time_hour_minutes(1,1)),NotificationData("test6",GREEN,"","", Time_hour_minutes(1,1)),NotificationData("very long name test aaaaaaaaaaa",GREEN,"very long name test aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","very long name test aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Time_hour_minutes(1,1)))),
        NotificationSaveData(2022,4,1, mutableListOf(NotificationData("test1",RED,"belgium","this is red", Time_hour_minutes(1,1)), NotificationData("test2", BLUE,"belgium","this is blue", Time_hour_minutes(1,1)))),
        NotificationSaveData(2022,2,27, mutableListOf(NotificationData("test1", YELLOW,"belgium","this is yellow", Time_hour_minutes(1,1)), NotificationData("cyan",CYAN,"belgium","this is cyan", Time_hour_minutes(1,1))))
)
private var layoutManager : LinearLayoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false)
val month1 = if(dateOffset>1){ dateOffset-1 } else{ 11 }
val month2 = if(dateOffset<11){ dateOffset+1 } else{ 0 }
val month3 = if(dateOffset<10){ dateOffset+2 } else if (dateOffset<11){ 0 } else{ 1 }
val year1 = if(dateOffset<11){ year } else{ year-1 }
val year2 = if(dateOffset<11){ year } else{ year+1 }
val year3 = if(dateOffset<10){ year } else if (dateOffset<11){ year+1 } else{ year+1 }


private var CalenderItems = mutableListOf(
    CalendarClass(months[month1]+" "+year1 ,year1, (month1)+1,notifications),
    CalendarClass(months[dateOffset]+" "+year ,year, (dateOffset)+1,notifications),
    CalendarClass(months[month2]+" "+year2 ,year2, (month2)+1,notifications),
    CalendarClass(months[month3]+" "+year3 ,year3, (month3)+1,notifications),
)

private val adapter = CalendarAdapter(CalenderItems,this,this)
var calendarPreviousSide = true
var noteRepeating = "once"
var currentMonth = dateOffset
var currentYear = year
var currentday = 1
var toast : Toast? = null
var lastTime = System.currentTimeMillis()
var selectedPosition = 0
var selectedDataPosition = 0
var removed = false

var picker: TimePickerDialog? = null
var eText: EditText? = null
var locationText : EditText? = null
var dateText : TextView? = null
var noteText : EditText? = null
var title: EditText? = null
var calendar_note: View? = null
var addNoteBtn: ImageButton? = null
var editNoteBtn: ImageButton? = null
var removeNoteBtn: ImageButton? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityCalendarBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val toolbar: Toolbar = findViewById<View>(R.id.custom_toolbar) as Toolbar
    setSupportActionBar(toolbar)
    supportActionBar?.setDisplayShowTitleEnabled(false)

    val helper: SnapHelper = LinearSnapHelper()
    helper.attachToRecyclerView(binding.Recyclerview)
    adapter.setHasStableIds(true)
    binding.Recyclerview.adapter = adapter
    binding.Recyclerview.layoutManager = layoutManager
    binding.Recyclerview.scrollToPosition(1)
    binding.Recyclerview.itemAnimator = null
    binding.Recyclerview.setItemViewCacheSize(3)
    adapter.notifyItemRangeChanged(0, CalenderItems.size)


    binding.Recyclerview.addOnScrollListener(object : RecyclerView.OnScrollListener(){
        override fun onScrolled(recyclerView: RecyclerView,dx: Int,dy :Int){
            if(dx>0 || dx<0)
            {
                val visibleItemCount = layoutManager.childCount
                val lastVisibleItem = layoutManager.findLastVisibleItemPosition()
                val pastVisibleItem = layoutManager.findFirstCompletelyVisibleItemPosition()
                val total = adapter.itemCount
                year=c.get(CalendarData.YEAR)

                if(visibleItemCount+pastVisibleItem >= total && dx>0){
                    if(calendarPreviousSide) {
                        dateOffset += 3
                        calendarPreviousSide=!calendarPreviousSide
                    }
                    dateOffset++
                    var month = dateOffset

                    while(month>12){
                        month -= 12
                        year++
                        if(month<13){
                            break
                        }
                    }
                    while(month<1){
                        month+=12
                        year--
                        if(month>0){
                            break
                        }
                    }
                    recyclerView.post {
                        CalenderItems.add(CalendarClass(months[month-1]+" "+year.toString(),year,month,notifications))
                        CalenderItems.removeAt(0)
                        adapter.notifyItemRangeChanged(0, CalenderItems.size)
                        binding.Recyclerview.scrollToPosition(CalenderItems.size-2)
                    }

                }
                else if(lastVisibleItem == 1 && dx<0){
                    if(!calendarPreviousSide) {
                        dateOffset -= 3
                        calendarPreviousSide=!calendarPreviousSide
                    }
                    dateOffset--
                    var month = dateOffset
                    while(month>12){
                        month -= 12
                        year++
                        if(month<13){
                            break
                        }
                    }
                    while(month<1){
                        month+=12
                        year--
                        if(month>0){
                            break
                        }
                    }
                    recyclerView.post {
                        CalenderItems.add(0,CalendarClass(months[month-1]+" "+year.toString(),year,month,notifications))
                        CalenderItems.removeAt(CalenderItems.size-1)
                        adapter.notifyItemRangeChanged(0, CalenderItems.size)
                        binding.Recyclerview.scrollToPosition(2)
                    }
                }
            }
            super.onScrolled(recyclerView, dx, dy)

        }
    })