滚动时 BaseAdapter 列表视图位置

BaseAdapter listview postion while scrolling

我有一个 ListView,里面有很多东西。每个项目都有一个按钮,当我点击它时,它变得不可见,另一个按钮变得可见。

问题是:当我滚动 ListView 时,许多其他项目按钮不可见。我该如何解决?

    public class homebuyer_fruits_adapter extends BaseAdapter {
    private ArrayList<Itemssetget> listData;
    private LayoutInflater layoutInflater;
    public  static   int cout=0;

    List  position_item=new ArrayList();

    Context context;

    String check;

    int lastpostition=-1;
    public static List<cartitemslist> cartlist=new ArrayList<>();
    public homebuyer_fruits_adapter(Context aContext, ArrayList<Itemssetget> listData,String number) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(aContext);
        context=aContext;
        check=number;
        this.notifyDataSetChanged();

    }
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View v, ViewGroup parent) {
        final ViewHolder holder;
        final cartitemslist homeitemslist = new cartitemslist();

        if (v == null) {
            v = layoutInflater.inflate(R.layout.food_vegitablews_view, null);
            holder = new ViewHolder();

                holder.name = (TextView) v.findViewById(R.id.name_item);
                holder.price = (TextView) v.findViewById(R.id.price_item);
                holder.add_q = (ImageView) v.findViewById(R.id.add_quantity);
                holder.delete_q = (ImageView) v.findViewById(R.id.delete_quantity);
                holder.quantity = (TextView) v.findViewById(R.id.add_quantity_items);

                //holder.description = (TextView) v.findViewById(R.id.description_item);
                //holder.id = (TextView) v.findViewById(R.id.);
                holder.imageView = (ImageView) v.findViewById(R.id.image_items);
                holder.discount = (TextView) v.findViewById(R.id.discout_price);
                holder.add = (Button) v.findViewById(R.id.addto_cart);
                holder.units=(TextView)v.findViewById(R.id.item_units);
                holder.percentage=(TextView)v.findViewById(R.id.discount_percentage);
                holder.linearLayou=(LinearLayout)v.findViewById(R.id.Quantity_control_linnear_view) ;
                holder.phone=check;

                v.setTag(holder);

        } else {
            holder = (ViewHolder) v.getTag();
            this.notifyDataSetChanged();
        }

        int i=Integer.parseInt(listData.get(position).getPrice())-Integer.parseInt(listData.get(position).getDiscountprice());
        int d=i*100;
        if((d/Integer.parseInt(listData.get(position).getPrice()))==0)
        {
            holder.percentage.setVisibility(View.GONE);
        }
        holder.percentage.setText(""+d/Integer.parseInt(listData.get(position).getPrice())+"% OFF");
           holder.name.setText(listData.get(position).getName());
           holder.price.setText("Rs " + listData.get(position).getPrice());
           if(listData.get(position).getPrice().equals(listData.get(position).getDiscountprice()))
           {
               holder.price.setVisibility(View.GONE);
           }
        holder.price.setPaintFlags(holder.price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
           holder.discount.setText("Rs " + listData.get(position).getDiscountprice());
           Picasso.with(context).load(listData.get(position).getImageurl())
                   .fit().centerCrop().into(holder.imageView);
           holder.units.setText(listData.get(position).getUnits());

            holder.add_q.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Toast.makeText(context,listData.get(position).getName(),Toast.LENGTH_LONG).show();

                    if(H.containsKey(position))
                    {
                        holder.quantity.setText(""+(1+Integer.parseInt(holder.quantity.getText().toString())));
                        cartlist.get(H.get(position)).setQuantity(holder.quantity.getText().toString());

                    }

                }
            });
            holder.delete_q.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(H.containsKey(position)) {

                   if(holder.quantity.getText().toString().equals("1"))
                   {
                       holder.add.setVisibility(View.VISIBLE);
                       holder.linearLayou.setVisibility(View.GONE);

                       int i=H.get(position);
                       cartlist.remove(i);
                       cout--;
                       Fruits.numberofitems.setText(cout + "");
                   }
                   else {

                           holder.quantity.setText("" + (Integer.parseInt(holder.quantity.getText().toString()) - 1));
                           cartlist.get(H.get(position)).setQuantity(holder.quantity.getText().toString());

                   }
                   }

                }
            });

            holder.add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Toast.makeText(context,"Add to cart"+holder.name.getText(),Toast.LENGTH_SHORT).show();

                        holder.add.setVisibility(View.GONE);
                        holder.linearLayou.setVisibility(View.VISIBLE);

                        // check=listData.get(position).getPhonenumber_seller();

                        if(cartlist.isEmpty()) {
                            cout++;
                            homeitemslist.setName(holder.name.getText().toString());
                            homeitemslist.setPrice(holder.price.getText().toString());
                            homeitemslist.setDiscountp(holder.discount.getText().toString());
                            homeitemslist.setQuantity(holder.quantity.getText().toString());
                            homeitemslist.setPhone_id(holder.phone);
                            homeitemslist.setImage(listData.get(position).getImageurl());
                            if(Fruits.numberofitems!=null)
                            {
                                Fruits.numberofitems.setText(cout + "");
                            }

                           shop_items.numberofitems.setText(cout + "");
                           buyer_home.numberofitems.setText(cout + "");
                            cartlist.add(homeitemslist);
                            H.put(position,cartlist.indexOf(homeitemslist));
                        }
                        else {
                            if(check.equals(cartlist.get(0).getPhone_id()))
                            {
                                cout++;
                                cartitemslist homeitemslist = new cartitemslist();
                                homeitemslist.setName(holder.name.getText().toString());
                                homeitemslist.setPrice(holder.price.getText().toString());
                                homeitemslist.setDiscountp(holder.discount.getText().toString());
                                homeitemslist.setQuantity(holder.quantity.getText().toString());
                                homeitemslist.setPhone_id(holder.phone);
                                homeitemslist.setImage(listData.get(position).getImageurl());
                                if(Fruits.numberofitems!=null)
                                {
                                    Fruits.numberofitems.setText(cout + "");
                                }

                                shop_items.numberofitems.setText(cout + "");
                               buyer_home.numberofitems.setText(cout + "");
                                cartlist.add(homeitemslist);
                               H.put(position,cartlist.indexOf(homeitemslist));


                            }
                            else
                            {
                                Toast.makeText(context,"Clear the Previous cart First than you can buy from this shop",Toast.LENGTH_LONG).show();
                            }

                    }


                }


            });


            // holder.description.setText(listData.get(position).getDiscription());

           // holder.id.setText(listData.get(position).getId());
            //holder.imageView.setImageAlpha(R.drawable.banana);
        setanimation(v,position);

        return v;
    }
    static class ViewHolder {
        LinearLayout linearLayou;
        TextView name;
        TextView price;
        TextView discount;
        ImageView add_q;
        ImageView delete_q;
        TextView quantity;
        ImageView imageView;
        TextView description;
        TextView units;
        TextView percentage;
       String phone;
        Button add;
    }

    public  void setanimation(View viewanim,int position)
    {
        if(position>lastpostition)
        {
            ScaleAnimation animation=new ScaleAnimation(0.0f,1.0f,0.0f,1.0f,
            Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            animation.setDuration(1500);
            viewanim.setAnimation(animation);
            lastpostition=position;
        }

    }


}

