Android: Use of TabHost and ViewPage multi-page sliding

Source: Internet
Author: User

Android: Use of TabHost and ViewPage multi-page sliding

 

A simple function to be implemented:

Generally speaking:
In TabHost, you can set a custom area that can be clicked to switch the Tab. If you click the same Tab twice to refresh the current tab, The ViewPage can also be like this, however, there is a third-party viewpagerindicator, which is directly used in the project;

Text description:
When sliding between three Fragment in the ViewPage, the content of each Fragment is not refreshed. When you click Indicate on the ViewPage, each Fragment is refreshed;

 

Some ideas for simple implementation:

TabHost is relatively simple. When you click tab1, if tab1 is currently displayed, refresh the page. Otherwise, switch to the tab.

  tabHost.addTab(tabHost.newTabSpec(tab+i).setIndicator(tabView[i])                    .setContent(tabContent[i]));            tabView[i].setTag(i);            tabView[i].setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                        int tabId=Integer.parseInt(v.getTag().toString());                    if(tabId==tabHost.getCurrentTab()){                        reloadByTabId(tabId);                    }else  {                        tabHost.setCurrentTab(tabId);                        changeTabCotent(tabId);                    }                }            });

ViewPage can also be the same as TabHost, but a third party is used, so you can change your mind;

        CONTENT =new String[]{};        FRAMES = new Fragment[]{};        initIndicator(,CONTENT,FRAMES);        adapter = new ViewPageAdapter(getSupportFragmentManager());        mViewPage = (MyViewPager)findViewById(R.id.pager);        mViewPage.setAdapter(adapter);        mViewPage.setOffscreenPageLimit(0);        TabPageIndicator indicator =(TabPageIndicator)findViewById(R.id.indicator);        indicator.setViewPager(mViewPage, selectIndex);        indicator.setOnPageChangeListener(new ViewPageListener());   

Some people say that the page becomes white after switching back a certain number of Fragment entries in the viewPage, and it is unclear why, then intercept destroyItem () in the ViewPage ():

 class ViewPageAdapter extends FragmentPagerAdapter {        public ViewPageAdapter(FragmentManager fm) {            super(fm);        }        @Override        public Fragment getItem(int position) {            return FRAMES[position % CONTENT.length];        }        @Override        public int getItemPosition(Object object) {            return super.getItemPosition(object);        }        @Override        public CharSequence getPageTitle(int position) {            return CONTENT[position % CONTENT.length];        }        @Override        public int getCount() {            return CONTENT.length;        }        @Override        public void destroyItem(View container, int position, Object object) {        }        @Override        public void destroyItem(ViewGroup container, int position, Object object) {        }    }
The key to ViewPageListener. class is that the selected viewpage initialization event is not clear before the interface Initialization is complete. Therefore, when onViewPageSelection () is called, note that the current page has been initialized;
    class ViewPageListener implements OnPageChangeListener {        private int offset = 0;        @Override        public void onPageScrollStateChanged(int arg0) {        }        @Override        public void onPageScrolled(int arg0, float arg1, int arg2) {            offset = arg2;            if(isInit&&arg1==0&&arg2==0){                onPageSelected(arg0);                isInit = false;            }        }        @Override        public void onPageSelected(int arg0) {            if(adapter!=null){                Fragment fragment = adapter.getItem(arg0);                if(fragment instanceof MyBaseFragment){                    MyBaseFragment myFragment = (MyBaseFragment)fragment;                    myFragment.onViewPageSelection(arg0,offset);                }            }        }    }

Get Viewpager Event Notifications in a single Fragment:

@Override    public void onViewPageSelection(int index,int offset) {        super.onViewPageSelection(index,offset);        selectedIndex = index;        setPositon(index);        if(!hasLoadOk.containsKey(index)||hasLoadOk.get(index)==false){            isShowProgress = false;            startLocation(true,false);        }else if(offset==0){            isShowProgress = true;            loadData(true);        }    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.