android - 在 ExpandableListView 的子级上单击什么按钮
大家好,感谢阅读我的文章。
我希望我能正确表达自己,因为我是法国人...如果您不明白什么,请告诉我。
我有一个可扩展列表活动,并且通过适配器,我将孩子放在上面。每个孩子都有两个按钮,可以减少或增加数字(指定数量)。我的困难是知道当按下按钮时,孩子关心的是哪个孩子。请注意,按下按钮时,相关的子项并不总是所选的子项。
我看过这个页面 http://www .geekmind.net/2009/11/android-custom-list-item-with-nested.html感谢Andikki在此页面上的回答在 Expandablelistview 中创建按钮 我已经部分解决了这个问题。
确实,标签的解决方案看似解决了问题,但事实并非如此。 当我快速单击按钮时出现错误。一切正常,但如果我快速单击一个按钮,例如在最短的时间内单击 20 次,则所单击的按钮并不总是能够被正确检测到。
就我而言,我想增加或减少孩子的数量“数量”。当我单击加号按钮或减号按钮时,它就起作用了。 但是当我快速点击同一个按钮时,恰好增加或减少的数量不是好孩子的数量!
我把我的代码如下,如果您想要更多,请告诉我。 这是 child_row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/childname"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:maxWidth="150dip"
android:singleLine="false"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|right"
android:gravity="center_vertical|right">
<ImageView
android:id="@+id/decrease_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/minus"
android:background="@color/transparent"
android:clickable="true"
android:onClick="decreaseQuantity"/>
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<ImageView
android:id="@+id/increase_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/plus"
android:background="@color/transparent"
android:clickable="true"
android:onClick="increaseQuantity"/>
</LinearLayout>
</LinearLayout>
这是我的适配器中 getChildView 方法的摘录:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = null;
if (convertView != null){
v = convertView;
}
else{
v = inflater.inflate(R.layout.child_row, parent, false);
}
Product product = (Product)getChild(groupPosition,childPosition);
ImageView decreaseQuantityImageView = (ImageView)v.findViewById(R.id.decrease_quantity);
decreaseQuantityImageView.setTag(groupPosition + "/" + childPosition);
TextView quantity = (TextView)v.findViewById(R.id.quantity);
quantity.setText(String.valueOf(product.getQuantity()));
ImageView increaseQuantityImageView = (ImageView)v.findViewById(R.id.increase_quantity);
increaseQuantityImageView.setTag(groupPosition + "/" + childPosition);
// I do that for all ImageView clickable of child
return v;
}
这里是我的活动(扩展 ExpandableListActivity)和 getTag() 的摘录
public void increaseQuantity(View v){
ImageView increaseQuantityImageView = (ImageView)v.findViewById(R.id.increase_quantity);
String positions = (String) increaseQuantityImageView.getTag();
String tabOfPositions[] = positions.split("\\/");
Product product = (Product)this.retailerAdapter.getChild(Integer.parseInt(tabOfPositions[0]), Integer.parseInt(tabOfPositions[1]));
product.increaseQuantity();
this.retailerAdapter.notifyDataSetChanged();
}
Hello everibody and thanks to read me.
I hope I will express correctly myself because i'm french... Please tell me if you don't understand something.
I have an expandableListActivity, and, with an adapter, I have put childs on it. Every child has two buttons which reduce or increased a number (named quantity). My difficulty is to know, when a button is pressed, what child is concerned. Please note that when a button is pressed, the child concerned is not always the child selected.
I have seen this page http://www.geekmind.net/2009/11/android-custom-list-item-with-nested.html thanks to the Andikki's answer on this page Creating buttons in expandablelistview and I have partially solve the problem.
Indeed, the tag's solution seem to solve the problem, but it not.
I have a bug when I quickly click on a button. All work, but if i quickly click on a button, for example 20 times in a minimum of time, the button clicked is not always right detected.
In my case, i whant to increase or decrease a number "quantity" in a child. When i click on the plus button, or the minus button, it works.
But when i quickly click on a same button, it happends that the quantity increased or decreased is not the quantity of the good child !
I put you my code as follow, if you want more, please tell me.
Here it's the child_row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/childname"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:maxWidth="150dip"
android:singleLine="false"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|right"
android:gravity="center_vertical|right">
<ImageView
android:id="@+id/decrease_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/minus"
android:background="@color/transparent"
android:clickable="true"
android:onClick="decreaseQuantity"/>
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<ImageView
android:id="@+id/increase_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/plus"
android:background="@color/transparent"
android:clickable="true"
android:onClick="increaseQuantity"/>
</LinearLayout>
</LinearLayout>
Here it's an extract of the getChildView method in my adapter :
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = null;
if (convertView != null){
v = convertView;
}
else{
v = inflater.inflate(R.layout.child_row, parent, false);
}
Product product = (Product)getChild(groupPosition,childPosition);
ImageView decreaseQuantityImageView = (ImageView)v.findViewById(R.id.decrease_quantity);
decreaseQuantityImageView.setTag(groupPosition + "/" + childPosition);
TextView quantity = (TextView)v.findViewById(R.id.quantity);
quantity.setText(String.valueOf(product.getQuantity()));
ImageView increaseQuantityImageView = (ImageView)v.findViewById(R.id.increase_quantity);
increaseQuantityImageView.setTag(groupPosition + "/" + childPosition);
// I do that for all ImageView clickable of child
return v;
}
And here an extract of my activity (extends ExpandableListActivity) with the getTag()
public void increaseQuantity(View v){
ImageView increaseQuantityImageView = (ImageView)v.findViewById(R.id.increase_quantity);
String positions = (String) increaseQuantityImageView.getTag();
String tabOfPositions[] = positions.split("\\/");
Product product = (Product)this.retailerAdapter.getChild(Integer.parseInt(tabOfPositions[0]), Integer.parseInt(tabOfPositions[1]));
product.increaseQuantity();
this.retailerAdapter.notifyDataSetChanged();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定,但我认为我已经解决了问题。
目前,我没有再看到这个错误。
这是“解决方案”(如果有人可以解释我......)。
我已经删除了以下代码:
I'm not sure, but I think that I have solved the problem.
For the moment, I don't see the bug anymore.
Here is the "solution" (if somebody can explain me...).
I have deleted the following code :