工作原理:上次讲解的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);
}
}