English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Use the OnTouchListener interface of View to listen to the sliding of the listView, compare the size of the coordinates with the last time, determine the sliding direction, and determine whether to display or hide the corresponding layout based on the sliding direction, with animation effects.
1.Automatically show or hide the Toolbar
Firstly, add a HeaderView to the listView to avoid the first item being obscured by the Toolbar.
View header=new View(this); header.setLayoutParams(new AbsListView.LayoutParams( AbsListView.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.abc_action_bar_default_height_material))); mListView.addHeaderView(header);
//R.dimen.abc_action_bar_default_height_material is the height of the system ActionBar
Define a mTouchSlop variable to get the minimum sliding distance considered by the system
mTouchSlop=ViewConfiguration.get(this).getScaledTouchSlop();//The minimum sliding distance considered by the system
Determine sliding events
bbsListView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mFirstY=event.getY(); break; case MotionEvent.ACTION_MOVE: mCurrentY=event.getY(); if(mCurrentY-mFirstY>mTouchSlop) direction=0; //listView向下滑动 else if(mFirstY-mCurrentY>mTouchSlop) direction=1; //listView向上滑动 if(direction==1) { if(mShow) { toolbarAnim(1); //隐藏上方的view mShow=!mShow; } } else if(direction==0) { if(!mShow) { toolbarAnim(0); //展示上方的view mShow=!mShow; } } case MotionEvent.ACTION_UP: break; } return false; } }); }
属性动画
protected void toolbarAnim(int flag) { if(set!=null && set.isRunning()) { set.cancel(); } if(flag==0) { mAnimator1=ObjectAnimator.ofFloat(mToolbar, "translationY", linearView.getTranslationY(),0); mAnimator2=ObjectAnimator.ofFloat(mToolbar, "alpha", 0f,1f); } else if(flag==1) { mAnimator1=ObjectAnimator.ofFloat(mToolbar, "translationY", linearView.getTranslationY(),-linearView.getHeight()); mAnimator2=ObjectAnimator.ofFloat(mToolbar, "alpha", 1f,0f); } set=new AnimatorSet(); set.playTogether(mAnimator1,mAnimator2); set.start(); }
//上面为位移还有透明度属性动画
使用的时候theme要用NoActionBar的,不然会引起冲突。同时引入编译
dependencies{ compile fileTree(include=['*.jar', dir:'libs') compile 'com.android.support:appcompat-v7:21.0.3' }
2When the component to be hidden and displayed is not toolbar, but our custom layout myView, some points need to be paid attention to,
(1) The layout should use a relative layout, so that our custom layout can float above listView.
(2To avoid the first item being obscured by myView, add a HeaderView to listView. At this time, it is necessary to measure the height of myView, and use the following method to post the task to the UI thread, otherwise the execution will fail.
final View header=new View(this); //Add a headView to listView to avoid the first item being obscured header.post(new Runnable() { public void run() { header.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, myView.getHeight())); } });
Others are the same as toolbar
That's all for this article. Hope it will be helpful to everyone's learning, and also hope everyone will support the Shouting Tutorial more.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When sending an email, please replace # with @ to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)