How to create browser tabs in Android Studio

Asked

Viewed 103 times

0

Hello! , I’m creating a browser for Android using Android Studio I’m trying to create a feature that has in all browser that are the multiple tabs like Google Chrome, but I’m out of luck and all my attempts are failing, anyone knows how to create tabs for Android browser? Like Google Chrome, uc Browser etc...

1 answer

2


Yes. You can create an adapter with appendable fragments.

public class MyAdapter extends FragmentStatePagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    /**Adds a fragment and corresponding title to the list*/
    public void addFragment(Fragment f, String title){
        mFragmentList.add(f);
        mFragmentTitleList.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
}

And add a viewpager that doesn’t allow sliding between fragments:

public class NonSwipeableViewPager extends ViewPager {

    public NonSwipeableViewPager(Context context) {
        super(context);
        setMyScroller();
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMyScroller();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    //down one is added for smooth scrolling

    private void setMyScroller() {
        try {
            Class<?> viewpager = ViewPager.class;
            Field scroller = viewpager.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            scroller.set(this, new MyScroller(getContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class MyScroller extends Scroller {
        public MyScroller(Context context) {
            super(context, new DecelerateInterpolator());
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
        }
    }
}

Set up on onCreate activity:

// Sets up the ViewPager with the sections adapter
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager();

Use this function for setup:

/**Sets up the custom viewpager*/
private void setupViewPager(){
    // here we set up our own adapter...
    MyAdapter adapter= new MyAdapter(getSupportFragmentManager());

    // ...add the fragments we want...
    adapter.addFragment(new FragmentOne(), "First");
    adapter.addFragment(new FragmentTwo(), "Second");
    adapter.addFragment(new FragmentThree(), "Third");

    // ...and set up our own adapter on the viewpager
    mViewPager.setAdapter(adapter);
}

To add fragments you only have to call addFragment() from the adapter.

If you want, you can put a function in Viewpager to display the fragment you want. Something like:

public void showFragment(int fragmentIndex){
    mViewPager.setCurrentItem(fragmentIndex);
}

Don’t forget to use the right Viewpager in the activity layout.

You can use the source tabs that come in the Android Studio library. You just have to make them interact with Viewpager, and present the bonds accordingly.

I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.