Awesome Recyclerview library that supports live animations and auto swipe with ViewPager.
- Minimum SDK Version : 24
- Recommended SDK Version : 29
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.shhj1998:android-live-slider:Tag'
}
Step 1. Create a ViewPager layout like example_page.xml
which will be inside your ViewPager :
<FrameLayout
android:id="@+id/page"
...>
<ImageView
android:id="@+id/image"
.../>
<TextView
android:id="@+id/title"
android:text="Title"
.../>
<TextView
android:id="@+id/description"
android:text="Description"
.../>
...
data class ExampleItem (
var title: String,
var description: String,
var img: Bitmap,
...
)
Step 3. Implement your customer PagerAdapter by inheriting the abstract class LiveSliderPagerAdapter
.
class ExamplePageAdapter : LiveSliderPagerAdapter<ExampleItem, String>() {
override fun createView(context: Context, container: ViewGroup, item: ExampleItem): View {
val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
// Create and connect the view xml you want to display in the viewPager.
val view = inflater.inflate(R.layout.example_page, container, false)
// Put the item data into the view object you want.
view.title.text = item.title
view.image.setImageBitmap(item.img)
...
return view
}
// Try adding an animation to the view object.
override fun startAnimation(context: Context, view: View) {
view.image.startAnimation(AnimationUtils.loadAnimation(context, R.anim.zoom))
...
}
override fun stopAnimation(context: Context, view: View) {
view.image.clearAnimation()
...
}
}
mRecyclerView = findViewById(R.id.recycler_view)
...
// definition of your recyclerview adapter
mExampleAdapter = LiveSliderAdapter(applicationContext, ExamplePageAdapter(), true)
mExampleAdapter.setHasStableIds(true)
mRecyclerView.adapter = mExampleAdapter
// If the ViewPager shown in RecycleView changes, start the animation.
mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if(newState == RecyclerView.SCROLL_STATE_IDLE) {
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
val animationItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition()
mExampleAdapter.startAnimation(animationItemPosition)
}
}
})
When you call the setFeedData()
function with parsed contents data, it applies immediately to the live-slider
recyclerview.
var mSampleData: Array<LiveSliderFeed<ExampleItem, String>>
...
mExampleAdapter!!.setFeedData(mSampleData)
Finish!
More details are in our example project. Also, if you want to add listeners to category title like touch, you can see our other example application.
Copyright 2019 POSCAT.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.