Cdroid/apps/samples/tablayout.cc

51 lines
1.9 KiB
C++
Raw Normal View History

2022-01-18 16:21:54 +08:00
#include <cdroid.h>
2021-09-07 16:57:48 +08:00
class MyPageAdapter:public PagerAdapter{
public:
int getCount()override{return 8;}
bool isViewFromObject(View* view, void*object)override{ return view==object;}
void* instantiateItem(ViewGroup* container, int position)override{
2021-09-07 16:57:48 +08:00
SimpleMonthView*sm=new SimpleMonthView(1280,560);
sm->setMonthParams(23,Calendar::MAY+position,2021,-1,1,31);
2022-02-08 09:02:33 +08:00
container->addView(sm).setId(100+position);
2021-09-07 16:57:48 +08:00
return sm;
}
void destroyItem(ViewGroup* container, int position,void* object)override{
2021-09-07 16:57:48 +08:00
container->removeView((View*)object);
delete (View*)object;
2021-09-07 16:57:48 +08:00
}
std::string getPageTitle(int position)override{
2021-09-07 16:57:48 +08:00
return std::string("Tab")+std::to_string(position);
}
float getPageWidth(int position)override{return 1.f;}//if returned calue <1 OffscreenPageLimit must be larger to workfine
2021-09-07 16:57:48 +08:00
};
2021-09-03 18:05:15 +08:00
int main(int argc,const char*argv[]){
App app(argc,argv);
AnimationHandler::getInstance().setFrameDelay(200);
2021-09-03 18:05:15 +08:00
Window*w=new Window(0,0,1280,600);
2021-09-07 16:57:48 +08:00
MyPageAdapter*gpAdapter=new MyPageAdapter();
2021-09-03 18:05:15 +08:00
2021-09-07 16:57:48 +08:00
LinearLayout*layout=new LinearLayout(1280,600);
layout->setOrientation(LinearLayout::VERTICAL);
TabLayout* tab=new TabLayout(1280,36);
2022-01-05 16:28:21 +08:00
ViewPager* pager=new ViewPager(1280,560);
pager->setOffscreenPageLimit(gpAdapter->getCount());
pager->setOverScrollMode(View::OVER_SCROLL_ALWAYS);
2021-09-07 16:57:48 +08:00
pager->setAdapter(gpAdapter);
pager->setBackgroundColor(0xFFF3333);
2022-01-05 16:28:21 +08:00
tab->setSelectedTabIndicatorColor(0x8000FF00);
tab->setSelectedTabIndicatorHeight(4);
tab->setTabIndicatorGravity(Gravity::BOTTOM);//TOP/BOTTOM/CENTER_VERTICAL/FILL_VERTICAL
2021-09-07 16:57:48 +08:00
tab->setupWithViewPager(pager);
2022-01-05 16:28:21 +08:00
layout->addView(tab).setId(1);
2021-09-07 16:57:48 +08:00
layout->addView(pager).setId(10);
tab->setTabTextColors(0xFFFF0000,0xFF00FF00);
2021-09-07 16:57:48 +08:00
w->addView(layout);
2021-09-03 18:05:15 +08:00
w->requestLayout();
int result = app.exec();
delete gpAdapter;
return result;
2021-09-03 18:05:15 +08:00
}