Cdroid/apps/samples/viewpager1.cc

61 lines
2.4 KiB
C++
Raw Normal View History

2021-06-24 10:12:55 +08:00
#include <windows.h>
#include <widget/simplemonthview.h>
static int mPageCount=5;
2021-06-24 10:12:55 +08:00
class MyPageAdapter:public PagerAdapter{
public:
int getCount(){return mPageCount;}
2021-06-24 10:12:55 +08:00
bool isViewFromObject(View* view, void*object) { return view==object;}
void* instantiateItem(ViewGroup* container, int position) {
SimpleMonthView*sm=new SimpleMonthView(100,100);
sm->setMonthParams(23,Calendar::MAY+position,2021,-1,1,31);
container->addView(sm);
return sm;
2021-06-24 10:12:55 +08:00
}
void destroyItem(ViewGroup* container, int position,void* object){
container->removeView((View*)object);
}
2021-08-26 17:50:15 +08:00
float getPageWidth(int position){return 1.f;}//if returned calue <1 OffscreenPageLimit must be larger to workfine
2021-06-24 10:12:55 +08:00
};
int main(int argc,const char*argv[]){
App app(argc,argv);
Window*w=new Window(0,0,800,600);
HorizontalScrollView* hs=new HorizontalScrollView(800,400);
LinearLayout*layout=new LinearLayout(400,100);
2021-06-24 10:12:55 +08:00
ColorStateList*cl=ColorStateList::inflate(nullptr,"/home/houzh/Miniwin/src/gui/res/color/textview.xml");
layout->setId(10);
hs->addView(layout);
2021-06-24 10:12:55 +08:00
hs->setOverScrollMode(View::OVER_SCROLL_ALWAYS);
w->addView(hs).setId(1);
if(argc>1)mPageCount=std::max(5L,std::strtol(argv[1],nullptr,10));
2021-06-24 10:12:55 +08:00
for(int i=0;i<16;i++){
Button*btn=new Button("Hello Button"+std::to_string(i),150,30);
btn->setPadding(5,5,5,5);
btn->setTextColor(cl);
btn->setTextSize(30);
btn->setBackgroundColor(0xFF000000|i*20<<8|i*8);
btn->setId(100+i);
btn->setTextAlignment(View::TEXT_ALIGNMENT_CENTER);
layout->addView(btn,new LinearLayout::LayoutParams(800,LayoutParams::WRAP_CONTENT));
2021-06-24 10:12:55 +08:00
}
ViewPager*pager=new ViewPager(800,560);
2021-06-24 10:12:55 +08:00
MyPageAdapter*gpAdapter=new MyPageAdapter();
pager->setOffscreenPageLimit(3);
pager->setAdapter(gpAdapter);
2021-06-24 10:12:55 +08:00
ViewPager::OnPageChangeListener listener={nullptr,nullptr,nullptr};
listener.onPageSelected=[&](int position){
//hs->
};
listener.onPageScrolled=[&](int position, float positionOffset, int positionOffsetPixels){
hs->scrollTo(position*pager->getWidth()+positionOffsetPixels,0);
2021-06-24 10:12:55 +08:00
};
pager->setOnPageChangeListener(listener);
pager->setOverScrollMode(View::OVER_SCROLL_ALWAYS);
w->addView(pager).setPos(0,40);
2021-06-24 10:12:55 +08:00
gpAdapter->notifyDataSetChanged();
pager->setCurrentItem(0);
2021-06-24 10:12:55 +08:00
w->requestLayout();
app.exec();
}