我有这个列表视图,您可以在其中选择要购买的商品。单击某个项目时,将显示一组元素。 (复选框,2个按钮和文本视图。)您可以在哪里选择要购买的产品数量。
这组元素隐藏在布局中。这组元素的可见性从VIEW.GONE变为VIEW.VISIBLE
问题是按钮在其他行中随机出现。 例如,当我点击THIN BEER时,视图中会出现元素组,但是当我向下滚动应用程序时,元素组也会出现在HAMBURGER行和其他行上。
当我更改其中一个textview的值时,也会增加其他元素。
public class ProductAdapter extends ArrayAdapter<Product> {
ArrayList<Boolean> positionArray;
public ProductAdapter(Context context, ArrayList<Product> products){
super(context,0,products);
positionArray = new ArrayList<Boolean>(products.size());
for(int i =0;i<products.size();i++){
positionArray.add(false);
}
}
public static class ViewHolder{
TextView tv_price,tv_prodName, tv_details, tv_location;
ImageView imgView_productImage;
CheckBox checkBox;
Button bntUp,btnDown;
}
//When a listview recycles views , it recycles its present state as well as listeners attached to it.if the checkbox was checked and has a
// onCheckedChangeListener set, both will remain a part of recycled view based on position.
// So it is our responsibility to reset all states and remove previous listeners.
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
Product product = getItem(position);
if(convertView==null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_product_item,parent, false);
viewHolder = new ViewHolder();
viewHolder.imgView_productImage = (ImageView)convertView.findViewById(R.id.list_item_imgView_productImage);
viewHolder.tv_price = (TextView)convertView.findViewById(R.id.listItem_tv_price);
viewHolder.tv_prodName = (TextView)convertView.findViewById(R.id.listItem_tv_productName);
viewHolder.tv_location = (TextView)convertView.findViewById(R.id.listItem_tv_location);
viewHolder.tv_details = (TextView)convertView.findViewById(R.id.listItem_tv_productDetail);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.listProd_checkbox);
viewHolder.bntUp = (Button)convertView.findViewById(R.id.listProd_up);
viewHolder.btnDown = (Button)convertView.findViewById(R.id.listProd_down);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
viewHolder.checkBox.setOnCheckedChangeListener(null); //
}
viewHolder.tv_price.setText("$" + String.valueOf(product.getPrice()));
viewHolder.tv_prodName.setText(product.getProductName());
viewHolder.tv_location.setText(product.getLocation());
viewHolder.tv_details.setText(product.getProductDetail());
viewHolder.imgView_productImage.setImageBitmap(MifareUtilities.getDecodeImage(product.getProductImage()));
viewHolder.checkBox.setChecked(positionArray.get(position));
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
positionArray.set(position,true);
}else {
positionArray.set(position,false);
}
}
});
return convertView;
}
`}
这是我片段中的代码,我在其中显示listView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
productsList = new HashMap<>();
final View viewroot = inflater.inflate(R.layout.fragment_payment, container, false);
final ArrayList<Product> products = (ArrayList<Product>)new ProductController(new ProductFromString()).getProducts();
final ListView listView = (ListView)viewroot.findViewById(R.id.fragPayment_listview);
ProductAdapter productAdapter = new ProductAdapter(getActivity(),products);
listView.setAdapter(productAdapter);
listView.setLongClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
final int positionf = position;
final CheckBox checkBox = (CheckBox)view.findViewById(R.id.listProd_checkbox);
checkBox.setVisibility(View.VISIBLE);
checkBox.setChecked(!checkBox.isChecked());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if(!checkBox.isChecked()){
productsList.remove(String.valueOf(position));
}else {
Product product = (Product) listView.getAdapter().getItem(positionf);
productsList.put(String.valueOf(position),product);
}
}
});
final Button btnUp = (Button)view.findViewById(R.id.listProd_up);
final Button btnDown = (Button)view.findViewById(R.id.listProd_down);
final TextView quantity = (TextView)view.findViewById(R.id.listItem_quantity);
btnUp.setTag(position);
btnDown.setTag(position);
btnDown.setVisibility(View.VISIBLE);
btnUp.setVisibility(View.VISIBLE);
quantity.setVisibility(View.VISIBLE);
btnDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//String q = quantity.getText().toString();
int quan = Integer.parseInt(quantity.getText().toString());
//int btnPosition = (Integer)v.getTag();
if(quan!=1){
quan --;
quantity.setText(String.valueOf(quan));
updateProductQuantity(listView,(Integer)v.getTag(),Integer.parseInt(quantity.getText().toString()) );
}else { //if the quantity is 0 it will hide the buttons and uncheck
btnDown.setVisibility(View.GONE);
btnUp.setVisibility(View.GONE);
checkBox.setChecked(false);
quantity.setVisibility(View.GONE);
checkBox.setVisibility(View.GONE);
//remove product from list
productsList.remove(String.valueOf((Integer)v.getTag()));
}
}
});
btnUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int quan = Integer.parseInt(quantity.getText().toString());
quan++;
quantity.setText(String.valueOf(quan));
// updateProductQuantity(listView,(Integer)v.getTag(),Integer.parseInt(quantity.getText().toString()) );
}
});
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final Product product = (Product) listView.getAdapter().getItem(position);
//dlgShowConfig(product);
Intent intentActivityPayment = new Intent(getActivity(), ActivityPayment.class);
intentActivityPayment.putExtra("productObject",product);
startActivity(intentActivityPayment);
return true; //evitara que se ejecute el onitemclicklistener cuando ejecutes el onitemlongclicklisstener
}
});
return viewroot;
}
答案 0 :(得分:1)
如果我通过向下滚动创建新的ListView项目而正确理解您,则项目中会显示所有这些按钮。
逻辑表明,您需要做的是在创建项目时再次隐藏按钮,也就是在适配器中。
答案 1 :(得分:0)
你是对的,逻辑需要在适配器中,你需要将状态保存在变量中。 我已经保存了我的复选框的状态,以保存我在产品模型中添加布尔变量的状态。
感谢@PAVAN的帮助。
src: url("~/font/material-design-icons/Material-Design-Icons.eot?#iefix") format("embedded-opentype"), url("../font/material-design-icons/Material-Design-Icons.woff") format("woff"), url("../font/material-design-icons/Material-Design-Icons.ttf") format("truetype"), url("../font/material-design-icons/Material-Design-Icons.svg#Material-Design-Icons") format("svg");