滚动时的 BaseAdapter 列表视图位置

Posted

技术标签:

【中文标题】滚动时的 BaseAdapter 列表视图位置【英文标题】:BaseAdapter listview postion while scrolling 【发布时间】:2020-10-29 16:24:00 【问题描述】:

我有一个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;
        

    



这是我的适配器代码。 我尝试了很多方法,但仍然停留在这个问题上。

【问题讨论】:

不能直接在onClick里面使用holder.view,使用setTag()来标记位置等信息。检查我的答案***.com/questions/31169131/… 那我该怎么办?指导我 ViewHolder h=listData.get(position)view.getTag();错误 【参考方案1】:

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

所以在getView() 中,您必须恢复视图的完成 状态,包括按钮可见性。将所有状态信息存储在列表项中。在你的 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。

【讨论】:

我没有得到它,如果你有任何链接,请解释它,然后分享我 这个答案提供了 ListView 如何工作的基本解释:***.com/a/14108676/4706541 我更新了代码示例以更好地解释解决方案 在这个 listItem.isAddButtonVisible() 中显示错误 您必须在Itemssetget 中创建getter 和setter 函数,它们还不存在。您还需要一个成员变量isAddButtonVisible 来保存状态。我没有包含该代码,因为它应该是不言自明的

以上是关于滚动时的 BaseAdapter 列表视图位置的主要内容,如果未能解决你的问题,请参考以下文章

滚动列表视图 BaseAdapter 上的复选框选中/未选中状态更改

使用带有自定义 baseadapter 的 arraylist 错误地删除列表视图项位置

如何在 Baseadapter 中获得位置

如何在android中使用baseadapter刷新自定义列表视图

Android:回收站视图没有滚动到最后一个位置

从上到下滚动时如何自动停止对项目的列表视图取消选择?