使用自定义 LayoutManager
实现 Android 中 Gallery
或者 ViewPager
控件的效果,支持垂直和水平两个方向,支持 RecycleView
的试图回收机制
implementation 'github.hellocsl:GalleryLayoutManager:{latest-release-version}'
不要使用 RecycleView#setLayoutManager
方法,而是使用 GalleryLayoutManager#attach
方法来绑定RecycleView
和 GalleryLayoutManager
GalleryLayoutManager layoutManager = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
//layoutManager.attach(mPagerRecycleView); // 默认选中位置为0
//不要使用 RecycleView#setLayoutManager 方法,而是使用 GalleryLayoutManager#attach 方法
layoutManager.attach(mPagerRecycleView, 30);
//...
//为 RecycleView 绑定 adapter
mPagerRecycleView.setAdapter(imageAdapter);
//添加视图选中监听
layoutManager.setOnItemSelectedListener(new GalleryLayoutManager.OnItemSelectedListener() {
@Override
public void onItemSelected(RecyclerView recyclerView, View item, int position) {
//.....
}
});
继承实现 GalleryLayoutManager.ItemTransformer
public class ScaleTransformer implements GalleryLayoutManager.ItemTransformer {
@Override
public void transformItem(GalleryLayoutManager layoutManager, View item, float fraction) {
item.setPivotX(item.getWidth() / 2.F);
item.setPivotY(item.getHeight() / 2.F);
float scale = 1 - 0.3F * Math.abs(fraction);
item.setScaleX(scale);
item.setScaleY(scale);
}
}
//添加 Item 转换器,和 ViewPager 一样使用
layoutManager.setItemTransformer(new ScaleTransformer());
Copyright [2017] [Hello Csl]
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.