ExpandableListView 中的 Android 数据绑定 [英] Android Databinding in ExpandableListView

查看:24
本文介绍了ExpandableListView 中的 Android 数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 expandableListView 中有一个问题,这对数据绑定很奇怪.有数量的文本视图,可以在按钮单击时增加/减少.Textview 在特定位置正确更新.但是当向下滚动时,该值会在不同位置更改为 textview.此外,当组扩展值移动下面的项目 textview.Below 是适配器,xml 和模型类.

I have a problem in expandableListView that is weird with databinding.There is textview of quantity which can be incremented/decremented on button click.The Textview correctly updates at specific position. But when scrolled down the value changes the to textview at different position. Also , when group expanded value move the below item textview.Below are adapter , xml and model class.

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>

产品项目

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);

    }

问题是什么以及可以做些什么来解决这个问题.

What is the problem and what can be done to solve this.

推荐答案

也许问题已经解决了,但如果没有,我会更改 getGroupView(...) 方法(以及 getChildView 类似),用于视图不存在的情况null 像这样:

Maybe the problem is solved already but if not, I would change the getGroupView(...) method (and getChildView likewise) for the case when view is not null like this:

@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;
}

这篇关于ExpandableListView 中的 Android 数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