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

上次封装顶部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);//通过上面定义的名字获取styleable
final int headerID = typedArray.getResourceId(R.styleable.ExpandableLayout_el_headerLayout, -1);//使用资源第二个参数为defValue
final int contentID = typedArray.getResourceId(R.styleable.ExpandableLayout_el_contentLayout, -1);

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

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

recyle应该就是回滚吧。


差不多就是这样






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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ToggleExpandLayout是一个可折叠和展开子view的开关布局件。它可以将它的子view以阶梯式的展开。项目地址:https://github.com/fenjuly/ToggleExpandLayout 效果图:如何使用<com.fenjuly.mylibrary.ToggleExpandLayout             android:id="@ id/toogleLayout"             android:layout_width="wrap_content"             android:layout_height="80dp"             >             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 1"/>             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 2"/>             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view"/>                      </com.fenjuly.mylibrary.ToggleExpandLayout>注意,由于ToggleExpandLayout的本质是个FrameLayout,所以必须将其高度设置为大于所有子view展开状态的高度,不能设为wrap_content。为了解决这个问题,你可以将ToggleExpandLayout的外面在加个DropDownLayout:<com.fenjuly.mylibrary.DropDownLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         >         <com.fenjuly.mylibrary.ToggleExpandLayout             android:id="@ id/toogleLayout"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             >             <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 1"/>              <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view 2"/>               <TextView                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="view"/>                              </com.fenjuly.mylibrary.ToggleExpandLayout> </com.fenjuly.mylibrary.DropDownL

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值