1

I am building an app in which I want to enable swipe. I want to swipe xml layouts but in a single activity. Earlier I tried to put images, it worked. I created an ImageAdapter class that extends PagerAdapter. In instantiateItem, I created a imageView object and set its image resource using setImageResource method. I'm new to android and I got this code from somewhere (i don't remember now). Now, I'm trying to do the same, just instead of images, I want to put layouts. I searched in net, I even got the code which uses Fragment..... But, when I write the code, eclipse says, minimum required sdk is 11. But I want my app to run in GingeBread also. I tried to get a reference to the layout using findViewById. But since I've not kept the required layout in setContentView in main activity, find view by id is returning null. I want a way to get a reference the layout or set the layout in View Pager. Any code to do this or which helps to enable swiping layouts would be useful. But I want it to work on Gingerbread also. Pls tell me, if I need to change my question format. Thanks in advance

Nagabhushan S N
  • 4,063
  • 5
  • 26
  • 54

2 Answers2

0

enter image description hereI have answered a similar question here :

How to use swipe gesture in a view pager's page with android? enter image description here Take a look at that and you will get what you are looking for.

Hope that helps .. and don't forget to accept my answer :)

Here's the images for that:

http://imageshack.com/a/img547/6988/cpr5.png

http://imageshack.com/a/img198/2594/tp58.png

Here's what I found for your first question. Its the infinite scrolling. Its a good post here:

Changing ViewPager to enable infinite page scrolling

Community
  • 1
  • 1
mike20132013
  • 5,257
  • 3
  • 28
  • 41
  • Thanks. But Fragment Adapter requires minimum API level of 11. I want it to run on API 8 also. Thats where I'm getting problem. Is it possible in API 8? Or atleast mention some method to respond to swipe events. Thanks – Nagabhushan S N Mar 01 '14 at 01:57
  • Not necessary.. I am using fragments even when my api level is 8. You need to include the support library and that's it. Did you try the answer from my other post ? – mike20132013 Mar 01 '14 at 15:19
  • Try that answer in my other post. I am pretty sure it should work because I am able to run in my device and in the manifest the min sdk I defined is 8..:) – mike20132013 Mar 01 '14 at 15:22
  • I have edited my answer here.. take a look at those images..:) – mike20132013 Mar 01 '14 at 15:28
  • Yeah Thanks. It worked out as i required. A lot of thanks. I've two more doubts. One thing, is it possible to swipe infinitely? i.e. after the last page, the first page should come. Second one, I'm unable to edit the contents dynamically. I've saved the data from one activity in a file. I want to read the file and set the data in this view pager accordingly. I've searched a lot. I got two answers. One inefficient method, to return POSITION_NONE and call notifyDataSetChanged(). I tried it, but seems to be not working. – Nagabhushan S N Mar 08 '14 at 07:07
  • One more thing to setTag in instantiate method and use findViewByTag() and then change. But i'm not getting any example how to do this. Pls can you refer me something or any other method. And again, ThanQ very much. – Nagabhushan S N Mar 08 '14 at 07:08
  • Sure.. let me look into that issue.. thanks for the info..For the second issue, do you want to set some data in one of the view and use the same data in some other view ? – mike20132013 Mar 08 '14 at 15:01
  • Yeah. I've three activities- say A,B,C. A is the main activity, A starts B, B starts C. C has text fields for user to enter that data. I've to change the textViews in Activity in A with the data that the user enters in C. I've got the data from C into A. I don't know how to change the data. Also, when the user closes the app, I'll save this data in a text file in internal memory of phone. Again when the user opens the app, this data should be loaded. I'm able to read the data but again I'm unable to change the text in the TextViews. Hope, I'm more clear this time – Nagabhushan S N Mar 09 '14 at 15:10
  • Here are the links http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view/8024557#8024557 http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view Alvarolb says to use setTag in instantiateItem and then find the view using findViewByTag and update. But, i'm not understanding how to do this. Pls help – Nagabhushan S N Mar 09 '14 at 15:16
  • I've encountered one more problem. I start Activity C from Activity B. But to my astonishment, the activity C opens a number of times. Twice in emulator, thrice in my friend's mobile and 5 times in my mobile. I meant, I started activity C. Now when i press back, it won't come back to activity B. I've to press back 5 times (in my mobile) to come back to activity B. The problem is, as I've explained, Activity C has some text fields for user to enter the data. When the user starts activity C, the previous data will be loaded in the text fields. The user alters the data and presses back. – Nagabhushan S N Mar 09 '14 at 15:24
  • Now, the activity closes and B has to open up. Instead, one more activity C will be open the previous data and not the data that the user altered currently. The same thing happens 4 times. What the user enters 5th time will be returned to activity B. I found this very strange. I'm starting activity once, but it gets opened a number of times that too different in different mobiles. I tried this. I used a static field count to count the number of times the activity C opens. In emulator it opens twice. So, I used if(count%2!=0) finish(); and also onBackPressed(); But it throws null ptr exception – Nagabhushan S N Mar 09 '14 at 15:29
  • I've posted a seperate question here http://stackoverflow.com/questions/22284268/activity-opens-five-times-when-i-start-it-once – Nagabhushan S N Mar 09 '14 at 15:41
  • Can you post your code in your other question you asked? – mike20132013 Mar 09 '14 at 17:27
0

For your first question, the Infinite Scrolling, I figured out one solution to it.

Here's how I am doing it:

No looping is required in this case. No matter what your getCount() is.

After setting the adapter, I am setting a setOnPageChangeListener to the view pager like this:

myPager.setOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(int position) {
//Define the focused page before your onCreate()..private int focusedPage = 0;
                    focusedPage = position;
                }
                //We don't have to do anything here.
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub

                }
                //Here's where the magic is. After you reach the end of the page, you can scroll again and it will move your view to page 1 again.
                @Override
                public void onPageScrollStateChanged(int arg0) {

                    myPager.setCurrentItem(0,false);

                }
            });

See if this solution works.. It's working perfect for me. .:D

mike20132013
  • 5,257
  • 3
  • 28
  • 41
  • ThanQ very much. I'll try and get you back. – Nagabhushan S N Mar 09 '14 at 15:20
  • I tried this. Its working perfectly. ThanQ very much. But the thing is, you won't get the swiping effect when u swipe from last page to first one as u get in other pages. Also, I'm unable to scroll back to last page from first page. Pls see see. And, I'll post the code for that question. ThanQ very much – Nagabhushan S N Mar 10 '14 at 00:22