This library shows you a gallery using RecyclerView.
First step, add dependence in your build.gradle
.
compile 'com.ryan.rv_gallery:rv-gallery:1.1.2'
Second step, using GalleryRecyclerView
in your layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
android:gravity="center"
android:orientation="vertical">
<!-- PagerSnapHelper can move just one page when scroll -->
<!-- LinearSnapHelper can move serveral page when scroll-->
<com.ryan.rv_gallery.GalleryRecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="480dp"
app:helper="PagerSnapHelper/LinearSnapHelper" />
</RelativeLayout>
Third step, init your GalleryRecyclerView in your java code just like using the normal RecyclerView. Note that you must use the LinearLayoutManager as your LayoutManager. At the same time, you must set the orientation like HORIZONTAL
or VERTICAL
, to make your GalleryRecyclerView
scroll horizontally or vertically.
GalleryRecyclerView mRecyclerView = findViewById(R.id.rv_list);
RecyclerAdapter adapter = new RecyclerAdapter(getApplicationContext(), getDatas());
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
mRecyclerView.setAdapter(adapter);
Finally, set some params of GalleryRecyclerView
if your need, and you must use the setUp()
method to make the GalleryRecyclerView
to work.
mRecyclerView
// set scroll speed(pixel/s)
.initFlingSpeed(9000)
// set page distance and visible distance of the nearby.
.initPageParams(0, 40)
// set the animation factor
.setAnimFactor(0.1f)
// set animation type. you can choose AnimManager.ANIM_BOTTOM_TO_TOP or AnimManager.ANIM_TOP_TO_BOTTOM
.setAnimType(AnimManager.ANIM_BOTTOM_TO_TOP)
// set click listener
.setOnItemClickListener(this)
// set whether auto play
.autoPlay(false)
// set auto play intervel
.intervalTime(2000)
// set default position
.initPosition(1)
// finally call method
.setUp();
You also can release GalleryRecyclerView
if you need
@Override
protected void onDestroy() {
super.onDestroy();
if (mRecyclerView != null) {
// release
mRecyclerView.release();
}
}
Java API
initFlingSpeed(int speed)
:set scroll speed(pixel/s)setAnimFactor(float factor)
:set the animation factorsetAnimType(int type)
:set animation type. you can chooseAnimManager.ANIM_BOTTOM_TO_TOP
orAnimManager.ANIM_TOP_TO_BOTTOM
setOnItemClickListener(OnItemClickListener mListener)
:set click listenerinitPageParams(int pageMargin, int leftPageVisibleWidth)
:set page distance and visible distance of the nearby.getScrolledPosition()
: get current positiongetLinearLayoutManager()
:get LayoutManagergetOrientation()
:get current scroll orientation(HORIZONTAL:0 VERTICAL:1)autoPlay(boolean)
:set can it auto playintervalTime(int interval)
:set auto play intervelinitPosition(int position)
: set default position
XML API
app:helper="PagerSnapHelper/LinearSnapHelper"
:PagerSnapHelper can move just one page when scroll,LinearSnapHelper can move serveral page when scroll.
see more in Releases。
Copyright 2017 ryanlijianchang
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.