登录 立即注册
安币:

安卓巴士 - 安卓开发 - Android开发 - 安卓 - 移动互联网门户

使用RecyclerView编写画廊效果

[复制链接]
来自: littleRed 分类: Android精品源码 上传时间: 2017-8-8 10:35:13
Tag:

项目介绍:

Gradle

Add this into your dependencies block.

compile 'com.yarolegovich:discrete-scrollview:1.2.0'
Sample

Get it on Google Play

Please see the sample app for examples of library usage.

GifSampleWeather

Wiki

General

The library uses a custom LayoutManager to adjust items' positions on the screen and handle scroll, however it is not exposed to the client code. All public API is accessible through DiscreteScrollView class, which is a simple descendant of RecyclerView.

If you have ever used RecyclerView - you already know how to use this library. One thing to note - you should NOT set LayoutManager.

Usage:

Add DiscreteScrollView to your layout either using xml or code:
Create your implementation of RecyclerView.Adapter. Refer to the sample for an example, if you don't know how to do it.
Set the adapter.
You are done!
<com.yarolegovich.discretescrollview.DiscreteScrollView
android:id="@+id/picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:dsv_orientation="horizontal|vertical" /> <!-- orientation is optional, default is horizontal -->
DiscreteScrollView scrollView = findViewById(R.id.picker);
scrollView.setAdapter(new YourAdapterImplementation());
API

Layout

scrollView.setOrientation(Orientation o); //Sets an orientation of the view
scrollView.setOffscreenItems(count); //Reserve extra space equal to (childSize * count) on each side of the view
Related to the current item:

scrollView.getCurrentItem(); //returns adapter position of the currently selected item or -1 if adapter is empty.
scrollView.scrollToPosition(int position); //position becomes selected
scrollView.smoothScrollToPosition(int position); //position becomes selected with animated scroll
scrollView.setItemTransitionTimeMillis(int millis); //determines how much time it takes to change the item on fling, settle or smoothScroll
Transformations

One useful feature of ViewPager is page transformations. It allows you, for example, to create carousel effect. DiscreteScrollView also supports page transformations.

scrollView.setItemTransformer(transformer);

public interface DiscreteScrollItemTransformer {

/**
 * In this method you apply any transform you can imagine (perfomance is not guaranteed).
 * @param position is a value inside the interval [-1f..1f]. In idle state:
 * |view1|  |currentlySelectedView|  |view2|
 * -view1 and everything to the left is on position -1;
 * -currentlySelectedView is on position 0;
 * -view2 and everything to the right is on position 1.
 */
void transformItem(View item, float position); 

}
Because scale transformation is the most common, I included a helper class - ScaleTransformer, here is how to use it:

cityPicker.setItemTransformer(new ScaleTransformer.Builder()
.setMaxScale(1.05f)
.setMinScale(0.8f)
.setPivotX(Pivot.X.CENTER) // CENTER is a default one
.setPivotY(Pivot.Y.BOTTOM) // CENTER is a default one
.build());
You may see how it works on GIFs.

Infinite scroll

Infinite scroll is implemented on the adapter level:

InfiniteScrollAdapter wrapper = InfiniteScrollAdapter.wrap(yourAdapter);
scrollView.setAdapter(wrapper);
An instance of InfiniteScrollAdapter has the following useful methods:

int getRealItemCount();

int getRealCurrentPosition();

int getRealPosition(int position);

/*

  • You will probably want this method in the following use case:

  • int targetAdapterPosition = wrapper.getClosestPosition(targetPosition);

  • scrollView.smoothScrollTo(targetAdapterPosition);

  • To scroll the data set for the least required amount to reach targetPosition.
    */

int getClosestPosition(int position);
Currently InfiniteScrollAdapter handles data set changes inefficiently, so your contributions are welcome.

Callbacks

Scroll state changes:
scrollView.addScrollStateChangeListener(listener);
scrollView.removeScrollStateChangeListener(listener);

public interface ScrollStateChangeListener<T extends ViewHolder> {

void onScrollStart(T currentItemHolder, int adapterPosition); //called when scroll is started, including programatically initiated scroll

void onScrollEnd(T currentItemHolder, int adapterPosition); //called when scroll ends
/**

  • Called when scroll is in progress.

  • @param scrollPosition is a value inside the interval [-1f..1f], it corresponds to the position of currentlySelectedView.

  • In idle state:

  • |view1| |currentlySelectedView| |view2|

  • -view1 is on position -1;

  • -currentlySelectedView is on position 0;

  • -view2 is on position 1.

  • @param currentHolder - ViewHolder of a current view

  • @param newCurrent - ViewHolder of a view that moved closer to the center
    */

void onScroll(float scrollPosition, @NonNull T currentHolder, @NonNull T newCurrentHolder);
}
Scroll:
scrollView.addScrollListener(listener);
scrollView.removeScrollListener(listener);

public interface ScrollListener<T extends ViewHolder> {
//The same as ScrollStateChangeListener, but for the cases when you are interested only in onScroll()
void onScroll(float scrollPosition, @NonNull T currentHolder, @NonNull T newCurrentHolder);
}
Current selection changes:
scrollView.addOnItemChangedListener(listener);
scrollView.removeOnItemChangedListener(listener);

public interface OnItemChangedListener<T extends ViewHolder> {
/**

  • Called when new item is selected. It is similar to the onScrollEnd of ScrollStateChangeListener, except that it is

  • also called when currently selected item appears on the screen for the first time.

  • viewHolder will be null, if data set becomes empty
    */

void onCurrentItemChanged(@Nullable T viewHolder, int adapterPosition);
}

相关源码推荐:

我来说两句
*滑动验证:
所有评论(102)
myself2015 2017-8-8 10:56:37
感谢大神~
回复
柠檬lemon 2017-8-8 10:56:41
楼主是好人,回个帖会有安币吗?
回复
manmanyun 2017-8-8 10:56:52
感觉楼主很用心,辛苦啦~
回复
rebecca 2017-8-8 10:57:10
安卓巴士是个不错的网站,我来顶个贴~
回复
humanboy 2017-8-8 10:57:23
楼主威武,以后多发干货,多办活动~!
回复
是个宝宝 2017-8-8 10:57:26
感觉楼主很用心,辛苦啦~
回复
apkbus热心网友 2017-8-8 11:02:00
淡定,淡定,淡定……
回复
提取码:  下载次数:44 状态:已购或VIP 售价:10(原价:10)金钱 下载权限:限制会员 
2679 3 44
代码贡献英雄榜
用户名 下载数
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,769657487

如何获得代码达人称号

如何成为签约作者

联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 申请友链|手机版|站点统计|安卓巴士 ( 粤ICP备15117877号 )

快速回复 返回顶部 返回列表