自定义控件—自定义的ExpandableTextView

这里写图片描述

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:padding="6dp" >

    <LinearLayout
        android:id="@+id/part2_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/part2_iv_icon0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/safeicon0" />

        <ImageView
            android:id="@+id/part2_iv_icon1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:src="@drawable/safeicon0" />

        <ImageView
            android:id="@+id/part2_iv_icon2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:src="@drawable/safeicon0" />

        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/part2_iv_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow_down" />
    </LinearLayout>

    <LinearLayout

        android:id="@+id/part2_ll_texts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/part2_ll_text_0"
            style="@style/Text_Detail"
            android:layout_margin="2dp"
            android:drawableLeft="@drawable/safedesurl0"
            android:text="正版xxxxxxxxxxxx" />

        <TextView
               android:id="@+id/part2_ll_text_1"
            style="@style/Text_Detail"
            android:layout_margin="2dp"
            android:drawableLeft="@drawable/safedesurl0"
            android:text="正版xxxxxxxxxxxx" />

        <TextView
            android:id="@+id/part2_ll_text_2"
            style="@style/Text_Detail"
            android:layout_margin="2dp"
            android:drawableLeft="@drawable/safedesurl0"
            android:text="正版xxxxxxxxxxxx" />
    </LinearLayout>
</LinearLayout>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.include_app_detail_part2);
        final LinearLayout part2_ll = (LinearLayout) findViewById(R.id.part2_ll);
        final ImageView part2_iv_arrow = (ImageView) findViewById(R.id.part2_iv_arrow);
        final LinearLayout part2_ll_texts = (LinearLayout) findViewById(R.id.part2_ll_texts);
        CollapseUtil.initCollapseAnim(part2_ll,part2_iv_arrow,part2_ll_texts);
    }
}
**
 *伸缩工具类
 */
public class CollapseUtil {

    //参数.接受点击事件的条目
    //参数:箭头控件
    //参数:折叠控件
    public static void initCollapseAnim(final LinearLayout bar, final ImageView arrowIv, final LinearLayout collapseView) {
        //步骤三。使用measure(0,0)获取 折叠内容的宽高
        final int collapseHeight = 0;
        ;
        //3.1.先测量
        collapseView.measure(0, 0);
        //3.2获取测量值
        final int expandHeight = collapseView.getMeasuredHeight();
        //注意 getMeasuredHeight与布局时设置的width height无关。因为它返回的是内容的测量值
        collapseView.getLayoutParams().height = collapseHeight;
        arrowIv.setImageResource(R.drawable.arrow_down);
        collapseView.requestLayout();//view.layout(left,top,right,bottom)
        //步骤一。完成点击切换箭头状态
        bar.setOnClickListener(new View.OnClickListener() {
            private boolean collapse = true;

            @Override
            public void onClick(View v) {
                if (collapse) {
                    //折叠状态
                    collapse = false;
                    arrowIv.setImageResource(R.drawable.arrow_up);
                } else {
                    collapse = true;
                    arrowIv.setImageResource(R.drawable.arrow_down);
                }
                //步骤二。通过ValueAnimator 逐渐 地修改高度,达到折叠效果
                //2.1 依赖 nine old 动画库 compile 'com.nineoldandroids:library:2.4.0'
                //LayoutParams 一个布局参数封装。
                //2.2 创建属性动画里面的值动画
                int fromHeight = 0;
                int toHeigith = 0;
                fromHeight = collapse ? expandHeight : collapseHeight;
                toHeigith = collapse ? collapseHeight : expandHeight;
                ValueAnimator valueAnimator = ValueAnimator.ofInt(fromHeight, toHeigith);
                //设置定时时间
                valueAnimator.setDuration(500);
                //2.3.添加一个处理获取更新值的逻辑
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        //2.4 获取变化值 给折叠视图的高度
                        Integer newHeight = (Integer) animation.getAnimatedValue();
                        collapseView.getLayoutParams().height = newHeight;
                        collapseView.requestLayout();//view.layout(left,top,right,bottom)
                    }
                });
                //启动定时器
                valueAnimator.start();
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值