屏幕翻页控件,翻页控件,/* * To chan
分享于 点击 42331 次 点评:74
屏幕翻页控件,翻页控件,/* * To chan
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package 你的包名;import android.app.Activity;import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;/** * * @author Administrator */public class PageSelector extends HorizontalScrollView{ private LinearLayout m_scrollContent; private boolean m_scrollEvent = false; private int m_scrollPos = 0; private boolean m_smoothScroll = false; private boolean m_touchScroll = false; public PageSelector(Context context) { super(context); // TODO Auto-generated constructor stub m_scrollEvent = false; m_scrollContent = new LinearLayout(context); m_scrollContent.setOrientation(LinearLayout.HORIZONTAL); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); addView(m_scrollContent, lp); this.setFadingEdgeLength(0); m_scrollFinishedListener = null; } public PageSelector(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub m_scrollEvent = false; m_scrollContent = new LinearLayout(context); m_scrollContent.setOrientation(LinearLayout.HORIZONTAL); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); addView(m_scrollContent, lp); this.setFadingEdgeLength(0); m_scrollFinishedListener = null; } public PageSelector(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub m_scrollEvent = false; m_scrollContent = new LinearLayout(context); m_scrollContent.setOrientation(LinearLayout.HORIZONTAL); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); addView(m_scrollContent, lp); this.setFadingEdgeLength(0); m_scrollFinishedListener = null; } public void touchScrollAble( boolean able ){ this.m_touchScroll = able; } public int addPage(int ResID) { Activity screen = (Activity)this.getContext(); View pageView = (LinearLayout)screen.getLayoutInflater().inflate(ResID, null); if ( null == pageView ) return -1; return addPage(pageView); } public View delPage( int index ){ View v = getPage( index ); if ( null == v ) return null; m_scrollContent.removeViewAt(index); return v; } public View getPage(int index){ if ( 0 > index || index >= m_scrollContent.getChildCount() ) return null; return m_scrollContent.getChildAt(index); } public boolean toNextPage() { m_scrollPos = getScrollX() + getWidth(); if (m_scrollPos > computeHorizontalScrollRange() - getWidth()) return true; m_scrollEvent = true; m_smoothScroll = false; this.scrollTo(m_scrollPos, 0); return true; } public boolean smoothToNextPage() { m_scrollPos = getScrollX() + getWidth(); if (m_scrollPos > computeHorizontalScrollRange() - getWidth()) return true; m_scrollEvent = true; m_smoothScroll = true; this.smoothScrollTo(m_scrollPos, 0); return true; } public boolean toPreviousPage() { if (getScrollX() <= 0) return true; m_scrollPos = getScrollX() - getWidth(); m_scrollEvent = true; m_smoothScroll = false; this.scrollTo(m_scrollPos, 0); return true; } public boolean smoothToPreviousPage() { if (getScrollX() <= 0) return true; m_scrollPos = getScrollX() - getWidth(); m_scrollEvent = true; m_smoothScroll = true; this.smoothScrollTo(m_scrollPos, 0); return true; } public int getCurPage(){ if (getScrollX() <= 0) return 0; else return getScrollX() / getWidth(); } private int addPage(View pageView) { if ( null == pageView ) return -1; ViewGroup.LayoutParams lp = getPageLayoutParams(); m_scrollContent.addView(pageView, m_scrollContent.getChildCount(), lp); return m_scrollContent.getChildCount() - 1; } private ViewGroup.LayoutParams getPageLayoutParams(){ Activity screen = (Activity)this.getContext(); ViewGroup.LayoutParams lp = this.getLayoutParams(); int m_pageWidth; int m_pageHeigth; if (lp.width == LayoutParams.FILL_PARENT || lp.width == LayoutParams.WRAP_CONTENT || lp.width == LayoutParams.MATCH_PARENT) { DisplayMetrics metrics = new DisplayMetrics(); screen.getWindowManager().getDefaultDisplay().getMetrics(metrics); m_pageWidth = metrics.widthPixels; } else { m_pageWidth = lp.width; } if (lp.height == LayoutParams.FILL_PARENT || lp.height == LayoutParams.WRAP_CONTENT || lp.height == LayoutParams.MATCH_PARENT) { DisplayMetrics metrics = new DisplayMetrics(); screen.getWindowManager().getDefaultDisplay().getMetrics(metrics); m_pageHeigth = metrics.heightPixels; } else { m_pageHeigth = lp.height; } return new ViewGroup.LayoutParams(m_pageWidth, m_pageHeigth); } @Override public boolean onTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub return m_touchScroll; } @Override public void onScrollChanged(int l, int t, int oldl, int oldt){// super.onScrollChanged(l, t, oldl, oldt); if ( !m_scrollEvent ) { m_scrollEvent = true; m_scrollPos = oldl; this.scrollTo(m_scrollPos, 0); return; } else if ( l == m_scrollPos ) { m_scrollEvent = false; if ( null != m_scrollFinishedListener ){ m_scrollFinishedListener.OnScrollFinished(); } } } public static abstract class OnScrollFinishedListener { public abstract void OnScrollFinished(); } OnScrollFinishedListener m_scrollFinishedListener; public void setOnScrollFinishedListener( OnScrollFinishedListener scrollFinishedListener ){ m_scrollFinishedListener = scrollFinishedListener; }}//该片段来自于http://byrx.net
用户点评