Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of swipe navigation on android?

Tags:

java

android

I'm trying to figure out how to create three views for lateral navigation but the examples provided by developer.android.com are useless since they are only for API 11 forward. I've been looking for an easy copy/paste example of swipe navigation but I haven't had any luck.

I have found tutorials to swype between activities, or different layouts but it's not what I want to do, I need to generate each "screen" programatically. This is because I'm working on something that should work like the home screen, and the number of screens may vary.

Could somebody provide some example/tutorial on how to create three views with lateral navigation programatically? Anything will help.

like image 387
lisovaccaro Avatar asked Dec 07 '22 10:12

lisovaccaro


2 Answers

This is the main activity:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.home_pannels_pager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }

}

Create another class called MyPagerAdapter in the same package:

    public class MyPagerAdapter extends PagerAdapter {

        // State number of pages
        public int getCount() {
            return 5;
        }

        // Set each screen's content
        @Override
        public Object instantiateItem(View container, int position) {
            Context context = container.getContext();
            LinearLayout layout = new LinearLayout(context);
            // Add elements
            TextView textItem = new TextView(context);

            buttonItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("com.phone");
                   // myFancyMethod(v);
                }
            });


            switch (position) {
            case 0:
               textItem.setText("First Screen");
               break;
            case 1:
               textItem.setText("Second Screen");
               break;
            case 2:
                textItem.setText("Third Screen");
                break;


  case 3:
            textItem.setText("Fourth Screen");
            break;
        case 4:
            textItem.setText("Fifth Screen");
            break;
        }
        layout.addView(textItem);
        ((ViewPager) container).addView(layout, 0); // This is the line I added
        return layout;
    }
    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }
    @Override
    public Parcelable saveState() {
        return null;
    }
}

Finally create activity_main.xml inside /res/layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/home_pannels_pager"/>
</LinearLayout>
like image 180
lisovaccaro Avatar answered Dec 22 '22 17:12

lisovaccaro


Visit the site its give better library for swipe view navigation

like image 32
bhavesh N Avatar answered Dec 22 '22 15:12

bhavesh N