Code Example

Kotlin Android MultiSnapRecyclerView Snippets

A step by step Android MultiSnapRecyclerView example.

1. TakuSemba/MultiSnapRecyclerView

Android library for multiple snapping of RecyclerView.

Features

MultiSnapRecyclerView Example Tutorial
MultiSnapRecyclerView Example Tutorial
MultiSnapRecyclerView Example Tutorial
MultiSnapRecyclerView Example Tutorial

Make sure to use LinearManger. Other managers are not supported.

MultiSnapRecyclerView Example Tutorial

This is an Android Library for multiple snapping of RecyclerView. MultiSnapRecyclerView easily provides a snapping feature to your recycler view. What this does are clean, neat, and powerful.

  • gravitated snapping to start end and center.
  • snap count to specify the number of items to scroll over.
  • supports horizontal and vertical scrolling.
  • listener to be called when snapped.
  • sample code.
  • [ ] support reverse layout.

Make sure to use LinearManger. Other managers are not supported.

Step 1: Gradle

dependencies {
    implementation 'com.github.takusemba:multisnaprecyclerview:x.x.x'
}

MultiSnapRecyclerView Example Tutorial

Usage

There are two ways to use MultiSnapRecyclerView.

You can either use MultiSnapRecyclerView in your layout.

<com.takusemba.multisnaprecyclerview.MultiSnapRecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:msrv_gravity="start" or center, end
        app:msrv_interval="2" items to scroll over
        app:msrv_ms_per_inch="100" /> // speed of scrolling through.

Or you can simply attach MultiSnapHelper to your normal RecyclerView.

val multiSnapHelper = MultiSnapHelper(gravity, interval, speedMsPerInch)
multiSnapHelper.attachToRecyclerView(recyclerView)

Full Example

Here is a full example project:

(a). HorizontalAdapter.kt

Here is the full code for our HorizontalAdapter.kt file:

package com.takusemba.multisnaprecyclerviewsample

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.takusemba.multisnaprecyclerviewsample.HorizontalAdapter.ViewHolder

class HorizontalAdapter(
    private val titles: Array<String>
) : androidx.recyclerview.widget.RecyclerView.Adapter<ViewHolder>() {

  override fun onCreateViewHolder(
      viewGroup: ViewGroup,
      viewType: Int
  ): ViewHolder {
    val inflater = LayoutInflater.from(viewGroup.context)
    val view = inflater.inflate(R.layout.item_horizontal, viewGroup, false)
    return ViewHolder(view)
  }

  override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val title = titles[position]
    holder.title.text = title
  }

  override fun getItemCount(): Int {
    return titles.size
  }

  class ViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(
      itemView) {
    val title: TextView = itemView.findViewById(R.id.title) as TextView
  }
}

(b). VerticalAdapter.kt

Here is the full code for our VerticalAdapter.kt file:

package com.takusemba.multisnaprecyclerviewsample

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

class VerticalAdapter(
    private val titles: Array<String>
) : androidx.recyclerview.widget.RecyclerView.Adapter<VerticalAdapter.ViewHolder>() {

  override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
    val inflater = LayoutInflater.from(viewGroup.context)
    val view = inflater.inflate(R.layout.item_vertical, viewGroup, false)
    return ViewHolder(view)
  }

  override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val title = titles[position]
    val description = "Hello world, $title"
    holder.title.text = title
    holder.description.text = description
  }

  override fun getItemCount(): Int {
    return titles.size
  }

  class ViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(
      itemView) {
    val title: TextView = itemView.findViewById(R.id.title) as TextView
    val description: TextView = itemView.findViewById(R.id.description) as TextView
  }
}

(c). VerticalActivity.kt

Here is the full code for our VerticalActivity.kt file:

package com.takusemba.multisnaprecyclerviewsample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.takusemba.multisnaprecyclerview.MultiSnapRecyclerView

class VerticalActivity : AppCompatActivity() {

  companion object {
    private val TITLES = arrayOf(
        "Android",
        "Beta",
        "Cupcake",
        "Donut",
        "Eclair",
        "Froyo",
        "Gingerbread",
        "Honeycomb",
        "Ice Cream Sandwich",
        "Jelly Bean",
        "KitKat",
        "Lollipop",
        "Marshmallow",
        "Nougat",
        "Oreo"
    )
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_vertical)

    val adapter = VerticalAdapter(TITLES)
    val recyclerView = findViewById<MultiSnapRecyclerView>(R.id.recycler_view)
    val manager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    recyclerView.layoutManager = manager
    recyclerView.adapter = adapter
  }
}

(d). MainActivity.kt

Here is the full code for our MainActivity.kt file:

package com.takusemba.multisnaprecyclerviewsample

import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    findViewById<TextView>(R.id.horizontal).setOnClickListener {
      startActivity(Intent([email protected], HorizontalActivity::class.java))
    }

    findViewById<TextView>(R.id.vertical).setOnClickListener {
      startActivity(Intent([email protected], VerticalActivity::class.java))
    }
  }
}

Reference

You can DOWNLOAD FULL CODE.
You can also browse code or read more here.
Follow code author here.


Read More.

Related Posts