gpt4 book ai didi

java - ExpandableListView 中的 Android 数据绑定(bind)

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:56 25 4
gpt4 key购买 nike

我在 expandableListView 中遇到一个问题,它与数据绑定(bind)很奇怪。有数量的 TextView 可以在单击按钮时增加/减少。 TextView 在特定位置正确更新。但是当向下滚动时,值会在不同位置更改为 textview。此外,当组扩展值移动到下面的 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>

产品项目

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

}

问题是什么,可以采取什么措施来解决。

最佳答案

也许问题已经解决了,但如果没有,我会针对 View 不为 null 的情况更改 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;
}

关于java - ExpandableListView 中的 Android 数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50674788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com