【问题标题】:android - what button is clicked on expandableListView's childandroid - 在 expandableListView 子项上单击什么按钮
【发布时间】:2011-09-22 13:15:53
【问题描述】:

大家好,感谢阅读。

我希望我能正确表达自己,因为我是法国人......如果你有什么不明白的地方请告诉我。

我有一个可扩展列表活动,并且通过一个适配器,我已经在上面放置了孩子。每个孩子都有两个减少或增加数字(命名数量)的按钮。我的困难是要知道,当按下按钮时,关心的是哪个孩子。请注意,当按下按钮时,相关的孩子并不总是被选中的孩子。

感谢Andikki 在此页面Creating buttons in expandablelistview 上的回答,我已经看到了这个页面http://www.geekmind.net/2009/11/android-custom-list-item-with-nested.html,我已经部分解决了这个问题。

确实,标签的解决方案似乎解决了问题,但事实并非如此。 当我快速单击一个按钮时,我遇到了一个错误。一切正常,但如果我快速点击一个按钮,例如在最短的时间内点击 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();

}

【问题讨论】:

    标签: android button expandablelistview


    【解决方案1】:

    我不确定,但我认为我已经解决了这个问题。 目前,我看不到这个错误了。

    这是“解决方案”(如果有人可以解释我......)。

    我删除了以下代码:

    if (convertView != null){
        v = convertView;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多