Привязка данных Android в ExpandableListView - PullRequest
0 голосов
/ 04 июня 2018

У меня проблема в expandableListView, что странно с привязкой данных. Существует текстовое представление количества, которое может быть увеличено / уменьшено при нажатии кнопки. Текстовое представление корректно обновляется в определенной позиции.Но при прокрутке вниз значение меняется на textview в другом положении.Кроме того, когда расширенное значение группы перемещает текстовое представление элемента ниже. Ниже указаны адаптер, XML и класс модели.

public class ProductExpandableListAdapter extends BaseExpandableListAdapter {

    private Activity contextActivity;
    private ArrayList<ProductItems> productItemsArrayList;

    public ProductExpandableListAdapter(ProductActivity productActivity, ArrayList<ProductItems> productItemsArrayList) {
        this.contextActivity = productActivity;
        this.productItemsArrayList = productItemsArrayList;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver dataSetObserver) {

    }


    @Override
    public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

    }

    @Override
    public int getGroupCount() {
        return this.productItemsArrayList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 1;
    }

    @Override
    public Object getGroup(int i) {
        return this.productItemsArrayList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return this.productItemsArrayList.get(i).getProductItemDescription();

    }


    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        final ProductItems productItems = productItemsArrayList.get(i);
        if (view == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
            final ProductItemBinding productItemsBinding = DataBindingUtil.inflate(layoutInflater, R.layout.product_items, viewGroup, false);
            HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.productTitleTextView);
            productItemsBinding.productTitleTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_64_pt));
            view = productItemsBinding.getRoot();
            HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.productPriceTextView);
            productItemsBinding.productPriceTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_72_pt));
            HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.qtyTextView);
            productItemsBinding.qtyTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_104_pt));
            productItemsBinding.setHandler(new QuantityHandler() {
                @Override
                public void onQuantityIncrement() {
                    changeQuantity(productItemsBinding, productItems, productItems.getProductItemQty(), Constants.ADDITION);
                    Toast.makeText(contextActivity, "quantityIncrement : ", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onQuantityDecrement() {
                    changeQuantity(productItemsBinding, productItems, productItems.getProductItemQty(), Constants.SUBTRACTION);
                    Toast.makeText(contextActivity, "quantityDecrement : ", Toast.LENGTH_LONG).show();
                }
            });
            productItemsBinding.setProductItems(productItems);

        }
        return view;
    }


    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

        if (view == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
            final ProductItemsChildBinding productItemsChildBinding = DataBindingUtil.inflate(layoutInflater, R.layout.product_items_child, viewGroup, false);
            HelveticaNeueFontHelper.applyHelveticaneuelightFont(contextActivity, productItemsChildBinding.productDescriptionTextView);
            productItemsChildBinding.productDescriptionTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_64_pt));
            productItemsChildBinding.setProductItems(productItemsArrayList.get(i));
            view = productItemsChildBinding.getRoot();
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int i) {
    }

    @Override
    public void onGroupCollapsed(int i) {

    }

    @Override
    public long getCombinedChildId(long l, long l1) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long l) {
        return 0;
    }


    private void changeQuantity(final ProductItemBinding productItemsBinding, ProductItems productItems, ObservableInt observableIntQty, String operand) {


        switch (operand) {
            case Constants.ADDITION:
                if (observableIntQty.get() >= 0) {
                    observableIntQty.set(observableIntQty.get() + 1);
                }
                break;
            case Constants.SUBTRACTION:
                if (observableIntQty.get() > 0) {
                    observableIntQty.set(observableIntQty.get() - 1);
                }
                break;

        }

        contextActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                productItemsBinding.executePendingBindings();
            }
        });


    }


}

<data class="ProductItemBinding">

    <variable
        name="productItems"
        type="com.vtrio.waterapp.data.ProductItems" />

    <variable
        name="handler"
        type="com.vtrio.waterapp.listeners.QuantityHandler" />