这是我的适配器代码。 我试了很多方法还是卡在这个问题上

您仅在 OnClickListener 中设置可见性,但您也需要在 getView() 中设置它,因为当您滚动时,您的视图将被回收(重新用于不同的项目) .

因此,在 getView() 中,您必须恢复视图的 complete 状态,包括按钮可见性。将所有状态信息存储在您的列表项中。在你的 getView() 中做这样的事情:

@Override
public View getView(final int position, View v, ViewGroup parent) {

    ...

    // the view (v) could be recycled and showing a state from another item
    // e.g. the add button is visible

    final Itemssetget listItem = listData.get(position);

    // so we have to restore the state of the view for listItem above
    // e.g. update the button visibility
    holder.add.setVisibility(listItem.isAddButtonVisible() ? View.VISIBLE : View.GONE); 
    holder.add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
            holder.add.setVisibility(View.GONE);
            // save the state of the button, so it can be restored later
            listItem.setAddButtonVisible(true); 
            ...
        }
    }
    ...
}

Itemssetget中添加必要的成员变量和getter/setter函数:

public class Itemssetget {
    ...
    private boolean isAddButtonVisible = false;
    
    public boolean isAddButtonVisible() {
        return isAddButtonVisible;
    }
    
    public void setAddButtonVisible(boolean isVisible) {
        this.isAddButtonVisible = isVisible;
    }
    ...
}

PS:你的代码看起来很糟糕。看看 naming conventions.