A sliding suspension top View, through custom ScrollView to achieve a fine fixed suspension effect
Effect:
This special effect isn’t really that complicated!
Ideas:
- A custom ListView handles the head layout
- Custom RecycleView seems very complicated
- Custom Behavior complicates matters
- Custom listView + PopuWindows
- Customize ViewGroup (need to re-onLayout method)
- Custom Scrollview, View processing
After a lot of thought, the essence of what we did when we wrote the View was to build the wheel with a little bit of code. So we’re going to process the scrollView. Obvious simplicity!!
By comparing the height H of the advertisement bar with the distance Y of the lock sliding in the Y direction of view, the view is processed
Key code:
Override onTouchEvent to get the sliding distance
/** * Override onTouchEvent when the user's hand is above the HoveringScrollview */ public Boolean onTouchEvent(MotionEvent ev) {// Set HoveringScrollview's sliding distance in the Y direction to onScrollif(onScrollListener ! = null) { onScrollListener.onMyScroll(lastScrollY = this.getScrollY()); } switch (ev.getAction()) {// HoveringScrollview may still slide when the user raises his hand, // So when the user raises his hand we send a message to handler every 6 milliseconds, // Handler handles how far the HoveringScrollview slidescase MotionEvent.ACTION_UP:
handler.sendMessageDelayed(handler.obtainMessage(), 20);
break;
}
return super.onTouchEvent(ev);
};
`Copy the code
You need to do it in Handler
Private Handler Handler = new; /* private Handler Handler = new; /* private Handler Handler = new; / / private Handler = new; / / private Handler = new; / / private Handler = new; / / private Handler = newHandler() { public void handleMessage(android.os.Message msg) { int scrollY = HoveringScrollview.this.getScrollY(); // Send a message to handler every 6 milliseconds.if(lastScrollY ! = scrollY) { lastScrollY = scrollY; handler.sendMessageDelayed(handler.obtainMessage(), 6); }if (onScrollListener != null) {
onScrollListener.onMyScroll(scrollY);
}
};
};Copy the code
It is important to do something in the listener callback:
@Override
public void onMyScroll(int scrollY) {
LogUtils.e("yuyahao"."scrollY: " +scrollY);
// TODO Auto-generated method stub
if (scrollY >= searchLayoutTop) {
if (hoveringLayout.getParent() != search01) {
search02.removeView(hoveringLayout);
search01.addView(hoveringLayout);
}
} else {
if(hoveringLayout.getParent() ! = search02) { search01.removeView(hoveringLayout); search02.addView(hoveringLayout); }}}Copy the code
When the highlight of the advertisement bar is greater than the height of the finger, specify the fixed floating View in its parent layout, otherwise specify a top parent container to add inside.
For more
Advanced UI special like live like effect – a beautiful cool like animation
A small case of recording and playback
NDK project actual combat – high imitation 360 mobile phone assistant uninstall monitoring
Play with custom bar charts – Teach you how to play awesome
Play with radar effects – it’s so easy to make a cool radar diffusion effect
There is not a lot of code, students can download github Demo experience and learn.
Project Github link address
Github.com/androidstar…
Download slow? CSDN Download link:
Download.csdn.net/download/an…