Search code examples
androidandroid-activityviewflipper

how to swipe between layouts in one activity using onTouchEvent


swipping between two layoutsI'm newbie android developer and I need to implement an activity with several layouts or frames and I want only one to be displayed at a time.I also want to swipe between these layouts (left or right) using onTouchEvent as it is shown in the picture. Any suggestion for that?Thanks in advance.


Solution

  • To achieve this you will have to use Tabs with swiping pages. Create two fragments.

    Follow the process.

    I think the way you trying to achieve this is an old way.

    For sliding pages with tabs do following.

    Download or copy following two files on github and paste your project. this is same as on developers.google.com except setDistributeEvenly method.

    https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java

    https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabStrip.java

    activity_main.xml

    <your.package.name.SlidingTabLayout
        android:clickable="true"
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
     </your.package.name.SlidingTabLayout>
    
     <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    

    MyAdapter.java (Here i used two pages only)

    class MyPagerAdapter extends FragmentPagerAdapter  
    {  
        String[] title = {"All","Favourites"};
        public MyPagerAdapter(FragmentManager fm) {  
            super(fm);  
        }  
        @Override  
        public Fragment getItem(int position) {
            Fragment fragment=null;  
             if (position==0)
                fragment= new All();  
                 if (position==1)
                    fragment= new Favourites();  
            return fragment;  
        }  
        @Override  
         public int getCount() {  
            return 2;  
        }  
           @Override  
           public CharSequence getPageTitle(int position) {
                 return title[position];
        }  
     }  
    

    tab_view.xml (view of tab only , if you want u can also use ImageView here)

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/tab_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text=""
            android:padding="15dp"
            android:textStyle="bold"
            android:textSize="25dp"
            />
       </FrameLayout>
    

    MainActivity.java

     private SlidingTabLayout tabLayout;
     private ViewPager pager;
     tabLayout= (SlidingTabLayout) findViewById(R.id.tabs);  
     pager = (ViewPager) findViewById(R.id.pager);  
     tabLayout.setCustomTabView(R.layout.tab_view,R.id.tab_title);  
     MyPagerAdapter adapter =  new MyPagerAdapter(getSupportFragmentManager());  
     pager.setAdapter(adapter);  
     tabLayout.setDistributeEvenly(true);  
     tabLayout.setViewPager(pager); 
    

    Above code is fine. but latest way to achive swiping tabs with pages is throug android support design library.