The vertical Gallery, zoom in the middle and press on both sides

Use RecyclerView to achieve Gallery effect, enlarge in the middle, and press both sides

Specific github:

Usage method

1. Add dependency

2. Add a layout and use the recyclerview of the system (remember to import the package of recyclerview)


3. The user-defined adapter usually uses the item click event, so you write it in advance

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Integer> mFruitList;
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener{
        void onClick( int position);
        void onLongClick( int position);
    public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
        this. mOnItemClickListener=onItemClickListener;
    public MyAdapter(List<Integer> mFruitList) {
        this.mFruitList = mFruitList;
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(parent.getContext())
        ViewHolder holder=new ViewHolder(view);
        return holder;
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final int index=position;
        int fruit=mFruitList.get(index);
        if( mOnItemClickListener!= null){
            holder.itemView.setOnClickListener( new View.OnClickListener() {
                public void onClick(View v) {
            holder. itemView.setOnLongClickListener( new View.OnLongClickListener() {
                public boolean onLongClick(View v) {
                    return false;
    public int getItemCount() {
        return mFruitList.size();
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        public ViewHolder(View itemView) {

4. Used in activity

public class CarouselRecyclerView extends AppCompatActivity {
        private List<Integer> mDatas;
    protected void onCreate(Bundle savedInstanceState) {
        mDatas=new ArrayList<>();

        final CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.VERTICAL, true);
        layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener());//Achieve zoom effect
        final RecyclerView recyclerView =findViewById(;
        recyclerView.setHasFixedSize(true);//Fixed item size
        final MyAdapter myAdapter=new MyAdapter(mDatas);
        recyclerView.addOnScrollListener(new CenterScrollListener());
        myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            public void onClick(int position) {
                Toast.makeText(CarouselRecyclerView.this,"you click "+position+" position"
            public void onLongClick(int position) {
        layoutManager.addOnItemSelectionListener(new CarouselLayoutManager.OnCenterItemSelectionListener() {
             After use, it is found that this method is a listening method for item change. For example, after the completion of rolling from pos=1 to pos=2, it will call
            public void onCenterItemChanged(int adapterPosition) {
                int index=adapterPosition;
                //I don't know what the judgment is. It's rolling enough
                if(CarouselLayoutManager.INVALID_POSITION!= adapterPosition) {
                    Toast.makeText(CarouselRecyclerView.this,"you scroll to "+index+" position"
                Toast.makeText(CarouselRecyclerView.this,"you scroll to "+index+" position"


The above is all the code. Copy, paste and import the package to use


I can't write it myself, so I have to find it

It took a long time to find this effect

It seems that the spacing between two items cannot be set directly. Change the xml of the item to meet the effect

If the ImageView in this item is changed to VideoView, scrolling up and down will still show the masking effect, but I'm satisfied


See the github address of this project for other details

Reference article:

Tags: github xml

Posted on Sun, 05 Jan 2020 19:30:58 -0800 by adi