4

I want the user be able to change the activity of my Android application by using swipe up/down/left/right. I have so far been unable to find a way to do this, I have experimented with GridView and GridViewPager however these have so far been of no use as I want to create a "table" of activities which the user swipes between where the rows and columns are of variable length. For example;

Act 01 - Act 02 - Act 03 - Act 04
Act 05 - Act 06 - Act 07
Act 08 - Act 09
Act 10 - Act 11 - Act 12
Act 13 - Act 14 - Act 15 - Act 16 - Act 17

In the above example swiping left and right would take the user to the appropriate activity on the left or right, if however the user was to swipe up or down it would take them to the first activity on the row above/below where they swiped.

e.g. User is on Act 09;
Swipe left = Act 08
Swipe right = No action
Swipe up = Act 10
Swipe down = Act 05

This as far as I can tell is not possible to do using grid view.

Worth pointing out I intend to implement this into android wear

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
James B
  • 149
  • 2
  • 14
  • Interesting idea. Anything is possible ;). What have you tried? – Jay Harris Mar 01 '17 at 01:51
  • I've tried with the grid view pager but as you have to declare the number of rows and columns then all the rows have to be of the same length which is obviously not ideal. I have also tried an on touch listener however I have been unable to get this to work effectively. – James B Mar 01 '17 at 15:42
  • So you simply want to navigate between an array of activities, with a swipe gesture? – Jay Harris Mar 01 '17 at 16:23
  • I guess if a 2D array of activities would be possible, then I would be open to that. I was more thinking of on the on create of each activity, a switch or else if statement coupled with a listener of some sort saying "if 'swipe up' then 'open activity x' if swipe down 'open activity y' and I would obviously only have 4 conditions on each activity for this. – James B Mar 01 '17 at 17:15
  • I would use a different approach. Creating 17 activities with explicit `onCreate` functions to scroll to each page, is wildly inefficient not to mention unscalable. I would NOT recommend this. I would instead do more research into `ViewPager` activities and how they are implemented. Then I would extend it to include swipe up and down gestures in an efficient and appropriate fashion. I warn that what you want to do is complex if your going to do it the right way. I don't know where you are in your programming career but this will require more advanced programming knowledge / techniques. – Jay Harris Mar 01 '17 at 20:25
  • 1
    Another approach is to have an activity and a container view eg `ViewGroup` and have that activity handle swipe gestures to change between layouts (for your case pages) inside the `ViewGroup`. This will prove to be the easiest solution to implement. But to have the smoothness of a `ViewPager` you'll have to do more work. . Even tough this is simpler and more native to your question, The problem with this is you might be reinventing the wheel (`ViewPager`) – Jay Harris Mar 01 '17 at 20:30
  • i am agree with @JayHarris, and instead of many activities, you should consider using fragment. – Randyka Yudhistira Mar 02 '17 at 01:46
  • If any of you would like to post your comment in an answer I'm happy to accept it :) – James B Mar 07 '17 at 17:45

0 Answers0