The realization method of automatic display hidden layout of Android ListView _android

Source: Internet
Author: User
Tags gety

With the view of the Ontouchlistener interface to monitor the listview of the sliding, by comparing the size of the last coordinates, judge the slide direction, and through the slide direction to determine whether the corresponding layout should be shown or hidden, and with animation effect.

1. Automatically Show hidden toolbar

First add a Headerview to ListView, avoid the first item is toolbar occlusion.

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

 
 

Judging 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 slide down
else if (mfirsty-mcurrenty> Mtouchslop)
direction=1//listview Upward slide
if (direction==1)
{
if (mshow)
{
Toolbaranim (1); Hide the top view
mshow=!mshow;
}
else if (direction==0)
{
if (!mshow)
{
Toolbaranim (0);//Show view mshow=!mshow above;
}
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 ();
}
Above for displacement and transparency properties animation

Use the time theme to use Noactionbar, otherwise it will cause conflict. Introducing compilation at the same time

dependencies{
Compile Filetree (include:[' *.jar '],dir: ' Libs ')
compile ' com.android.support:appcompat-v7 : 21.0.3 '
}

2. When you want to hide and display the component is not toolbar, but our custom layout myview, we need to pay attention to some points,

(1) Layout to use relative layout, let our custom layout suspended above the ListView.

(2) To avoid the first item is MyView occlusion, to ListView Add a Headerview, at this point need to measure the height of myview, to use the following method, the task post to the UI thread, otherwise the implementation will be wrong.

Final View Header=new view (this); Add a Headview to the ListView to avoid the first item being blocked 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 is a small set to introduce the Android ListView automatic display hidden layout of the implementation method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.