首先我来说说我的项目场景:我有这样一个需求,向右滑动ExpandableListView中的Child,动态显示Child中的【Delete】按钮。
这是我Child节点的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textChildTitle"
android:text="No Data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="40dp"
android:layout_gravity="left|center_vertical" />
<TextView
android:id="@+id/textChildMoney"
android:text="No Data"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="10dp"
android:gravity="right|center_vertical" />
<Button
android:id="@+id/btnDel"
android:text="删除"
android:textColor="@android:color/white"
android:textSize="10dp"
android:background="@drawable/btn_delete_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_gravity="right|center_vertical"
android:visibility="gone" />
</LinearLayout>
前面的两个是显示数据的,最后一个Button是删除按钮,默认不显示。当我在界面上面滑动的时候,获取滑动位置的View,然后通过findViewById来获取Button,最后设置Button为显示状态,代码如下://获取手指开始按屏幕的位置和最后抬起手指的位置
int pos1 = this.elv.pointToPosition((int)e1.getX(),(int)e1.getY());
int pos2 = this.elv.pointToPosition((int)e2.getX(),(int)e2.getY());
//先判断是否触发Child的delete事件
//如果起始位置【pos1】和结束位置【pos2】是在同一个【Child】并且这个【Child】的位置有效
//并且滑动距离大于100px,则触发Delete
if (pos1 > -1 && pos1 == pos2 && (e2.getX() - e1.getX() > 100) && (e2.getX() - e1.getX() < 200)){
//获取当前Child的【Delete】按钮
Button btnDel = (Button)elv.getChildAt(pos1).findViewById(R.id.btnDel);
if (btnDel != null){
btnDel.setVisibility(View.VISIBLE);
btnDel.setText("删除"+ pos1); //我将当前所在的位置【Pos1】写到按钮上面,方便调试
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "POS:" + ((TextView)((View)v.getParent()).findViewById(R.id.textChildMoney)).getText(), Toast.LENGTH_SHORT).show();
}
});
Log.i("BTN Show","SHOW:" + pos1);
//btnDel.setfo
return true;
}
}然后在模拟器上面调试,下面是调试步骤:
1、这是程序刚运行的时候的状态【如下图】:2、我展开第二组,并且在第一个Child上面滑动触发Delete【效果如下图】:如上图所示,按钮成功显示,并且后面显示了【Pos】当前所在的位置,但是我不明白:为什么当前位置为2?
3、我再在第二组上面,在第二个Child上面滑动【效果如下】:这边也是正常的,位置为3。
4、接下来,是为题出现的地方,我点击第一组,第一组展开,出现一个匪夷所思的现象:如图:第二组的第二个Child中的按钮,显示到第一组的第一个Child上面,为什么会出现这样的问题?请各位帮帮忙,这边我研究了好久,终究不知道是怎么回事,先谢谢了!
这是我Child节点的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textChildTitle"
android:text="No Data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="40dp"
android:layout_gravity="left|center_vertical" />
<TextView
android:id="@+id/textChildMoney"
android:text="No Data"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="10dp"
android:gravity="right|center_vertical" />
<Button
android:id="@+id/btnDel"
android:text="删除"
android:textColor="@android:color/white"
android:textSize="10dp"
android:background="@drawable/btn_delete_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_gravity="right|center_vertical"
android:visibility="gone" />
</LinearLayout>
前面的两个是显示数据的,最后一个Button是删除按钮,默认不显示。当我在界面上面滑动的时候,获取滑动位置的View,然后通过findViewById来获取Button,最后设置Button为显示状态,代码如下://获取手指开始按屏幕的位置和最后抬起手指的位置
int pos1 = this.elv.pointToPosition((int)e1.getX(),(int)e1.getY());
int pos2 = this.elv.pointToPosition((int)e2.getX(),(int)e2.getY());
//先判断是否触发Child的delete事件
//如果起始位置【pos1】和结束位置【pos2】是在同一个【Child】并且这个【Child】的位置有效
//并且滑动距离大于100px,则触发Delete
if (pos1 > -1 && pos1 == pos2 && (e2.getX() - e1.getX() > 100) && (e2.getX() - e1.getX() < 200)){
//获取当前Child的【Delete】按钮
Button btnDel = (Button)elv.getChildAt(pos1).findViewById(R.id.btnDel);
if (btnDel != null){
btnDel.setVisibility(View.VISIBLE);
btnDel.setText("删除"+ pos1); //我将当前所在的位置【Pos1】写到按钮上面,方便调试
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "POS:" + ((TextView)((View)v.getParent()).findViewById(R.id.textChildMoney)).getText(), Toast.LENGTH_SHORT).show();
}
});
Log.i("BTN Show","SHOW:" + pos1);
//btnDel.setfo
return true;
}
}然后在模拟器上面调试,下面是调试步骤:
1、这是程序刚运行的时候的状态【如下图】:2、我展开第二组,并且在第一个Child上面滑动触发Delete【效果如下图】:如上图所示,按钮成功显示,并且后面显示了【Pos】当前所在的位置,但是我不明白:为什么当前位置为2?
3、我再在第二组上面,在第二个Child上面滑动【效果如下】:这边也是正常的,位置为3。
4、接下来,是为题出现的地方,我点击第一组,第一组展开,出现一个匪夷所思的现象:如图:第二组的第二个Child中的按钮,显示到第一组的第一个Child上面,为什么会出现这样的问题?请各位帮帮忙,这边我研究了好久,终究不知道是怎么回事,先谢谢了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货