View Pager with previous and next item smaller in size with infinite scroll

前端 未结 3 1466
轮回少年
轮回少年 2020-12-01 07:06

Want to create the view pager same as following UI, applied custom transformer but not working.

ViewPager.java

public class MyViewPager ext         


        
相关标签:
3条回答
  • 2020-12-01 07:27

    You can make use of clipToPadding and programmatically adding the pageMargin and padding to your viewpager. Try something like this-

    viewpager.setClipToPadding(false);
    viewpager.setPadding(40, 0, 70, 0);
    viewpager.setPageMargin(20);
    
    0 讨论(0)
  • 2020-12-01 07:29

    Using ViewPager2 (that has RecyclerView in it).

    vp2 is instance of ViewPager2

    This worked for me:

    RecyclerView rv = (RecyclerView) vp2.getChildAt(0);
    rv.setPadding(80, 0, 80, 0);
    rv.setClipToPadding(false);
    

    I used FragmentStateAdapter for it and overriding getItemId() and containsItem() for add/remove fragments.

    result is here:

    0 讨论(0)
  • 2020-12-01 07:42

    UPDATE - if you want to make current page zoom use below PageTransformer

    import android.os.Bundle;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.viewpager2.widget.ViewPager2;
    
    public class JavaActivity extends AppCompatActivity {
    
        ViewPager2 myViewPager2;
        MyAdapter MyAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_java);
    
            myViewPager2 = findViewById(R.id.viewpager);
    
            MyAdapter = new MyAdapter(this);
            myViewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
            myViewPager2.setAdapter(MyAdapter);
            myViewPager2.setOffscreenPageLimit(3);
    
            float pageMargin = getResources().getDimensionPixelOffset(R.dimen.pageMargin);
            float pageOffset = getResources().getDimensionPixelOffset(R.dimen.offset);
    
            myViewPager2.setPageTransformer((page, position) -> {
                float myOffset = position * -(2 * pageOffset + pageMargin);
                if (position < -1) {
                    page.setTranslationX(-myOffset);
                } else if (position <= 1) {
                    float scaleFactor = Math.max(0.7f, 1 - Math.abs(position - 0.14285715f));
                    page.setTranslationX(myOffset);
                    page.setScaleY(scaleFactor);
                    page.setAlpha(scaleFactor);
                } else {
                    page.setAlpha(0);
                    page.setTranslationX(myOffset);
                }
            });
    
        }
    }
    

    OUTPUT

    NOTE: you can download complete code from my GitHub repositories

    Try this way

    JavaActivity

    import android.os.Bundle;
    import android.view.View;
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.view.ViewCompat;
    import androidx.viewpager2.widget.ViewPager2;
    
    public class JavaActivity extends AppCompatActivity {
    
        ViewPager2 myViewPager2;
        MyAdapter MyAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_java);
    
            myViewPager2 = findViewById(R.id.viewpager);
    
            MyAdapter = new MyAdapter(this);
            myViewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
            myViewPager2.setAdapter(MyAdapter);
            myViewPager2.setOffscreenPageLimit(3);
    
            float pageMargin= getResources().getDimensionPixelOffset(R.dimen.pageMargin);
            float pageOffset = getResources().getDimensionPixelOffset(R.dimen.offset);
            
            myViewPager2.setPageTransformer(new ViewPager2.PageTransformer() {
                @Override
                public void transformPage(@NonNull View page, float position) {
                    float myOffset = position * -(2 * pageOffset + pageMargin);
                    if (myViewPager2.getOrientation() == ViewPager2.ORIENTATION_HORIZONTAL) {
                        if (ViewCompat.getLayoutDirection(myViewPager2) == ViewCompat.LAYOUT_DIRECTION_RTL) {
                            page.setTranslationX(-myOffset);
                        } else {
                            page.setTranslationX(myOffset);
                        }
                    } else {
                        page.setTranslationY(myOffset);
                    }
                }
            });
        }
    }
    

    activity_java layout file

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      xmlns:app="http://schemas.android.com/apk/res-auto"
                      xmlns:tools="http://schemas.android.com/tools"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical"
                      tools:context=".JavaActivity">
        
            <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/viewpager"
                    android:clipToPadding="false"
                    android:clipChildren="false"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
        
        </LinearLayout>
    
    >
    

    MyAdapter

    import android.content.Context;
    import android.graphics.Color;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    
        private Context context;
    
        public MyAdapter(Context context) {
            this.context = context;
        }
    
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.row_item, parent, false);
            return new MyViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.tvName.setText(String.format("Row number%d", position));
            if (position % 2 ==0){
                holder.imgBanner.setBackgroundColor(Color.RED);
            }else {
                holder.imgBanner.setBackgroundColor(Color.GREEN);
            }
        }
    
        @Override
        public int getItemCount() {
            return 15;
        }
    
        public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tvName;
            ImageView imgBanner;
    
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                tvName = itemView.findViewById(R.id.tvName);
                imgBanner = itemView.findViewById(R.id.imgBanner);
            }
        }
    }
    

    row_item layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 xmlns:tools="http://schemas.android.com/tools"
                 android:layout_width="match_parent"
                 android:layout_marginLeft="@dimen/pageMarginAndOffset"
                 android:layout_marginRight="@dimen/pageMarginAndOffset"
                 android:layout_height="match_parent"
                 android:orientation="vertical">
    
        <ImageView
                android:id="@+id/imgBanner"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent"
                android:contentDescription="@string/app_name"/>
    
        <TextView
                android:id="@+id/tvName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_centerInParent="true"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                android:textStyle="bold"
                tools:text="Hello"/>
    
    </RelativeLayout>
    

    OUTPUT

    0 讨论(0)
提交回复
热议问题