</data>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/rounded_corner_bluegradient"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:weightSum="1">


    <ImageView
        android:id="@+id/productImageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        app:imgSrc="@{productItems.productItemImage}" />


    <LinearLayout
        android:id="@+id/productInfoLinearLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_weight="0.45"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/productTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{productItems.productItemTitleName}" />


        <TextView
            android:id="@+id/productPriceTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{productItems.productItemPrice}" />

    </LinearLayout>


    <RelativeLayout
        android:id="@+id/quantityModRelativeLayout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="0.25"
        android:gravity="center">


        <Button
            android:id="@+id/subtractQtyButton"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="@color/addsubColor"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="@{(v) -> handler.onQuantityDecrement()}"
            android:text="@string/sub"
            android:textColor="@color/addsubTextColor" />


        <TextView
            android:id="@+id/qtyTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@{Integer.toString(productItems.productItemQty)}" />

        <Button
            android:id="@+id/addQtyButton"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="@color/addsubColor"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="@{(v) -> handler.onQuantityIncrement()}"
            android:text="@string/add"
            android:textColor="@color/addsubTextColor" />

    </RelativeLayout>


</LinearLayout> </layout>

ProductItems

public class ProductItems extends BaseObservable {

    private String productItemTitleName;
    private String productItemPrice;
    private int productItemImage;
    private String productItemDescription;
    private ObservableInt productItemQty;
    private ArrayList<ProductItems> cartItem;


    public ProductItems(String productItemTitleName, String productItemPrice, int productItemImage, String productItemDescription, ObservableInt productItemQty) {
        this.productItemTitleName = productItemTitleName;
        this.productItemPrice = productItemPrice;
        this.productItemImage = productItemImage;
        this.productItemDescription = productItemDescription;
        this.productItemQty = productItemQty;
    }


    public String getProductItemTitleName() {
        return productItemTitleName;
    }

    public void setProductItemTitleName(String productItemTitleName) {
        this.productItemTitleName = productItemTitleName;
    }

    public String getProductItemPrice() {
        return productItemPrice;
    }

    public void setProductItemPrice(String productItemPrice) {
        this.productItemPrice = productItemPrice;
    }

    public int getProductItemImage() {
        return productItemImage;
    }

    public void setProductItemImage(int productItemImage) {
        this.productItemImage = productItemImage;
    }

    public String getProductItemDescription() {
        return productItemDescription;
    }

    public void setProductItemDescription(String productItemDescription) {
        this.productItemDescription = productItemDescription;
    }

    @Bindable
    public ObservableInt getProductItemQty() {
        return productItemQty;

    }


    public void setProductItemQty(ObservableInt productItemQty) {
        this.productItemQty = productItemQty;
        notifyPropertyChanged(BR.productItemQty);

    }

Чтопроблема и что можно сделать для ее решения.

1 Ответ

0 голосов
/ 03 июля 2018

Возможно, проблема уже решена, но если нет, я бы изменил метод getGroupView (...) (и аналогично getChildView) для случая, когда представление не равно нулю, как это:

@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
    final ProductItems productItems = productItemsArrayList.get(i);
    final ProductItemBinding  productItemsBinding;
    if (view == null) {
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        productItemsBinding = DataBindingUtil.inflate(layoutInflater, R.layout.product_items, viewGroup, false);
        HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.productTitleTextView);
        productItemsBinding.productTitleTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_64_pt));
        view = productItemsBinding.getRoot();
        HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.productPriceTextView);
        productItemsBinding.productPriceTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_72_pt));
        HelveticaNeueFontHelper.applyHelveticaneuemedFont(contextActivity, productItemsBinding.qtyTextView);
        productItemsBinding.qtyTextView.setTextSize(contextActivity.getResources().getDimension(R.dimen.dim_104_pt));
    }
    else
    {
        productItemsBinding = (ProductItemBinding) view.getTag();
    }

    productItemsBinding.setHandler(new QuantityHandler() {
        @Override
        public void onQuantityIncrement() {
            changeQuantity(productItemsBinding, productItems, productItems.getProductItemQty(), Constants.ADDITION);
            Toast.makeText(contextActivity, "quantityIncrement : ", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onQuantityDecrement() {
            changeQuantity(productItemsBinding, productItems, productItems.getProductItemQty(), Constants.SUBTRACTION);
            Toast.makeText(contextActivity, "quantityDecrement : ", Toast.LENGTH_LONG).show();
        }
    });
    productItemsBinding.setProductItems(productItems);

    view.setTag(productItemsBinding);

    return view;
}
...