ListView bounce, listview

Source: Internet
Author: User

ListView bounce, listview
Package com. rebound. myscroll;


Import android. content. Context;
Import android. util. AttributeSet;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. animation. Animation;
Import android. view. animation. Animation. AnimationListener;
Import android. view. animation. LinearInterpolator;
Import android. view. animation. TranslateAnimation;
Import android. widget. LinearLayout;
Import android. widget. ListView;


/**
* ListView Rebound
*
*/
Public class ListViewFantan extends LinearLayout {


Private float y_down;


Public ListViewFantan (Context context, AttributeSet attrs ){
Super (context, attrs );
}


@ Override
Public boolean onInterceptTouchEvent (MotionEvent ev ){
Float y2 = ev. getY ();
Float y_move = 0;
Switch (ev. getAction ()){
Case MotionEvent. ACTION_DOWN:
Y_down = y2;
Break;
Case MotionEvent. ACTION_MOVE:
Y_move = y2-y_down;
Break;
Case MotionEvent. ACTION_UP:
Break;


Default:
Break;
}
// When the listview reaches the top, the top bounce
If (listView! = Null & y_move> 20
& ListView. getFirstVisiblePosition () = 0 ){
View childAt = listView. getChildAt (0 );
If (childAt! = Null & childAt. getTop () = 0 ){
Move_top = true;
Move_bottom = false;
Return true; // process it by yourself
}
}
// The bottom of the listview has rebounded.
If (listView! = Null ){
// ListView.
View childAt = listView. getChildAt (listView. getChildCount ()-1 );
If (childAt! = Null ){
Int top = childAt. getTop ();
Int bottom = childAt. getBottom ();
Int height = getHeight ();
System. out. println ("bottom:" + bottom + "height:" + height );
If (bottom <= height & y_move <-20 ){
Move_bottom = true;
Move_top = false;
// The End
Return true; // process it by yourself
}
}
}
Return false;
}


/**
* Move up (top bounce)
*/
Private boolean move_top;
/**
* Move downward (bottom bounce)
*/
Private boolean move_bottom;


@ Override
Public boolean onTouchEvent (MotionEvent ev ){
Float y2 = ev. getY ();
Float y_move = 0;
Switch (ev. getAction ()){
Case MotionEvent. ACTION_DOWN:
Y_down = y2;
// System. out. println ("down:" + y_down );
Break;
Case MotionEvent. ACTION_MOVE:
Y_move = y2-y_down;
// System. out. println ("move:" + y_move );
If (move_top ){
// Top bounce
This. setPadding (0, (int) y_move, 0, 0 );
// System. out. println ("padding_top:" + y_move );
} Else if (move_bottom ){
// Bottom bounce
Int abs = (int) Math. abs (y_move );
// System. out. println ("padding_bottom:" + abs );
This. setPadding (0, 0, 0, abs );
MarginLayoutParams layoutParams = (MarginLayoutParams) listView
. GetLayoutParams ();
LayoutParams. topMargin = (int) y_move;
ListView. setLayoutParams (layoutParams );
}
Invalidate ();
Break;
Case MotionEvent. ACTION_UP:
// System. out. println ("up /////////");
If (move_top ){

TranslateAnimation translateAnimation = new TranslateAnimation (0, 0, y_move, 0 );
TranslateAnimation. setDuration (200 );
TranslateAnimation. setFillAfter (true );
TranslateAnimation. setInterpolator (new LinearInterpolator ());
// TranslateAnimation. start ();
TranslateAnimation. setAnimationListener (new AnimationListener (){

@ Override
Public void onAnimationStart (Animation animation ){

}

@ Override
Public void onAnimationRepeat (Animation animation ){
}

@ Override
Public void onAnimationEnd (Animation animation ){
ListViewFantan. this. setPadding (0, 0, 0, 0 );
}
});
ListView. startAnimation (translateAnimation );
} Else if (move_bottom ){

TranslateAnimation translateAnimation = new TranslateAnimation (0, 0, y_move, 0 );
TranslateAnimation. setDuration (200 );
TranslateAnimation. setFillAfter (true );
TranslateAnimation. setInterpolator (new LinearInterpolator ());
// TranslateAnimation. start ();
TranslateAnimation. setAnimationListener (new AnimationListener (){

@ Override
Public void onAnimationStart (Animation animation ){

}

@ Override
Public void onAnimationRepeat (Animation animation ){
}

@ Override
Public void onAnimationEnd (Animation animation ){
ListViewFantan. this. setPadding (0, 0, 0, 0 );
MarginLayoutParams layoutParams = (MarginLayoutParams) listView
. GetLayoutParams ();
LayoutParams. topMargin = 0;
ListView. setLayoutParams (layoutParams );
}
});
ListView. startAnimation (translateAnimation );
}
Invalidate ();
Move_top = false;
Move_bottom = false;
Break;


Default:
Break;
}
Return super. onTouchEvent (ev );
}


Private ListView listView;


@ Override
Protected void onFinishInflate (){
Super. onFinishInflate ();
View childAt = getChildAt (0 );
If (childAt instanceof ListView ){
ListView = (ListView) childAt;
}
}


}



How does a ListView bounce effect be achieved?

Override the listview control and determine the drop-down distance. In the ontouch event, use setpadding to set selection (0.
 
Listview clearing

I have never done Mobile Phone development, but the idea is clear. Clear the listview before calling and loading the listview, for example, listview1.clear (). You only need to find the portal to call the listview. For example:
The following timer re-loads the listview at intervals and displays the content. The InitListView () method is used to load the listview entry. You can use listView1.Clear () to clear the listview. Actually there are many methods.
Void timereffectick (object sender, EventArgs e) {listView1.Clear (); InitListView (); timer1.Enabled = true;}. If you have any questions, ask again.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.