How to recognize the last page of viewpager and automatically redirect to first page when swiped












0














My title is my problem, I have 3 pages in a viewPager. How do I redirect to first page when I swipe on the last page in viewPager?



ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
int x = position;
Log.i("ON Possition",Integer.Tostring(x));
if(x == 3){
viewPager.setCurrentItem(x);
Log.i("ON last Possition",Integer.Tostring());
}
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {

}
};









share|improve this question





























    0














    My title is my problem, I have 3 pages in a viewPager. How do I redirect to first page when I swipe on the last page in viewPager?



    ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
    int x = position;
    Log.i("ON Possition",Integer.Tostring(x));
    if(x == 3){
    viewPager.setCurrentItem(x);
    Log.i("ON last Possition",Integer.Tostring());
    }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }
    };









    share|improve this question



























      0












      0








      0







      My title is my problem, I have 3 pages in a viewPager. How do I redirect to first page when I swipe on the last page in viewPager?



      ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageSelected(int position) {
      int x = position;
      Log.i("ON Possition",Integer.Tostring(x));
      if(x == 3){
      viewPager.setCurrentItem(x);
      Log.i("ON last Possition",Integer.Tostring());
      }
      }

      @Override
      public void onPageScrolled(int arg0, float arg1, int arg2) {

      }

      @Override
      public void onPageScrollStateChanged(int arg0) {

      }
      };









      share|improve this question















      My title is my problem, I have 3 pages in a viewPager. How do I redirect to first page when I swipe on the last page in viewPager?



      ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageSelected(int position) {
      int x = position;
      Log.i("ON Possition",Integer.Tostring(x));
      if(x == 3){
      viewPager.setCurrentItem(x);
      Log.i("ON last Possition",Integer.Tostring());
      }
      }

      @Override
      public void onPageScrolled(int arg0, float arg1, int arg2) {

      }

      @Override
      public void onPageScrollStateChanged(int arg0) {

      }
      };






      java android android-viewpager






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 9:46









      Dominik

      59114




      59114










      asked Nov 21 '18 at 8:35









      s-dept

      368




      368
























          1 Answer
          1






          active

          oldest

          votes


















          0














          you can't move to first page smoothly from last page.



          read below code. we should fake item count.



          private class MyPagerAdapter extends PagerAdapter {
          Context context;

          public MyPagerAdapter(Context context) {
          this.context= context;
          }

          @Override
          public int getCount() {
          if (tList != null && tList.size() > 1) {
          return tList.size() * MAX_PAGE; // simulate infinite by big number of products
          } else {
          return tList.size();
          }
          }


          @Override
          public int getItemPosition(Object object) {
          int position;
          if (tList != null && tList.size() > 1) {
          position = super.getItemPosition(object) % tList.size(); // use modulo for infinite cycling
          return position;
          } else {
          return super.getItemPosition(object);
          }

          }

          public int getItemPosition(int index) {
          int position;
          if (tList!= null && tList.size() > 1) {
          position = index % tList.size(); // use modulo for infinite cycling
          return position;
          } else {
          return index;
          }

          }


          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408044%2fhow-to-recognize-the-last-page-of-viewpager-and-automatically-redirect-to-first%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            you can't move to first page smoothly from last page.



            read below code. we should fake item count.



            private class MyPagerAdapter extends PagerAdapter {
            Context context;

            public MyPagerAdapter(Context context) {
            this.context= context;
            }

            @Override
            public int getCount() {
            if (tList != null && tList.size() > 1) {
            return tList.size() * MAX_PAGE; // simulate infinite by big number of products
            } else {
            return tList.size();
            }
            }


            @Override
            public int getItemPosition(Object object) {
            int position;
            if (tList != null && tList.size() > 1) {
            position = super.getItemPosition(object) % tList.size(); // use modulo for infinite cycling
            return position;
            } else {
            return super.getItemPosition(object);
            }

            }

            public int getItemPosition(int index) {
            int position;
            if (tList!= null && tList.size() > 1) {
            position = index % tList.size(); // use modulo for infinite cycling
            return position;
            } else {
            return index;
            }

            }


            }





            share|improve this answer


























              0














              you can't move to first page smoothly from last page.



              read below code. we should fake item count.



              private class MyPagerAdapter extends PagerAdapter {
              Context context;

              public MyPagerAdapter(Context context) {
              this.context= context;
              }

              @Override
              public int getCount() {
              if (tList != null && tList.size() > 1) {
              return tList.size() * MAX_PAGE; // simulate infinite by big number of products
              } else {
              return tList.size();
              }
              }


              @Override
              public int getItemPosition(Object object) {
              int position;
              if (tList != null && tList.size() > 1) {
              position = super.getItemPosition(object) % tList.size(); // use modulo for infinite cycling
              return position;
              } else {
              return super.getItemPosition(object);
              }

              }

              public int getItemPosition(int index) {
              int position;
              if (tList!= null && tList.size() > 1) {
              position = index % tList.size(); // use modulo for infinite cycling
              return position;
              } else {
              return index;
              }

              }


              }





              share|improve this answer
























                0












                0








                0






                you can't move to first page smoothly from last page.



                read below code. we should fake item count.



                private class MyPagerAdapter extends PagerAdapter {
                Context context;

                public MyPagerAdapter(Context context) {
                this.context= context;
                }

                @Override
                public int getCount() {
                if (tList != null && tList.size() > 1) {
                return tList.size() * MAX_PAGE; // simulate infinite by big number of products
                } else {
                return tList.size();
                }
                }


                @Override
                public int getItemPosition(Object object) {
                int position;
                if (tList != null && tList.size() > 1) {
                position = super.getItemPosition(object) % tList.size(); // use modulo for infinite cycling
                return position;
                } else {
                return super.getItemPosition(object);
                }

                }

                public int getItemPosition(int index) {
                int position;
                if (tList!= null && tList.size() > 1) {
                position = index % tList.size(); // use modulo for infinite cycling
                return position;
                } else {
                return index;
                }

                }


                }





                share|improve this answer












                you can't move to first page smoothly from last page.



                read below code. we should fake item count.



                private class MyPagerAdapter extends PagerAdapter {
                Context context;

                public MyPagerAdapter(Context context) {
                this.context= context;
                }

                @Override
                public int getCount() {
                if (tList != null && tList.size() > 1) {
                return tList.size() * MAX_PAGE; // simulate infinite by big number of products
                } else {
                return tList.size();
                }
                }


                @Override
                public int getItemPosition(Object object) {
                int position;
                if (tList != null && tList.size() > 1) {
                position = super.getItemPosition(object) % tList.size(); // use modulo for infinite cycling
                return position;
                } else {
                return super.getItemPosition(object);
                }

                }

                public int getItemPosition(int index) {
                int position;
                if (tList!= null && tList.size() > 1) {
                position = index % tList.size(); // use modulo for infinite cycling
                return position;
                } else {
                return index;
                }

                }


                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 8:52









                user10684624

                1




                1






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408044%2fhow-to-recognize-the-last-page-of-viewpager-and-automatically-redirect-to-first%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen