android左右滑动加载分页以及动态加载数据
16lz
2021-12-04
android UI 往右滑动,滑动到最后一页就自动加载数据并显示
如图:
Java代码
Java代码
Java代码
Java代码
main.xml
Xml代码
app_item.xml
Xml代码
如图:
Java代码
- packagecn.anycall.ju;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importjava.util.List;
- importjava.util.Map;
- importandroid.app.Activity;
- importandroid.content.ActivityNotFoundException;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.content.pm.ResolveInfo;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Looper;
- importandroid.os.Message;
- importandroid.view.KeyEvent;
- importandroid.view.View;
- importandroid.view.Window;
- importandroid.widget.AdapterView;
- importandroid.widget.AdapterView.OnItemClickListener;
- importandroid.widget.GridView;
- importandroid.widget.Toast;
- importcn.anycall.ju.ScrollLayout.OnScreenChangeListenerDataLoad;
- /**
- *GridView分页显示安装的应用程序
- */
- publicclassAllAppListextendsActivity{
- privateScrollLayoutmScrollLayout;
- privatestaticfinalfloatAPP_PAGE_SIZE=4.0f;
- privateContextmContext;
- privatePageControlViewpageControl;
- publicMyHandlermyHandler;
- publicintn=0;
- privateDataLoadingdataLoad;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- mContext=this;
- setContentView(R.layout.main);
- dataLoad=newDataLoading();
- mScrollLayout=(ScrollLayout)findViewById(R.id.ScrollLayoutTest);
- myHandler=newMyHandler(this,1);
- //起一个线程更新数据
- MyThreadm=newMyThread();
- newThread(m).start();
- }
- /**
- *gridView的onItemLick响应事件
- */
- publicOnItemClickListenerlistener=newOnItemClickListener(){
- publicvoidonItemClick(AdapterView<?>parent,Viewview,intposition,
- longid){
- //TODOAuto-generatedmethodstub
- System.out.println("position="+position);
- }
- };
- @Override
- protectedvoidonDestroy(){
- //TODOAuto-generatedmethodstub
- android.os.Process.killProcess(android.os.Process.myPid());
- super.onDestroy();
- }
- @Override
- publicbooleanonKeyDown(intkeyCode,KeyEventevent){
- //TODOAuto-generatedmethodstub
- if(keyCode==KeyEvent.KEYCODE_BACK){
- finish();
- returntrue;
- }
- returnsuper.onKeyDown(keyCode,event);
- }
- //更新后台数据
- classMyThreadimplementsRunnable{
- publicvoidrun(){
- try{
- Thread.sleep(1000*3);
- }catch(InterruptedExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- Stringmsglist="1";
- Messagemsg=newMessage();
- Bundleb=newBundle();//存放数据
- b.putString("rmsg",msglist);
- msg.setData(b);
- AllAppList.this.myHandler.sendMessage(msg);//向Handler发送消息,更新UI
- }
- }
- classMyHandlerextendsHandler{
- privateAllAppListmContext;
- publicMyHandler(Contextconn,inta){
- mContext=(AllAppList)conn;
- }
- publicMyHandler(LooperL){
- super(L);
- }
- //子类必须重写此方法,接受数据
- @Override
- publicvoidhandleMessage(Messagemsg){
- //TODOAuto-generatedmethodstub
- super.handleMessage(msg);
- Bundleb=msg.getData();
- Stringrmsg=b.getString("rmsg");
- if("1".equals(rmsg)){
- //donothing
- List<Map>list=newArrayList<Map>();
- for(inti=0;i<16;i++){
- n++;
- Mapmap=newHashMap();
- map.put("name",n);
- list.add(map);
- }
- intpageNo=(int)Math.ceil(list.size()/APP_PAGE_SIZE);
- for(inti=0;i<pageNo;i++){
- GridViewappPage=newGridView(mContext);
- //getthe"i"pagedata
- appPage.setAdapter(newAppAdapter(mContext,list,i));
- appPage.setNumColumns(2);
- appPage.setOnItemClickListener(listener);
- mScrollLayout.addView(appPage);
- }
- //加载分页
- pageControl=(PageControlView)findViewById(R.id.pageControl);
- pageControl.bindScrollViewGroup(mScrollLayout);
- //加载分页数据
- dataLoad.bindScrollViewGroup(mScrollLayout);
- }
- }
- }
- //分页数据
- classDataLoading{
- privateintcount;
- publicvoidbindScrollViewGroup(ScrollLayoutscrollViewGroup){
- this.count=scrollViewGroup.getChildCount();
- scrollViewGroup.setOnScreenChangeListenerDataLoad(newOnScreenChangeListenerDataLoad(){
- publicvoidonScreenChange(intcurrentIndex){
- //TODOAuto-generatedmethodstub
- generatePageControl(currentIndex);
- }
- });
- }
- privatevoidgeneratePageControl(intcurrentIndex){
- //如果到最后一页,就加载16条记录
- if(count==currentIndex+1){
- MyThreadm=newMyThread();
- newThread(m).start();
- }
- }
- }
- }
- packagecn.anycall.ju;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importjava.util.List;
- importjava.util.Map;
- importandroid.app.Activity;
- importandroid.content.ActivityNotFoundException;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.content.pm.ResolveInfo;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Looper;
- importandroid.os.Message;
- importandroid.view.KeyEvent;
- importandroid.view.View;
- importandroid.view.Window;
- importandroid.widget.AdapterView;
- importandroid.widget.AdapterView.OnItemClickListener;
- importandroid.widget.GridView;
- importandroid.widget.Toast;
- importcn.anycall.ju.ScrollLayout.OnScreenChangeListenerDataLoad;
- /**
- *GridView分页显示安装的应用程序
- */
- publicclassAllAppListextendsActivity{
- privateScrollLayoutmScrollLayout;
- privatestaticfinalfloatAPP_PAGE_SIZE=4.0f;
- privateContextmContext;
- privatePageControlViewpageControl;
- publicMyHandlermyHandler;
- publicintn=0;
- privateDataLoadingdataLoad;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- mContext=this;
- setContentView(R.layout.main);
- dataLoad=newDataLoading();
- mScrollLayout=(ScrollLayout)findViewById(R.id.ScrollLayoutTest);
- myHandler=newMyHandler(this,1);
- //起一个线程更新数据
- MyThreadm=newMyThread();
- newThread(m).start();
- }
- /**
- *gridView的onItemLick响应事件
- */
- publicOnItemClickListenerlistener=newOnItemClickListener(){
- publicvoidonItemClick(AdapterView<?>parent,Viewview,intposition,
- longid){
- //TODOAuto-generatedmethodstub
- System.out.println("position="+position);
- }
- };
- @Override
- protectedvoidonDestroy(){
- //TODOAuto-generatedmethodstub
- android.os.Process.killProcess(android.os.Process.myPid());
- super.onDestroy();
- }
- @Override
- publicbooleanonKeyDown(intkeyCode,KeyEventevent){
- //TODOAuto-generatedmethodstub
- if(keyCode==KeyEvent.KEYCODE_BACK){
- finish();
- returntrue;
- }
- returnsuper.onKeyDown(keyCode,event);
- }
- //更新后台数据
- classMyThreadimplementsRunnable{
- publicvoidrun(){
- try{
- Thread.sleep(1000*3);
- }catch(InterruptedExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- Stringmsglist="1";
- Messagemsg=newMessage();
- Bundleb=newBundle();//存放数据
- b.putString("rmsg",msglist);
- msg.setData(b);
- AllAppList.this.myHandler.sendMessage(msg);//向Handler发送消息,更新UI
- }
- }
- classMyHandlerextendsHandler{
- privateAllAppListmContext;
- publicMyHandler(Contextconn,inta){
- mContext=(AllAppList)conn;
- }
- publicMyHandler(LooperL){
- super(L);
- }
- //子类必须重写此方法,接受数据
- @Override
- publicvoidhandleMessage(Messagemsg){
- //TODOAuto-generatedmethodstub
- super.handleMessage(msg);
- Bundleb=msg.getData();
- Stringrmsg=b.getString("rmsg");
- if("1".equals(rmsg)){
- //donothing
- List<Map>list=newArrayList<Map>();
- for(inti=0;i<16;i++){
- n++;
- Mapmap=newHashMap();
- map.put("name",n);
- list.add(map);
- }
- intpageNo=(int)Math.ceil(list.size()/APP_PAGE_SIZE);
- for(inti=0;i<pageNo;i++){
- GridViewappPage=newGridView(mContext);
- //getthe"i"pagedata
- appPage.setAdapter(newAppAdapter(mContext,list,i));
- appPage.setNumColumns(2);
- appPage.setOnItemClickListener(listener);
- mScrollLayout.addView(appPage);
- }
- //加载分页
- pageControl=(PageControlView)findViewById(R.id.pageControl);
- pageControl.bindScrollViewGroup(mScrollLayout);
- //加载分页数据
- dataLoad.bindScrollViewGroup(mScrollLayout);
- }
- }
- }
- //分页数据
- classDataLoading{
- privateintcount;
- publicvoidbindScrollViewGroup(ScrollLayoutscrollViewGroup){
- this.count=scrollViewGroup.getChildCount();
- scrollViewGroup.setOnScreenChangeListenerDataLoad(newOnScreenChangeListenerDataLoad(){
- publicvoidonScreenChange(intcurrentIndex){
- //TODOAuto-generatedmethodstub
- generatePageControl(currentIndex);
- }
- });
- }
- privatevoidgeneratePageControl(intcurrentIndex){
- //如果到最后一页,就加载16条记录
- if(count==currentIndex+1){
- MyThreadm=newMyThread();
- newThread(m).start();
- }
- }
- }
- }
Java代码
- packagecn.anycall.ju;
- importjava.util.ArrayList;
- importjava.util.List;
- importjava.util.Map;
- importandroid.content.Context;
- importandroid.content.pm.PackageManager;
- importandroid.content.pm.ResolveInfo;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.ImageView;
- importandroid.widget.TextView;
- importcn.anycall.ju.R;
- publicclassAppAdapterextendsBaseAdapter{
- privateList<Map>mList;
- privateContextmContext;
- publicstaticfinalintAPP_PAGE_SIZE=4;
- privatePackageManagerpm;
- publicAppAdapter(Contextcontext,List<Map>list,intpage){
- mContext=context;
- pm=context.getPackageManager();
- mList=newArrayList<Map>();
- inti=page*APP_PAGE_SIZE;
- intiEnd=i+APP_PAGE_SIZE;
- while((i<list.size())&&(i<iEnd)){
- mList.add(list.get(i));
- i++;
- }
- }
- publicintgetCount(){
- //TODOAuto-generatedmethodstub
- returnmList.size();
- }
- publicObjectgetItem(intposition){
- //TODOAuto-generatedmethodstub
- returnmList.get(position);
- }
- publiclonggetItemId(intposition){
- //TODOAuto-generatedmethodstub
- returnposition;
- }
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- //TODOAuto-generatedmethodstub
- MapappInfo=mList.get(position);
- AppItemappItem;
- if(convertView==null){
- Viewv=LayoutInflater.from(mContext).inflate(R.layout.app_item,null);
- appItem=newAppItem();
- appItem.mAppIcon=(ImageView)v.findViewById(R.id.imgdetail);
- appItem.mAppName=(TextView)v.findViewById(R.id.tuaninfo);
- v.setTag(appItem);
- convertView=v;
- }else{
- appItem=(AppItem)convertView.getTag();
- }
- //settheicon
- appItem.mAppIcon.setImageResource(R.drawable.icon);
- //settheappname
- appItem.mAppName.setText(appInfo.get("name").toString());
- returnconvertView;
- }
- /**
- *每个应用显示的内容,包括图标和名称
- *@authorYao.GUET
- *
- */
- classAppItem{
- ImageViewmAppIcon;
- TextViewmAppName;
- }
- }
- packagecn.anycall.ju;
- importjava.util.ArrayList;
- importjava.util.List;
- importjava.util.Map;
- importandroid.content.Context;
- importandroid.content.pm.PackageManager;
- importandroid.content.pm.ResolveInfo;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.ImageView;
- importandroid.widget.TextView;
- importcn.anycall.ju.R;
- publicclassAppAdapterextendsBaseAdapter{
- privateList<Map>mList;
- privateContextmContext;
- publicstaticfinalintAPP_PAGE_SIZE=4;
- privatePackageManagerpm;
- publicAppAdapter(Contextcontext,List<Map>list,intpage){
- mContext=context;
- pm=context.getPackageManager();
- mList=newArrayList<Map>();
- inti=page*APP_PAGE_SIZE;
- intiEnd=i+APP_PAGE_SIZE;
- while((i<list.size())&&(i<iEnd)){
- mList.add(list.get(i));
- i++;
- }
- }
- publicintgetCount(){
- //TODOAuto-generatedmethodstub
- returnmList.size();
- }
- publicObjectgetItem(intposition){
- //TODOAuto-generatedmethodstub
- returnmList.get(position);
- }
- publiclonggetItemId(intposition){
- //TODOAuto-generatedmethodstub
- returnposition;
- }
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- //TODOAuto-generatedmethodstub
- MapappInfo=mList.get(position);
- AppItemappItem;
- if(convertView==null){
- Viewv=LayoutInflater.from(mContext).inflate(R.layout.app_item,null);
- appItem=newAppItem();
- appItem.mAppIcon=(ImageView)v.findViewById(R.id.imgdetail);
- appItem.mAppName=(TextView)v.findViewById(R.id.tuaninfo);
- v.setTag(appItem);
- convertView=v;
- }else{
- appItem=(AppItem)convertView.getTag();
- }
- //settheicon
- appItem.mAppIcon.setImageResource(R.drawable.icon);
- //settheappname
- appItem.mAppName.setText(appInfo.get("name").toString());
- returnconvertView;
- }
- /**
- *每个应用显示的内容,包括图标和名称
- *@authorYao.GUET
- *
- */
- classAppItem{
- ImageViewmAppIcon;
- TextViewmAppName;
- }
- }
Java代码
- packagecn.anycall.ju;
- importandroid.content.Context;
- importandroid.util.AttributeSet;
- importandroid.widget.ImageView;
- importandroid.widget.LinearLayout;
- importcn.anycall.ju.R;
- importcn.anycall.ju.ScrollLayout.OnScreenChangeListener;
- publicclassPageControlViewextendsLinearLayout{
- privateContextcontext;
- privateintcount;
- publicvoidbindScrollViewGroup(ScrollLayoutscrollViewGroup){
- this.count=scrollViewGroup.getChildCount();
- System.out.println("count="+count);
- generatePageControl(scrollViewGroup.getCurrentScreenIndex());
- scrollViewGroup.setOnScreenChangeListener(newOnScreenChangeListener(){
- publicvoidonScreenChange(intcurrentIndex){
- //TODOAuto-generatedmethodstub
- generatePageControl(currentIndex);
- }
- });
- }
- publicPageControlView(Contextcontext){
- super(context);
- this.init(context);
- }
- publicPageControlView(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- this.init(context);
- }
- privatevoidinit(Contextcontext){
- this.context=context;
- }
- privatevoidgeneratePageControl(intcurrentIndex){
- this.removeAllViews();
- intpageNum=6;//显示多少个
- intpageNo=currentIndex+1;//第几页
- intpageSum=this.count;//总共多少页
- if(pageSum>1){
- intcurrentNum=(pageNo%pageNum==0?(pageNo/pageNum)-1
- :(int)(pageNo/pageNum))
- *pageNum;
- if(currentNum<0)
- currentNum=0;
- if(pageNo>pageNum){
- ImageViewimageView=newImageView(context);
- imageView.setImageResource(R.drawable.zuo);
- this.addView(imageView);
- }
- for(inti=0;i<pageNum;i++){
- if((currentNum+i+1)>pageSum||pageSum<2)
- break;
- ImageViewimageView=newImageView(context);
- if(currentNum+i+1==pageNo){
- imageView.setImageResource(R.drawable.page_indicator_focused);
- }else{
- imageView.setImageResource(R.drawable.page_indicator);
- }
- this.addView(imageView);
- }
- if(pageSum>(currentNum+pageNum)){
- ImageViewimageView=newImageView(context);
- imageView.setImageResource(R.drawable.you);
- this.addView(imageView);
- }
- }
- }
- }
- packagecn.anycall.ju;
- importandroid.content.Context;
- importandroid.util.AttributeSet;
- importandroid.widget.ImageView;
- importandroid.widget.LinearLayout;
- importcn.anycall.ju.R;
- importcn.anycall.ju.ScrollLayout.OnScreenChangeListener;
- publicclassPageControlViewextendsLinearLayout{
- privateContextcontext;
- privateintcount;
- publicvoidbindScrollViewGroup(ScrollLayoutscrollViewGroup){
- this.count=scrollViewGroup.getChildCount();
- System.out.println("count="+count);
- generatePageControl(scrollViewGroup.getCurrentScreenIndex());
- scrollViewGroup.setOnScreenChangeListener(newOnScreenChangeListener(){
- publicvoidonScreenChange(intcurrentIndex){
- //TODOAuto-generatedmethodstub
- generatePageControl(currentIndex);
- }
- });
- }
- publicPageControlView(Contextcontext){
- super(context);
- this.init(context);
- }
- publicPageControlView(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- this.init(context);
- }
- privatevoidinit(Contextcontext){
- this.context=context;
- }
- privatevoidgeneratePageControl(intcurrentIndex){
- this.removeAllViews();
- intpageNum=6;//显示多少个
- intpageNo=currentIndex+1;//第几页
- intpageSum=this.count;//总共多少页
- if(pageSum>1){
- intcurrentNum=(pageNo%pageNum==0?(pageNo/pageNum)-1
- :(int)(pageNo/pageNum))
- *pageNum;
- if(currentNum<0)
- currentNum=0;
- if(pageNo>pageNum){
- ImageViewimageView=newImageView(context);
- imageView.setImageResource(R.drawable.zuo);
- this.addView(imageView);
- }
- for(inti=0;i<pageNum;i++){
- if((currentNum+i+1)>pageSum||pageSum<2)
- break;
- ImageViewimageView=newImageView(context);
- if(currentNum+i+1==pageNo){
- imageView.setImageResource(R.drawable.page_indicator_focused);
- }else{
- imageView.setImageResource(R.drawable.page_indicator);
- }
- this.addView(imageView);
- }
- if(pageSum>(currentNum+pageNum)){
- ImageViewimageView=newImageView(context);
- imageView.setImageResource(R.drawable.you);
- this.addView(imageView);
- }
- }
- }
- }
Java代码
- packagecn.anycall.ju;
- importandroid.content.Context;
- importandroid.graphics.Canvas;
- importandroid.util.AttributeSet;
- importandroid.util.Log;
- importandroid.view.MotionEvent;
- importandroid.view.VelocityTracker;
- importandroid.view.View;
- importandroid.view.ViewConfiguration;
- importandroid.view.ViewGroup;
- importandroid.widget.Scroller;
- /**
- *仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类
- *
- */
- publicclassScrollLayoutextendsViewGroup{
- privatestaticfinalStringTAG="ScrollLayout";
- privateScrollermScroller;
- privateVelocityTrackermVelocityTracker;
- privateintmCurScreen;
- privateintmDefaultScreen=0;
- privatestaticfinalintTOUCH_STATE_REST=0;
- privatestaticfinalintTOUCH_STATE_SCROLLING=1;
- privatestaticfinalintSNAP_VELOCITY=600;
- privateintmTouchState=TOUCH_STATE_REST;
- privateintmTouchSlop;
- privatefloatmLastMotionX;
- privatefloatmLastMotionY;
- privateintcurrentScreenIndex=0;
- publicintgetCurrentScreenIndex(){
- returncurrentScreenIndex;
- }
- publicvoidsetCurrentScreenIndex(intcurrentScreenIndex){
- this.currentScreenIndex=currentScreenIndex;
- }
- publicScrollLayout(Contextcontext,AttributeSetattrs){
- this(context,attrs,0);
- //TODOAuto-generatedconstructorstub
- }
- publicScrollLayout(Contextcontext,AttributeSetattrs,intdefStyle){
- super(context,attrs,defStyle);
- //TODOAuto-generatedconstructorstub
- mScroller=newScroller(context);
- mCurScreen=mDefaultScreen;
- mTouchSlop=ViewConfiguration.get(getContext()).getScaledTouchSlop();
- }
- @Override
- protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
- //TODOAuto-generatedmethodstub
- intchildLeft=0;
- finalintchildCount=getChildCount();
- System.out.println("childCount="+childCount);
- for(inti=0;i<childCount;i++){
- finalViewchildView=getChildAt(i);
- if(childView.getVisibility()!=View.GONE){
- finalintchildWidth=childView.getMeasuredWidth();
- childView.layout(childLeft,0,childLeft+childWidth,
- childView.getMeasuredHeight());
- childLeft+=childWidth;
- }
- }
- }
- @Override
- protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
- Log.e(TAG,"onMeasure");
- super.onMeasure(widthMeasureSpec,heightMeasureSpec);
- finalintwidth=MeasureSpec.getSize(widthMeasureSpec);
- finalintwidthMode=MeasureSpec.getMode(widthMeasureSpec);
- if(widthMode!=MeasureSpec.EXACTLY){
- thrownewIllegalStateException(
- "ScrollLayoutonlycanmCurScreenrunatEXACTLYmode!");
- }
- finalintheightMode=MeasureSpec.getMode(heightMeasureSpec);
- if(heightMode!=MeasureSpec.EXACTLY){
- thrownewIllegalStateException(
- "ScrollLayoutonlycanrunatEXACTLYmode!");
- }
- //ThechildrenaregiventhesamewidthandheightasthescrollLayout
- finalintcount=getChildCount();
- for(inti=0;i<count;i++){
- getChildAt(i).measure(widthMeasureSpec,heightMeasureSpec);
- }
- System.out.println("movingtoscreen"+mCurScreen);
- scrollTo(mCurScreen*width,0);
- }
- /**
- *Accordingtothepositionofcurrentlayoutscrolltothedestination
- *page.
- */
- publicvoidsnapToDestination(){
- finalintscreenWidth=getWidth();
- finalintdestScreen=(getScrollX()+screenWidth/2)/screenWidth;
- snapToScreen(destScreen);
- }
- publicvoidsnapToScreen(intwhichScreen){
- //getthevalidlayoutpage
- whichScreen=Math.max(0,Math.min(whichScreen,getChildCount()-1));
- if(getScrollX()!=(whichScreen*getWidth())){
- finalintdelta=whichScreen*getWidth()-getScrollX();
- mScroller.startScroll(getScrollX(),0,delta,0,
- Math.abs(delta)*2);
- mCurScreen=whichScreen;
- invalidate();//Redrawthelayout
- }
- }
- publicvoidsetToScreen(intwhichScreen){
- whichScreen=Math.max(0,Math.min(whichScreen,getChildCount()-1));
- mCurScreen=whichScreen;
- scrollTo(whichScreen*getWidth(),0);
- }
- publicintgetCurScreen(){
- returnmCurScreen;
- }
- @Override
- publicvoidcomputeScroll(){
- //TODOAuto-generatedmethodstub
- if(mScroller.computeScrollOffset()){
- scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
- postInvalidate();
- }
- }
- @Override
- publicbooleanonTouchEvent(MotionEventevent){
- //TODOAuto-generatedmethodstub
- if(mVelocityTracker==null){
- mVelocityTracker=VelocityTracker.obtain();
- }
- mVelocityTracker.addMovement(event);
- finalintaction=event.getAction();
- finalfloatx=event.getX();
- finalfloaty=event.getY();
- switch(action){
- caseMotionEvent.ACTION_DOWN:
- Log.e(TAG,"eventdown!");
- if(!mScroller.isFinished()){
- mScroller.abortAnimation();
- }
- mLastMotionX=x;
- break;
- caseMotionEvent.ACTION_MOVE:
- intdeltaX=(int)(mLastMotionX-x);
- mLastMotionX=x;
- scrollBy(deltaX,0);
- break;
- caseMotionEvent.ACTION_UP:
- Log.e(TAG,"event:up");
- //if(mTouchState==TOUCH_STATE_SCROLLING){
- finalVelocityTrackervelocityTracker=mVelocityTracker;
- velocityTracker.computeCurrentVelocity(1000);
- intvelocityX=(int)velocityTracker.getXVelocity();
- Log.e(TAG,"velocityX:"+velocityX);
- if(velocityX>SNAP_VELOCITY&&mCurScreen>0){
- //Flingenoughtomoveleft
- Log.e(TAG,"snapleft");
- onScreenChangeListener.onScreenChange(mCurScreen-1);
- System.out.println("mCurScreen="+(mCurScreen-1));
- snapToScreen(mCurScreen-1);
- }elseif(velocityX<-SNAP_VELOCITY
- &&mCurScreen<getChildCount()-1){
- //Flingenoughtomoveright
- Log.e(TAG,"snapright");
- onScreenChangeListener.onScreenChange(mCurScreen+1);
- //只往右移动才加载数据
- onScreenChangeListenerDataLoad.onScreenChange(mCurScreen+1);
- snapToScreen(mCurScreen+1);
- }else{
- snapToDestination();
- }
- if(mVelocityTracker!=null){
- mVelocityTracker.recycle();
- mVelocityTracker=null;
- }
- //}
- mTouchState=TOUCH_STATE_REST;
- break;
- caseMotionEvent.ACTION_CANCEL:
- mTouchState=TOUCH_STATE_REST;
- break;
- }
- returntrue;
- }
- @Override
- publicbooleanonInterceptTouchEvent(MotionEventev){
- //TODOAuto-generatedmethodstub
- Log.e(TAG,"onInterceptTouchEvent-slop:"+mTouchSlop);
- finalintaction=ev.getAction();
- if((action==MotionEvent.ACTION_MOVE)
- &&(mTouchState!=TOUCH_STATE_REST)){
- returntrue;
- }
- finalfloatx=ev.getX();
- finalfloaty=ev.getY();
- switch(action){
- caseMotionEvent.ACTION_MOVE:
- finalintxDiff=(int)Math.abs(mLastMotionX-x);
- if(xDiff>mTouchSlop){
- mTouchState=TOUCH_STATE_SCROLLING;
- }
- break;
- caseMotionEvent.ACTION_DOWN:
- mLastMotionX=x;
- mLastMotionY=y;
- mTouchState=mScroller.isFinished()?TOUCH_STATE_REST
- :TOUCH_STATE_SCROLLING;
- break;
- caseMotionEvent.ACTION_CANCEL:
- caseMotionEvent.ACTION_UP:
- mTouchState=TOUCH_STATE_REST;
- break;
- }
- returnmTouchState!=TOUCH_STATE_REST;
- }
- //分页监听
- publicinterfaceOnScreenChangeListener{
- voidonScreenChange(intcurrentIndex);
- }
- privateOnScreenChangeListeneronScreenChangeListener;
- publicvoidsetOnScreenChangeListener(
- OnScreenChangeListeneronScreenChangeListener){
- this.onScreenChangeListener=onScreenChangeListener;
- }
- //动态数据监听
- publicinterfaceOnScreenChangeListenerDataLoad{
- voidonScreenChange(intcurrentIndex);
- }
- privateOnScreenChangeListenerDataLoadonScreenChangeListenerDataLoad;
- publicvoidsetOnScreenChangeListenerDataLoad(OnScreenChangeListenerDataLoadonScreenChangeListenerDataLoad){
- this.onScreenChangeListenerDataLoad=onScreenChangeListenerDataLoad;
- }
- }
- packagecn.anycall.ju;
- importandroid.content.Context;
- importandroid.graphics.Canvas;
- importandroid.util.AttributeSet;
- importandroid.util.Log;
- importandroid.view.MotionEvent;
- importandroid.view.VelocityTracker;
- importandroid.view.View;
- importandroid.view.ViewConfiguration;
- importandroid.view.ViewGroup;
- importandroid.widget.Scroller;
- /**
- *仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类
- *
- */
- publicclassScrollLayoutextendsViewGroup{
- privatestaticfinalStringTAG="ScrollLayout";
- privateScrollermScroller;
- privateVelocityTrackermVelocityTracker;
- privateintmCurScreen;
- privateintmDefaultScreen=0;
- privatestaticfinalintTOUCH_STATE_REST=0;
- privatestaticfinalintTOUCH_STATE_SCROLLING=1;
- privatestaticfinalintSNAP_VELOCITY=600;
- privateintmTouchState=TOUCH_STATE_REST;
- privateintmTouchSlop;
- privatefloatmLastMotionX;
- privatefloatmLastMotionY;
- privateintcurrentScreenIndex=0;
- publicintgetCurrentScreenIndex(){
- returncurrentScreenIndex;
- }
- publicvoidsetCurrentScreenIndex(intcurrentScreenIndex){
- this.currentScreenIndex=currentScreenIndex;
- }
- publicScrollLayout(Contextcontext,AttributeSetattrs){
- this(context,attrs,0);
- //TODOAuto-generatedconstructorstub
- }
- publicScrollLayout(Contextcontext,AttributeSetattrs,intdefStyle){
- super(context,attrs,defStyle);
- //TODOAuto-generatedconstructorstub
- mScroller=newScroller(context);
- mCurScreen=mDefaultScreen;
- mTouchSlop=ViewConfiguration.get(getContext()).getScaledTouchSlop();
- }
- @Override
- protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
- //TODOAuto-generatedmethodstub
- intchildLeft=0;
- finalintchildCount=getChildCount();
- System.out.println("childCount="+childCount);
- for(inti=0;i<childCount;i++){
- finalViewchildView=getChildAt(i);
- if(childView.getVisibility()!=View.GONE){
- finalintchildWidth=childView.getMeasuredWidth();
- childView.layout(childLeft,0,childLeft+childWidth,
- childView.getMeasuredHeight());
- childLeft+=childWidth;
- }
- }
- }
- @Override
- protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
- Log.e(TAG,"onMeasure");
- super.onMeasure(widthMeasureSpec,heightMeasureSpec);
- finalintwidth=MeasureSpec.getSize(widthMeasureSpec);
- finalintwidthMode=MeasureSpec.getMode(widthMeasureSpec);
- if(widthMode!=MeasureSpec.EXACTLY){
- thrownewIllegalStateException(
- "ScrollLayoutonlycanmCurScreenrunatEXACTLYmode!");
- }
- finalintheightMode=MeasureSpec.getMode(heightMeasureSpec);
- if(heightMode!=MeasureSpec.EXACTLY){
- thrownewIllegalStateException(
- "ScrollLayoutonlycanrunatEXACTLYmode!");
- }
- //ThechildrenaregiventhesamewidthandheightasthescrollLayout
- finalintcount=getChildCount();
- for(inti=0;i<count;i++){
- getChildAt(i).measure(widthMeasureSpec,heightMeasureSpec);
- }
- System.out.println("movingtoscreen"+mCurScreen);
- scrollTo(mCurScreen*width,0);
- }
- /**
- *Accordingtothepositionofcurrentlayoutscrolltothedestination
- *page.
- */
- publicvoidsnapToDestination(){
- finalintscreenWidth=getWidth();
- finalintdestScreen=(getScrollX()+screenWidth/2)/screenWidth;
- snapToScreen(destScreen);
- }
- publicvoidsnapToScreen(intwhichScreen){
- //getthevalidlayoutpage
- whichScreen=Math.max(0,Math.min(whichScreen,getChildCount()-1));
- if(getScrollX()!=(whichScreen*getWidth())){
- finalintdelta=whichScreen*getWidth()-getScrollX();
- mScroller.startScroll(getScrollX(),0,delta,0,
- Math.abs(delta)*2);
- mCurScreen=whichScreen;
- invalidate();//Redrawthelayout
- }
- }
- publicvoidsetToScreen(intwhichScreen){
- whichScreen=Math.max(0,Math.min(whichScreen,getChildCount()-1));
- mCurScreen=whichScreen;
- scrollTo(whichScreen*getWidth(),0);
- }
- publicintgetCurScreen(){
- returnmCurScreen;
- }
- @Override
- publicvoidcomputeScroll(){
- //TODOAuto-generatedmethodstub
- if(mScroller.computeScrollOffset()){
- scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
- postInvalidate();
- }
- }
- @Override
- publicbooleanonTouchEvent(MotionEventevent){
- //TODOAuto-generatedmethodstub
- if(mVelocityTracker==null){
- mVelocityTracker=VelocityTracker.obtain();
- }
- mVelocityTracker.addMovement(event);
- finalintaction=event.getAction();
- finalfloatx=event.getX();
- finalfloaty=event.getY();
- switch(action){
- caseMotionEvent.ACTION_DOWN:
- Log.e(TAG,"eventdown!");
- if(!mScroller.isFinished()){
- mScroller.abortAnimation();
- }
- mLastMotionX=x;
- break;
- caseMotionEvent.ACTION_MOVE:
- intdeltaX=(int)(mLastMotionX-x);
- mLastMotionX=x;
- scrollBy(deltaX,0);
- break;
- caseMotionEvent.ACTION_UP:
- Log.e(TAG,"event:up");
- //if(mTouchState==TOUCH_STATE_SCROLLING){
- finalVelocityTrackervelocityTracker=mVelocityTracker;
- velocityTracker.computeCurrentVelocity(1000);
- intvelocityX=(int)velocityTracker.getXVelocity();
- Log.e(TAG,"velocityX:"+velocityX);
- if(velocityX>SNAP_VELOCITY&&mCurScreen>0){
- //Flingenoughtomoveleft
- Log.e(TAG,"snapleft");
- onScreenChangeListener.onScreenChange(mCurScreen-1);
- System.out.println("mCurScreen="+(mCurScreen-1));
- snapToScreen(mCurScreen-1);
- }elseif(velocityX<-SNAP_VELOCITY
- &&mCurScreen<getChildCount()-1){
- //Flingenoughtomoveright
- Log.e(TAG,"snapright");
- onScreenChangeListener.onScreenChange(mCurScreen+1);
- //只往右移动才加载数据
- onScreenChangeListenerDataLoad.onScreenChange(mCurScreen+1);
- snapToScreen(mCurScreen+1);
- }else{
- snapToDestination();
- }
- if(mVelocityTracker!=null){
- mVelocityTracker.recycle();
- mVelocityTracker=null;
- }
- //}
- mTouchState=TOUCH_STATE_REST;
- break;
- caseMotionEvent.ACTION_CANCEL:
- mTouchState=TOUCH_STATE_REST;
- break;
- }
- returntrue;
- }
- @Override
- publicbooleanonInterceptTouchEvent(MotionEventev){
- //TODOAuto-generatedmethodstub
- Log.e(TAG,"onInterceptTouchEvent-slop:"+mTouchSlop);
- finalintaction=ev.getAction();
- if((action==MotionEvent.ACTION_MOVE)
- &&(mTouchState!=TOUCH_STATE_REST)){
- returntrue;
- }
- finalfloatx=ev.getX();
- finalfloaty=ev.getY();
- switch(action){
- caseMotionEvent.ACTION_MOVE:
- finalintxDiff=(int)Math.abs(mLastMotionX-x);
- if(xDiff>mTouchSlop){
- mTouchState=TOUCH_STATE_SCROLLING;
- }
- break;
- caseMotionEvent.ACTION_DOWN:
- mLastMotionX=x;
- mLastMotionY=y;
- mTouchState=mScroller.isFinished()?TOUCH_STATE_REST
- :TOUCH_STATE_SCROLLING;
- break;
- caseMotionEvent.ACTION_CANCEL:
- caseMotionEvent.ACTION_UP:
- mTouchState=TOUCH_STATE_REST;
- break;
- }
- returnmTouchState!=TOUCH_STATE_REST;
- }
- //分页监听
- publicinterfaceOnScreenChangeListener{
- voidonScreenChange(intcurrentIndex);
- }
- privateOnScreenChangeListeneronScreenChangeListener;
- publicvoidsetOnScreenChangeListener(
- OnScreenChangeListeneronScreenChangeListener){
- this.onScreenChangeListener=onScreenChangeListener;
- }
- //动态数据监听
- publicinterfaceOnScreenChangeListenerDataLoad{
- voidonScreenChange(intcurrentIndex);
- }
- privateOnScreenChangeListenerDataLoadonScreenChangeListenerDataLoad;
- publicvoidsetOnScreenChangeListenerDataLoad(OnScreenChangeListenerDataLoadonScreenChangeListenerDataLoad){
- this.onScreenChangeListenerDataLoad=onScreenChangeListenerDataLoad;
- }
- }
main.xml
Xml代码
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextViewandroid:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="仿淘宝聚划算"/>
- <RelativeLayout
- android:id="@+id/myView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <cn.anycall.ju.ScrollLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/ScrollLayoutTest"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"android:background="#000000">
- </cn.anycall.ju.ScrollLayout>
- <cn.anycall.ju.PageControlView
- android:id="@+id/pageControl"
- android:layout_width="fill_parent"
- android:layout_height="40px"
- android:background="#8f00000f"
- android:layout_alignParentBottom="true"
- android:gravity="center"/>
- </RelativeLayout>
- </LinearLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextViewandroid:layout_width="fill_parent"
- android:layout_height="wrap_content"android:text="仿淘宝聚划算"/>
- <RelativeLayout
- android:id="@+id/myView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <cn.anycall.ju.ScrollLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/ScrollLayoutTest"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"android:background="#000000">
- </cn.anycall.ju.ScrollLayout>
- <cn.anycall.ju.PageControlView
- android:id="@+id/pageControl"
- android:layout_width="fill_parent"
- android:layout_height="40px"
- android:background="#8f00000f"
- android:layout_alignParentBottom="true"
- android:gravity="center"/>
- </RelativeLayout>
- </LinearLayout>
app_item.xml
Xml代码
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <RelativeLayoutandroid:id="@+id/alllayout"android:layout_width="wrap_content"android:layout_height="wrap_content">
- <RelativeLayoutandroid:id="@+id/imglayout"android:layout_width="160dp"android:layout_height="160dp"android:background="@drawable/mer_border">
- <ImageViewandroid:id="@+id/imgdetail"android:layout_width="145dp"android:layout_height="145dp"android:layout_margin="8dp"/>
- <TextViewandroid:id="@+id/price"android:layout_width="180dp"android:layout_height="wrap_content"android:text="12345"android:layout_alignParentBottom="true"android:background="#C02000"android:textColor="#FFFFFF"/>
- <TextViewandroid:id="@+id/look"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="去看看"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:background="#C02000"android:textColor="#FFFFFF"/>
- </RelativeLayout>
- <TextViewandroid:id="@+id/tuaninfo"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:textSize="16dp"
- android:maxLines="2"android:layout_below="@id/imglayout"
- android:ellipsize="end"android:text="dddddddddd"/>"
- </RelativeLayout>
- </RelativeLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <RelativeLayoutandroid:id="@+id/alllayout"android:layout_width="wrap_content"android:layout_height="wrap_content">
- <RelativeLayoutandroid:id="@+id/imglayout"android:layout_width="160dp"android:layout_height="160dp"android:background="@drawable/mer_border">
- <ImageViewandroid:id="@+id/imgdetail"android:layout_width="145dp"android:layout_height="145dp"android:layout_margin="8dp"/>
- <TextViewandroid:id="@+id/price"android:layout_width="180dp"android:layout_height="wrap_content"android:text="12345"android:layout_alignParentBottom="true"android:background="#C02000"android:textColor="#FFFFFF"/>
- <TextViewandroid:id="@+id/look"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="去看看"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:background="#C02000"android:textColor="#FFFFFF"/>
- </RelativeLayout>
- <TextViewandroid:id="@+id/tuaninfo"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:textSize="16dp"
- android:maxLines="2"android:layout_below="@id/imglayout"
- android:ellipsize="end"android:text="dddddddddd"/>"
- </RelativeLayout>
- </RelativeLayout>
更多相关文章
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- python起点网月票榜字体反爬案例
- Android实现图片帮助跳转以及选择重拍Sqlite本地保存
- android Sqlite多线程访问异常解决方案
- ImageView的属性android:scaleType,即ImageView.setScaleType(Ima
- Android启动画面实现
- SeekBar 粗略小节
- 【Android(安卓)Training - 04】保存数据 [ Lesson 0 - 章节概览
- Android(安卓)系统状态栏一体化