Android仿人人网滑动侧边栏效果
程序员文章站
2024-03-04 23:51:18
很多应用为了节省空间而又使界面能够充足的显示信息,大多数应用都采用了侧边栏的方式,如下图:
...
很多应用为了节省空间而又使界面能够充足的显示信息,大多数应用都采用了侧边栏的方式,如下图:
来说说它的思路,底下是两个或多个视图,分别通过控制它们的宽度、左边距来控制它们的显示,来看看代码
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".mainactivity" > <linearlayout android:id="@+id/menu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/menu" > </linearlayout> <linearlayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/content" > </linearlayout> </linearlayout>
mainactivity.java
public class mainactivity extends activity implements ontouchlistener { private linearlayout menu; private linearlayout content; private layoutparams menuparams; private layoutparams contentparams; // menu完全显示时,留给content的宽度值。 private static final int menupadding = 80; // 分辨率 private int displaywidth; private float xdown; private float xmove; private boolean misshow = false; private static final int speed = 50; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); displaywidth = getwindowmanager().getdefaultdisplay().getwidth(); menu = (linearlayout) findviewbyid(r.id.menu); content = (linearlayout) findviewbyid(r.id.content); menuparams = (layoutparams) menu.getlayoutparams(); contentparams = (layoutparams) content.getlayoutparams(); findviewbyid(r.id.layout).setontouchlistener(this); menuparams.width = displaywidth - menupadding; contentparams.width = displaywidth; showmenu(misshow); } @override public boolean ontouch(view v, motionevent event) { switch (event.getaction()) { case motionevent.action_down: showmenu(!misshow); break; case motionevent.action_move: break; case motionevent.action_up: break; } return true; } private void showmenu(boolean isshow) { if (isshow) { misshow = true; menuparams.leftmargin = 0; } else { misshow = false; menuparams.leftmargin = 0 - menuparams.width; } menu.setlayoutparams(menuparams); } }
上述代码只是用两张图片代替了两个复杂的view(layout),你会发现,两个视图虽然可以切换,但没有动画的感觉,再加上要有拖动效果,所以,我们再给它加个平移时间段,看起来有动画的效果
package com.example.test; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.motionevent; import android.view.view; import android.view.view.onclicklistener; import android.view.view.ontouchlistener; import android.view.window; import android.widget.linearlayout; import android.widget.linearlayout.layoutparams; public class mainactivity extends activity implements ontouchlistener, onclicklistener { private linearlayout menu; private linearlayout content; private layoutparams menuparams; private layoutparams contentparams; // menu完全显示时,留给content的宽度值。 private static final int menupadding = 80; // 分辨率 private int displaywidth; private float xdown; private float xmove; private boolean misshow = false; private static final int speed = 50; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); displaywidth = getwindowmanager().getdefaultdisplay().getwidth(); menu = (linearlayout) findviewbyid(r.id.menu); menu.setonclicklistener(this); content = (linearlayout) findviewbyid(r.id.content); content.setonclicklistener(this); menuparams = (layoutparams) menu.getlayoutparams(); contentparams = (layoutparams) content.getlayoutparams(); //findviewbyid(r.id.layout).setontouchlistener(this); menuparams.width = displaywidth - menupadding; contentparams.width = displaywidth; showmenu(misshow); } @override public void onclick(view v) { switch (v.getid()) { case r.id.menu: new showmenuasynctask().execute(-50); break; case r.id.content: new showmenuasynctask().execute(50); break; } } @override public boolean ontouch(view v, motionevent event) { switch (event.getaction()) { case motionevent.action_down: showmenu(!misshow); break; case motionevent.action_move: break; case motionevent.action_up: break; } return true; } private void showmenu(boolean isshow) { if (isshow) { misshow = true; menuparams.leftmargin = 0; } else { misshow = false; menuparams.leftmargin = 0 - menuparams.width; } menu.setlayoutparams(menuparams); } /** * *这是主要代码:模拟动画过程,也让我更熟悉了asynctask这玩意儿 * */ class showmenuasynctask extends asynctask<integer, integer, integer> { @override protected integer doinbackground(integer... params) { int leftmargin = menuparams.leftmargin; //这里也是值得学习的地方,如果在平常,自己肯定又是这样写: // if(){ // while() // } // else if(){ // while() // } while (true) { leftmargin += params[0]; if (params[0] > 0 && leftmargin >= 0) { break; } else if (params[0] < 0 && leftmargin <= -menuparams.width) { break; } publishprogress(leftmargin); try { thread.sleep(30); } catch (interruptedexception e) { e.printstacktrace(); } } return leftmargin; } @override protected void onprogressupdate(integer... values) { super.onprogressupdate(values); menuparams.leftmargin = values[0]; menu.setlayoutparams(menuparams); } @override protected void onpostexecute(integer result) { super.onpostexecute(result); menuparams.leftmargin = result; menu.setlayoutparams(menuparams); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。