Home > Back-end >  ScrollView : Infinity scrolling with squares background
ScrollView : Infinity scrolling with squares background

Time:08-01

I need to make my ScrollView infinity scrolling with squares background. Something like A tileable grid image

tileable.png

(That's 24px x 24px, or 24dp x 24dp at the base LDPI)

tiled.xml (in drawables)

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/tileable"
    android:tileMode="repeat"
    android:antialias="false"
    android:filter="false"
    />

(it's vertical and horizontal lines so we don't want any smoothing/filtering when it's scaled up for higher DPIs)

Then you can use that as a background in a View and it'll tile. If you want a View you can scroll infinitely for some reason, you can do it with a RecyclerView:

item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="96dp"
    android:background="@drawable/tiled" />

(Height is a multiple of the 24dp size of the tiled image so it repeats properly)

class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
        ItemViewBinding.inflate(LayoutInflater.from(parent.context), parent, false).run(::ViewHolder)

    override fun onBindViewHolder(holder: ViewHolder, position: Int) { }

    // not literally infinite scrolling but they'll have to work to crash it
    override fun getItemCount(): Int = Int.MAX_VALUE

    inner class ViewHolder(binding: ItemViewBinding) : RecyclerView.ViewHolder(binding.root)

}

Stick that adapter in a RecyclerView and you have a scrolling grid

recyclerView.adapter = MyAdapter()
recyclerView.layoutManager = LinearLayoutManager(requireContext())

A RecyclerView looking like a big scrolling grid

I'm not sure why you'd want to do this, but there you go! If you're trying to create a scrollable field containing things you can interact with (like the node editor example you posted) that's a whole other thing, where the "infiniteness" is more about the widget's representation of space and its contents, and painting a tiled background that moves as you navigate that component's internal space would be the simplest part of that system.

  • Related