android - какая кнопка нажимается на дочернем элементе expandableListView - PullRequest
3 голосов
/ 25 июня 2011

Здравствуйте, everibody и спасибо, что прочитали меня.

Я надеюсь, что буду правильно выражать себя, потому что я француз ... Пожалуйста, скажите мне, если вы что-то не понимаете.

У меня есть атрибут extendedableListActivity, и с помощью адаптера я добавил в него childs. У каждого ребенка есть две кнопки, которые уменьшают или увеличивают число (названное количество). Моя трудность состоит в том, чтобы узнать, когда ребенок нажимает кнопку, что касается ребенка. Обратите внимание, что при нажатии кнопки соответствующий ребенок не всегда является выбранным ребенком.

Я видел эту страницу http://www.geekmind.net/2009/11/android-custom-list-item-with-nested.html благодаря ответу Andikki на этой странице Создание кнопок в расширяемом списке , и я частично решил проблему.

Действительно, решение тега, похоже, решает проблему, но это не так. У меня ошибка, когда я быстро нажимаю на кнопку. Все работает, но если я быстро нажму на кнопку, например, 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();

}

1 Ответ

1 голос
/ 27 июня 2011

Я не уверен, но думаю, что решил проблему.На данный момент я больше не вижу ошибку.

Вот "решение" (если кто-то может объяснить мне ...).

Я удалил следующий код:

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