android - Custom view for scrolling with fling -
i want create custom view should scroll horizontally , can useful header toolbar(as action bar) , footer toolbar.
protected override void onmeasure(int widthmeasurespec, int heightmeasurespec) { try { displaymetrics met = new displaymetrics(); display.getmetrics(met); m_ftoolsize = (met.xdpi * c_ftooliconsize) / c_fmmperinch; setmeasureddimension(met.widthpixels, (int)(m_ftoolsize + 2 * c_ntoolindent)); } catch { } } protected override void onlayout(bool changed, int l, int t, int r, int b) { try { int[] loc = new int[2]; getlocationonscreen(loc); loc[1] -= sysutil.getstatusbarheight(resources); display dsp = ((activity)context).windowmanager.defaultdisplay; if (loc[1] < dsp.height / 2) { m_bisuppertoolbar = true; } int ntoolsize = (int)m_ftoolsize; int nnumtools = (int)(((r - l) - (2 * c_ntoolindent)) / m_ftoolsize); m_ftoolspace = ((r - l) - 2 * c_ntoolindent - nnumtools * ntoolsize) / (nnumtools - 1); float x = c_ntoolindent, y = c_ntoolindent; (int = 0; < childcount; i++)// removed condition of nnumtools allow scrolling { getchildat(i).layout((int)x, (int)y, (int)x + ntoolsize, (int)y + ntoolsize); x += m_ftoolsize + m_ftoolspace; } } catch (exception ex) { } } //to implement scrolling //this allows viewgroup watch events dispatched child views public override bool onintercepttouchevent(motionevent e) { console.writeline("onintercepttouchevent"); boolean intercept = false; try { switch (e.action) { case motioneventactions.move: { int xdiff = (int)math.abs(e.getx() - m_flastx); if (xdiff > mtouchslop) { m_ntouchstate = c_ntouchstatehorizontalscrolling; m_flastx = e.getx(); } int ydiff = (int)math.abs(e.gety() - m_flasty); if (ydiff > mtouchslop) { m_ntouchstate = -1; } if (math.abs(xdiff * 2) > math.abs(ydiff) && xdiff > mtouchslop) { intercept = true; } break; } case motioneventactions.cancel: case motioneventactions.up: // release drag. m_ntouchstate = c_ntouchstaterest; break; case motioneventactions.down: m_flasty = e.gety(); m_flastx = e.getx(); break; default: break; } } catch (exception ex) { } return intercept; } public override bool ontouchevent(motionevent e) { console.writeline("tb ontouchevent"); try { if (mvelocitytracker == null) { mvelocitytracker = velocitytracker.obtain(); } mvelocitytracker.addmovement(e); float x = e.getx(); float y = e.gety(); switch (e.action) { case motioneventactions.down: if (!m_scroller.isfinished) { m_scroller.abortanimation(); } m_flastx = x; if (m_scroller.isfinished) { m_ntouchstate = c_ntouchstaterest; } else { m_ntouchstate = c_ntouchstatehorizontalscrolling; } break; case motioneventactions.move: int deltax = (int)(m_flastx - x); m_flastx = x; int scrollx = scrollx; // scroll right if (deltax < 0) { if (scrollx > 0) { scrollby(math.max(-scrollx, deltax), 0); } } // scroll left else if (deltax > 0) { // visible full icons without empty spaces int availabletoscroll = getchildat(childcount - 1).right - scrollx - (display.width - c_ntoolindent); if (availabletoscroll > 0) { scrollby(math.min(availabletoscroll, deltax), 0); } } break; case motioneventactions.up: //for (int index = 0; index < childcount; index++) //{ // view v = getchildat(index); // rect rect = new rect(v.left, v.top, v.right, v.bottom); // if (v.getlocalvisiblerect(rect)) // { // // here because either clipped or visible // int[] loc = new int[2]; // v.getlocationonscreen(loc); // int n = v.width; // if (loc[0] < c_ntoolindent) // { // // identify clipped view // if (math.abs(loc[0]) >= v.width / 2) // { // // scroll view outside screen // scrollby(v.width + loc[0] + (int)m_ftoolspace - c_ntoolindent, 0); // break; // } // else // { // scrollby(loc[0] - c_ntoolindent, 0); // break; // } // } // } //} velocitytracker velocitytracker = mvelocitytracker; velocitytracker.computecurrentvelocity(1000, mmaximumvelocity); int initialxvelocity = (int)velocitytracker.xvelocity; int initialyvelocity = (int)velocitytracker.yvelocity; if ((system.math.abs(initialxvelocity) + system.math.abs(initialyvelocity) > mminimumvelocity) && childcount > 0) { fling(-initialxvelocity, 0); } if (mvelocitytracker != null) { mvelocitytracker.recycle(); mvelocitytracker = null; } break; m_ntouchstate = c_ntouchstaterest; break; case motioneventactions.cancel: m_ntouchstate = c_ntouchstaterest; break; default: break; } } catch (exception ex) { } return true; } public void fling(int velocityx, int velocityy) { console.writeline("tb fling"); if (childcount > 0) { int height = height - paddingbottom - paddingtop; int bottom = getchildat(0).height; int width = width - paddingright - paddingleft; int right = getchildat(0).width; m_scroller.fling(scrollx, 0, velocityx, 0, 0, 1300, 0, 0); bool movingdown = velocityy > 0; bool movingright = velocityx > 0; // view newfocused = findfocusableviewinmybounds(movingright, mscroller.finalx, movingdown, mscroller.finaly, findfocus()); awakenscrollbars(m_scroller.duration); invalidate(); } }
Comments
Post a Comment