RecyclerView.Adapter只让一个子控件展开

工作原理:上次讲解的ListView作为RecyclerView的子控件显示不出item的项目中

每个item点击它都会展开它的子控件,而我做的这个项目要求每次点开一个子控件上一个展开的子控件就自动收起。

 

首先在itemClicklisten添加些方法

 

 private ItemClickListener itemClickListener = new ItemClickListener() {
        @Override
        public void onExpandChildren(DataBean bean) {
            /*如果临时DataBean对象temData不为空或者是展开状态我就将它收起*/
            if (temData != null && temData.isExpand()) {
                DataBean dataBean = dataBeanList.get(getCurrentPosition(temData.getID()));
                dataBean.setHide(true);//设置一个是否能隐藏的标志位
                dataBean.setExpand(false);
                onHideChildren(dataBean);//必须先隐藏再刷新不然程序会死掉报错,因为在RecyclerView在调用scrollToPosition(pos)时不能刷新控件,
                //onHideChildren(DataBean bean)里面接口回调了这个方法。
                notifyItemChanged(getCurrentPosition(temData.getID()));
            }
              //记录上一个DataBean对象
            temData = bean;

 

然后再ParentViewHolder设置一些判断

 

    if (dataBean.isExpand()) {
            expand.setRotation(90);
            parentDashedView.setVisibility(View.INVISIBLE);
        } else {
            //判断这个dataBean要隐藏,能的话就做出收起来的动画。
            if (dataBean.isHide()) {
                Logs.v("Hide  " + dataBean.getID());
                parentDashedView.setVisibility(View.VISIBLE);
                rotationExpandIcon(90, 0);
                dataBean.setHide(false);
            } else {
                expand.setRotation(0);
                parentDashedView.setVisibility(View.VISIBLE);
            }
        }

 

  • 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、付费专栏及课程。

余额充值