English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Implementation Method of Auto-showing and Hiding Layout in Android ListView

Use the OnTouchListener interface of View to listen to the sliding of listView, compare the size with the last coordinate, determine the sliding direction, and determine whether to show or hide the corresponding layout based on the sliding direction, with animation effects.

1.Automatically show or hide Toolbar

Firstly, add a HeaderView to listView to avoid the first item being obscured by 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's ActionBar

Define a variable mTouchSlop 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 swipe down
else if(mFirstY-mCurrentY>mTouchSlop)
direction=1; //listView swipe up
if(direction==1)
{
if(mShow)
{
toolbarAnim(1); //Hide the view above
mShow=!mShow;
}
}
else if(direction==0)
{
if(!mShow)
{
toolbarAnim(0); //Display the view above
mShow=!mShow;
}
}
case MotionEvent.ACTION_UP:
break;
}
return false;
}
});
}

property animation

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();
}
//The above is the animation of displacement and opacity properties

When using, the theme should be NoActionBar to avoid conflicts. At the same time, compile

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, allowing us to float our custom layout above the listView.

(2Avoid the first item being obscured by myView. Add a HeaderView to listView. At this time, it is necessary to measure the height of myView. 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()));
}
});

The others are the same as toolbar

The above-mentioned is the method introduced by the editor to implement the automatic display and hide layout of Android ListView, hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to you in time. I also want to express my heartfelt thanks to everyone for their support of the Naihua tutorial website!

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 any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email to report infringement. Provide relevant evidence, and once verified, the website will immediately delete the suspected infringing content.

You May Also Like