ExpandLayout用法和封装自定义控件的一些小记

来源:互联网 发布:凯撒博尔吉亚 知乎 编辑:程序博客网 时间:2024/05/12 11:39

上次封装顶部tab的时候没有记录。

这次用了这个点击打开链接 ExpandableLayout。

因为要改源码一些东西,把一起记录一下。


这个ExpandableLayout是一个可展开的ListView,恩,带动画的。

源生好像也有一个这个控件,不过看起来丑丑的,就没有用他,而且这个控件是可以自定义ListView中的布局的。

原理来说是蛮简单的,和网上用的一些原理一样,就是一开始是把content的东西的Visiable设成Gone,点击了header之后就设成visiable。

然后展开动画的实现是通过Animation的applyTransformation

设置所需控件的布局高度v.getLayoutParams().height 

 然后刷新v.requestLayout();

和以前那个做左滑抽屉一样的方法。


在动画的使用上是通过先对所有的非点击的item进行关闭抽屉操作,然后再对点击的查看状态,通过判断状态来实现开或关。

不过坑爹的是他这个判断并没有用,因为他使用的performItemClick在对已经打开的抽屉没办法再进行点击监听。

代码中就很鸡贼的改成在对tiem项的header进行监听,点击了header之后会对打开状态再监听。


说道封装自定义控件,那就是继承已有的控件再对里面的方法进行重写。

使控件得到自己想要的效果,现在问题来了,定义的控件怎么在XML布局文件里面使用?



第一步,不能再只重写一个构造函数,而要把3种常用的构造函数都写全,应为在xml构造时时要使用到后面两个构造函数的。

    public ExpandableLayoutItem(Context context, AttributeSet attrs)    {        super(context, attrs);        init(context, attrs);        this.context = context;    }    public ExpandableLayoutItem(Context context, AttributeSet attrs, int defStyle)    {        super(context, attrs, defStyle);        init(context, attrs);        this.context = context;    }
用这个控件的例子,第二个和第三个构造函数里面额attrs是我们定义的自定义控件可设置的一些参数。

而第三个构造函数里面的defStyle是这些参数的默认值。



第二步,就是需要有一个attrs.mxl

    <declare-styleable name="ExpandableLayout">        <attr name="el_headerLayout" format="reference"/>        <attr name="el_contentLayout" format="reference" />        <attr name="el_duration" format="integer" />    </declare-styleable>
这是本控件的attrs的一部分(去了头尾),里面定义的是一个styleable,就是我们会在本控件中使用的参数。

注意这个styleable的名字叫做ExpandableLayout 而他有三个可以使用的属性分别是

el_headerLayout     类型为reference

el_contentLayout     类型为reference

el_duration   类型为integer

reference为某一资源ID  integer 整型 还有其他的类型可以参考点击打开链接


第三步,就是在xml里面为这些参数赋值

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:expandable="http://schemas.android.com/apk/res-auto"        定义工具,expandable为自定义的名字,写什么都行    android:layout_width="match_parent"    android:layout_height="match_parent">    <com.lyn.justdoit.expandablelayout.ExpandableLayoutItem                     使用控件不用说就是使用完整包名加文件名        android:id="@+id/row"        android:layout_width="match_parent"        android:layout_height="wrap_content"        expandable:el_headerLayout="@layout/week_list_header"这么用        expandable:el_contentLayout="@layout/week_list_content"        expandable:el_duration="300"        /></LinearLayout>



最后,第三步就是要使用这些定义好的参数。

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableLayout);//通过上面定义的名字获取styleablefinal int headerID = typedArray.getResourceId(R.styleable.ExpandableLayout_el_headerLayout, -1);//使用资源第二个参数为defValuefinal int contentID = typedArray.getResourceId(R.styleable.ExpandableLayout_el_contentLayout, -1);

 注意资源使用时是  sytleable的名字 + _ + 资源名字。

TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!

recyle应该就是回滚吧。


差不多就是这样






附:做过N多笔试题,重写和重载的企鹅别

1.重写必须继承,重载不用。
2.重写的方法名,参数数目相同,参数类型兼容,重载的方法名相同,参数列表不同。
3.重写的方法修饰符大于等于父类的方法,重载和修饰符无关。
4.重写不可以抛出父类没有抛出的一般异常,可以抛出运行时异常

0 0
原创粉丝点击