现在很火的主流开发框架有类tabhost的微信选项卡、类QQ的左滑隐藏菜单、也有想网易新闻的viewpager+自定义指示器。这些主流的应用开发框架一定程度上迎合使用者的需求和审美。最近由于工作室在做一个项目,应用了这些框架及其二次开发。现在分享Github上最火的一项实现左右滑隐藏菜单的开源框架AndroidResideMenu,是我目前觉得在功能上比较完整、接口比较健全及其流畅度最高的一个框架。
话不多说,直接上链接先看看外国大神怎么写的 点击查看 点击打开链接
上面有其全的api及其中文的介绍方法,对于国内的开发者来说使用起来更加方便。
如果有不熟悉Github上面下载代码的,点击下载 点击打开链接
具体的功能链接中说明,下面是我使用该框架后认为可以根据项目需要做一些功能变换,读者可以根据自己的需求做参考。
介绍一下三个主要的类
ResideMenuItem 继承LinearLayout,实现单项菜单
ResideMenu继承FrameLayour实现主界面布局,为绝对布局,通过逻辑和动画控制左右滑动,并开发了添加ResideMenuItem的接口。其接口为addMenuItem()
MenuActivity 实现主界面功能的Activity
(即中间布局)其setUpMenu()方法为绑定当前activity与菜单。
一般来说,我们参考手机QQ左侧菜单的内容和开源框架,我们吸收开源框架的功能实现(性能已高度被优化)。参考源码可以知道,ResideMenuItem是自定义Linearlayout,通过继承该类来实现单项菜单的内容,如图。那么如果简单地只需要菜单内容,固然如此。现在假设我们在菜单中实现的布局或者功能更多的时候,我们
就需要考虑自定义布局或者修改框架里面的实现,类似于QQ这样。
其实现的方法有二:
1、动态实现:开源框架左右侧都是以实现菜单为主,所有添加的都是Linearlayout的子类。在接口addMenuItem(),修改传递参数为其他布局,比如Linearlayout后,通过xml文件自定义自己想要的布局,然后在MenuActivity类的setUpMenu()通过下面代码即可
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout example =(LinearLayout)inflater.inflate(R.layout.zuocedaidan, null);
resideMenu.addMenuItem(example,ResideMenu.DIRECTION_LEFT;
贴上自己实现的布局为例
2、 静态实现:在布局文件中编辑布局。查看ResideMenu的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/iv_shadow"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/shadow"
android:scaleType="fitXY" />
<ScrollView
android:id="@+id/sv_left_menu"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:paddingLeft="30dp"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/layout_left_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:orientation="horizontal" >
<cn.topview.hk.ui.CircularImage
android:id="@+id/cuocecaidan_datou"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="60dp"
android:text="周杰伦"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/eze"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="指挥长"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/shenfen"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/shenfen"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="消防部"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/danwei"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/danwei"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="消防部队长"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/dianhua"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/dianhua"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="020-8213675"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/shouji"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/shouji"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="13876519267"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:background="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/yichuli"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/yichuli"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="已处理任务(2)"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/weichuli"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/zhengzaichuli"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="正在处理任务(12)"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="20dp"
android:orientation="horizontal" >
<ImageView
android:layout_gravity="center_vertical"
android:layout_width="18dp"
android:layout_height="18dp"
android:background="@drawable/tip"
android:scaleType="centerCrop" />
<TextView
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="注销账号"
android:textColor="@android:color/white"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<ScrollView
android:id="@+id/sv_right_menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:paddingRight="30dp"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/layout_right_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</FrameLayout>
通过上述两种方法都可以自定义的菜单,监听的实现都在setUpMenu()即可。
对于框架的使用,希望对于更多接触开源框架的人能更好地开发自己喜欢的应用,在此只是给下自己的意见,希望对其他人有帮助。 在接下来的两篇文章中,会陆续介绍fragment+fragmenttabhost实现类微信的导航(tabhost+activity官方已不推荐使用)及其3Dviewpager+多样化指示器的炫酷功能,希望得到大家的支持!写得方式不好或者存在的问题,希望大家能踊跃之处,我会改进不足。