让scrollview在滚动的过程中自动定位页的边边

/**
* The Class PageScrollView, we can scroll the pages in this component.
*/
public class PageScrollView extends LinearLayout {

/** The context. */
private Context mContext;

/** The adapter used to get the view of each page. */
private BaseAdapter mAdapter;

/** The page count. */
private int mPageCount = 0;

/** The current page index. */
private int mCurrPageIndex = 0;

/** The scroll view in this component. */
private HorizontalScrollView mScrollView;

/** The target parent view for each page. */
private LinearLayout mPageContent;

/** The velocity tracker. */
private VelocityTracker mVelocityTracker;

/** The width for each page. */
private int mWidth;

/** The maximum velocity. */
private int mMaximumVelocity;

/** The change. */
private boolean mChange = false;

/** The Constant SNAP_VELOCITY. */
private static final int SNAP_VELOCITY = 500;

/** The Constant PAGE_FACTOR. */
private static final int PAGE_FACTOR = 3;

/** The units parameter for velocity. */
private static final int VELOCITY_UNITS = 1000;

/**
* Instantiates a new page scroll view.
*
* @param context the context
*/
public PageScrollView(Context context) {
super(context);
mContext = context;
}

/**
* Instantiates a new page scroll view.
*
* @param context the context
* @param attrs the attrs
*/
public PageScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}

/**
* Sets the adapter.
*
* @param adapter the adapter
* @param with the with for each page
*/
public void setAdapter(BaseAdapter adapter, int with) {
if (adapter == null) {
return;
}

mAdapter = adapter;
mWidth = with;
initUI();
bindLayoutUI();
}

/**
* Inits the ui.
*/
private void initUI() {
this.removeAllViews();

mScrollView = new HorizontalScrollView(mContext);
mScrollView.setHorizontalScrollBarEnabled(false);
mScrollView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));

mPageContent = new LinearLayout(mContext);
mPageContent.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
//mPageContent.setGravity(Gravity.CENTER);
mPageContent.setOrientation(HORIZONTAL);

mScrollView.setOnTouchListener(mTouchListener);
this.addView(mScrollView);
mScrollView.addView(mPageContent);

// FIXME: width is a problem sometimes!
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) this.getLayoutParams();
if (!(lp.width == LinearLayout.LayoutParams.FILL_PARENT
|| lp.width == LinearLayout.LayoutParams.WRAP_CONTENT)) {
mWidth = lp.width;
}

final ViewConfiguration configuration = ViewConfiguration.get(mContext);
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}

/**
* Bind layout ui, add pages to this component.
*/
private void bindLayoutUI() {
mPageCount = mAdapter.getCount();

final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
mWidth, LinearLayout.LayoutParams.FILL_PARENT);
// params.gravity = Gravity.CENTER;
for (int i = 0; i < mPageCount; i++) {
final View v = mAdapter.getView(i, null, null);
mPageContent.addView(v, params);
}
}

/**
* Gets the current page index.
*
* @return the current page index
*/
public int getCurrentPageIndex() {
return mCurrPageIndex;
}

/** The m touch listener. */
private View.OnTouchListener mTouchListener = new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();

if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);

switch (action) {
case MotionEvent.ACTION_UP:

final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(VELOCITY_UNITS, mMaximumVelocity);
final int velocityX = (int) velocityTracker.getXVelocity();
final int scrollX = mScrollView.getScrollX();
final int deltaX = (int) (scrollX - mCurrPageIndex * mWidth);
if (((velocityX > SNAP_VELOCITY) || (deltaX < -mWidth / PAGE_FACTOR))
&& mCurrPageIndex > 0) {
mCurrPageIndex -= 1;
mChange = true;

} else if (((velocityX < -SNAP_VELOCITY) || (deltaX > mWidth / PAGE_FACTOR))
&& mCurrPageIndex < (mPageCount - 1)) {
mCurrPageIndex += 1;
mChange = true;
}
mScrollView.smoothScrollTo(mCurrPageIndex * mWidth, 0);
if (mChange) {
if (mOnPageChangeListener != null) {
mOnPageChangeListener.onPageChange();
}
mChange = false;
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
return true; // break;
default:
break;
}
return false;
}
};

/**
* The listener interface for receiving onPageChange events.
* The class that is interested in processing a onPageChange
* event implements this interface, and the object created
* with that class is registered with a component using the
* component's <code>addonPageChangeListener<code> method. When
* the onPageChange event occurs, that object's appropriate
* method is invoked.
*
* @see onPageChangeEvent
*/
public interface OnPageChangeListener {

/**
* On page change.
*/
void onPageChange();
}

/** The m on page change listener. */
private OnPageChangeListener mOnPageChangeListener;

/**
* Sets the on page change listener.
*
* @param l the new on page change listener
*/
public void setOnPageChangeListener(OnPageChangeListener l) {
mOnPageChangeListener = l;
}

/**
* Sets the page index.
*
* @param index the new page index
*/
public void setPageIndex(int index) {
if (index < 0 || index > mPageCount - 1 || index == mCurrPageIndex) {
return;
}

mCurrPageIndex = index;
mScrollView.smoothScrollTo(mCurrPageIndex * mWidth, 0);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值