很多应用为了节省空间而又使界面能够充足的显示信息,大多数应用都采用了侧边栏的方式,如下图:
来说说它的思路,底下是两个或多个视图,分别通过控制它们的宽度、左边距来控制它们的显示,来看看代码
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); } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的Android仿人人网滑动侧边栏效果全部内容,希望文章能够帮你解决Android仿人人网滑动侧边栏效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)