標籤:
該自訂控制項對外提供getAdapter和setAdapter介面,能夠設定要滾動顯示的View(每個View預設顯示全螢幕寬高)
public class ScrollerViewGroup extends ViewGroup { private int screenHeight;//the phone screen height private int currentY;//記錄當前當前的Y位置 private int startScrollY;//記錄手指按下觸摸事件時的Y方向的滾動位置 private int endScrollY;//記錄手指抬起觸摸事件時的Y方向的滾動位置 private Adapter mAdapter;//顯示適配器 private Scroller mScroller; public ScrollerViewGroup(Context context, AttributeSet attrs) { this(context, null, attrs); } public ScrollerViewGroup(Context context) { this(context, null, null); } public ScrollerViewGroup(Context context, Adapter adapter) { this(context, adapter, null); } public ScrollerViewGroup(Context context, Adapter adapter, AttributeSet attrs) { this(context, adapter, attrs, 0); } public ScrollerViewGroup(Context context, Adapter adapter, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mAdapter = adapter; init(context); } //初始化mScroller和screenHeight private void init(Context context) { mScroller = new Scroller(context); DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); screenHeight = metrics.heightPixels; } public Adapter getAdapter() { return mAdapter; } public void setAdapter(Adapter mAdapter) { this.mAdapter = mAdapter; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mAdapter == null) return; int count = mAdapter.getCount(); for (int i = 0; i < count; i++) { View child = mAdapter.getView(i, null, this); addView(child); measureChild(child, widthMeasureSpec, heightMeasureSpec); } } @Override protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { return new MarginLayoutParams(p); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int count = getChildCount(); MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams(); mlp.height = screenHeight * count; setLayoutParams(mlp); for (int i = 0, j = 0; i < count; i++) { View child = getChildAt(i); if (child.getVisibility() == VISIBLE) { child.layout(l, j * screenHeight, r, (++j) * screenHeight); } } } @Override public boolean onTouchEvent(MotionEvent event) { int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: currentY = y; startScrollY = getScrollY(); break; case MotionEvent.ACTION_MOVE: if (!mScroller.isFinished()) mScroller.abortAnimation(); int offset = currentY - y; if (getScrollY() < 0) offset = 0; if (getScrollY() > getHeight() - screenHeight) offset = 0; scrollBy(0, offset); currentY = y; break; case MotionEvent.ACTION_UP: endScrollY = getScrollY(); int offsetY = endScrollY - startScrollY; if (offsetY > 0) { if (offsetY > screenHeight / 3) { mScroller.startScroll(0, endScrollY, 0, screenHeight - offsetY); } else { mScroller.startScroll(0, endScrollY, 0, -offsetY); } } else { if (-offsetY > screenHeight / 3) { mScroller.startScroll(0, endScrollY, 0, -screenHeight - offsetY); } else { mScroller.startScroll(0, endScrollY, 0, -offsetY); } } break; } postInvalidate(); return true; } @Override public void computeScroll() { super.computeScroll(); if (mScroller.computeScrollOffset()) { scrollTo(0, mScroller.getCurrY()); postInvalidate(); } }}
使用時:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); Adapter adapter = new ArrayAdapter<Integer>(this, 0, color) { @Override public View getView(int position, View convertView, ViewGroup parent) { int color = getItem(position); if (convertView == null) { convertView = new View(getContext()); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ); convertView.setLayoutParams(params); } convertView.setBackgroundColor(color); return convertView; } }; svg = (ScrollerViewGroup) findViewById(R.id.svg); if(svg==null) Log.e("main","null"); svg.setAdapter(adapter); } Integer[] color = new Integer[]{Color.RED, Color.BLUE, Color.YELLOW};
Android——自訂滾動ViewGroup