title: How to add objects dynamically to a view pager of CardViews?
body: <p>I am trying to achieve an effect similar to <a href="https://www.youtube.com/watch?v=qgrsCOLM0LA&amp;feature=youtu.be" rel="nofollow noreferrer">this</a>. I looked around and I reached a code similar to this</p>
  
  <p><strong>ViewPagerAdapter</strong></p>
  
  <pre><code>public class ViewPagerAdapter extends PagerAdapter {
  
      private Context mContext;
      private LayoutInflater mLayoutInflater;
      private ArrayList&lt;SomeObject&gt; someObjectList;
  
      public ViewPagerAdapter(Context context, ArrayList&lt;SomeObject&gt; someObjectList) {
          mContext = context;
          this.someObjectList = someObjectList;
          mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      @Override
      public int getCount() {
          return someObjectList.size();
      }
  
      @Override
      public boolean isViewFromObject(View view, Object object) {
          return view == object;
      }
  
      @NonNull
      @Override
      public Object instantiateItem(@NonNull ViewGroup container, int position) {
          View itemvView = mLayoutInflater.inflate(R.layout.content_main, container, false);
  
          SomeObject object = someObjectList.get(position);
  
          TextView tvTitle = itemvView.findViewById(R.id.textView);
          tvTitle.setText(object.getTitle());
  
          ImageView image = itemvView.findViewById(R.id.imageView);
          image.setImageResource(object.getImageResource());
  
          container.addView(itemvView);
  
          return itemvView;
      }
  
      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
          container.removeView((LinearLayout) object);
      }
  
      public void addItems(List&lt;SomeObject&gt; someObject) {
          // TODO : Add objects dynamically without using notifyDataSetChanged()
      }
  
  }
  </code></pre>
  
  <p>I also modified a custom View Pager component to match my needs</p>
  
  <p><strong>Custom View Pager</strong></p>
  
  <pre><code>class VerticalViewPager : ViewPager {
  
      private val MIN_SCALE = 0.75f
  
      constructor(context: Context) : super(context) {
          init()
      }
  
      constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
          init()
      }
  
      private fun init() {
          // The majority of the magic happens here
          setPageTransformer(true, VerticalPageTransformer())
          // The easiest way to get rid of the overscroll drawing that happens on the left and right
          overScrollMode = View.OVER_SCROLL_NEVER
      }
  
      private inner class VerticalPageTransformer : ViewPager.PageTransformer {
          override fun transformPage(view: View, position: Float) {
  
              when {
                  position &lt; -1 -&gt; // [-Infinity,-1)
                      // This page is way off-screen to the left.
                      view.alpha = 0f
                  position &lt;= 0 -&gt; { // [-1,0]
                      // Use the default slide transition when moving to the left page
                      view.alpha = 1f
                      // Counteract the default slide transition
                      view.translationX = view.width * -position
  
                      //set Y position to swipe in from top
                      val yPosition = position * view.height
                      view.translationY = yPosition
                      view.scaleX = 1f
                      view.scaleY = 1f
  
                  }
                  position &lt;= 1 -&gt; { // [0,1]
                      view.alpha = 1f
  
                      // Counteract the default slide transition
                      view.translationX = view.width * -position
  
  
                      // Scale the page down (between MIN_SCALE and 1)
                      val scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position))
                      view.scaleX = scaleFactor
                      view.scaleY = scaleFactor
  
                  }
                  else -&gt; // (1,+Infinity]
                      // This page is way off-screen to the right.
                      view.alpha = 0f
              }
          }
  
      }
  
      /**
       * Swaps the X and Y coordinates of your touch event.
       */
      private fun swapXY(ev: MotionEvent): MotionEvent {
          val width = width.toFloat()
          val height = height.toFloat()
  
          val newX = ev.y / height * width
          val newY = ev.x / width * height
  
          ev.setLocation(newX, newY)
  
          return ev
      }
  
      override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
          val intercepted = super.onInterceptTouchEvent(swapXY(ev))
          swapXY(ev) // return touch coordinates to original reference frame for any child views
          return intercepted
      }
  
      override fun onTouchEvent(ev: MotionEvent): Boolean {
          return super.onTouchEvent(swapXY(ev))
      }
  }
  </code></pre>
  
  <p>And I inserted the custom ViewPager view in the main_activity.xml and set the adapter in the MainActivity.java..</p>
  
  <p>I need to add the card items dynamically and infinitely. i.e. The data will be loaded as the user scrolls through the card stack and will show infinite cards.</p>
  
  <p>So for that, I will need to add objects to the view pager dynamically as the cards are scrolled down.</p>
  
  <p>As I googled, the only way to add more data to the view pager is to use the notifyDataSetChanged method. But I do not want this as it becomes really inefficient as the data size increases.'</p>
  
  <p>How do I add more data to the view pager dynamically?</p>
  
  <p>I figured out the way to notify the activity when to load. I just need to figure out how to set the data without using notifyDataSetChanged().</p>