代码之家  ›  专栏  ›  技术社区  ›  Xay

我正在使用GridView,当我滚动它时,收藏夹按钮的状态将更改为默认状态

  •  -3
  • Xay  · 技术社区  · 7 年前

    我使用gridview来显示图像和文本,就像一个简单的电子商务应用程序有一个收藏夹图标一样,当我滚动视图时,我标记为收藏夹的项目将更改为其默认状态,即(未标记),尽管,就像一个RecyclerView,但我没有使用它,因此无法找到视图更改的任何线索,请给出正确的解释。

    适配器类

    public class Category_Adapter extends BaseAdapter {
        private Context mContext;
        String name[], img[];
        int price[];
        LayoutInflater inflater;
    
        public Category_Adapter(Context context, String[] name, String[] img, int price[]) {
            this.mContext = context;
            this.price = price;
            this.name = name;
            this.img = img;
            inflater = (LayoutInflater) context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        }
    
        Category_Adapter(Context context) {
            this.mContext = context;
        }
    
        @Override
        public int getCount() {
            return name.length;
        }
    
        @Override
        public Object getItem(int position) {
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        public class Holderr {
            TextView product_name;
            Button addCart;
            TextView price_pro;
            ImageView product_img, mark, marked;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final Holderr holderr = new Holderr();
            View rowView;
            rowView = inflater.inflate(R.layout.product_category_grid, null);
            holderr.product_img = (ImageView
                    ) rowView.findViewById(R.id.imageofitem);
            holderr.addCart = rowView.findViewById(R.id.cartmein);
            holderr.mark = rowView.findViewById(R.id.markFav);
            holderr.marked = rowView.findViewById(R.id.markedFav);
            holderr.product_name = (TextView) rowView.findViewById(R.id.productkanam);
            holderr.price_pro = (TextView) rowView.findViewById(R.id.priceof_item);
            holderr.price_pro.setText("Rs. " + price[position]);
            holderr.product_name.setText(name[position]);
            Glide.with(mContext).load(img[position]).into(holderr.product_img);
            holderr.addCart.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toasty.success(mContext, name[position] + " Added in cart", Toast.LENGTH_SHORT).show();
                }
            });
            holderr.mark.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holderr.mark.setVisibility(View.GONE);
                    holderr.marked.setVisibility(View.VISIBLE);
                }
            });
            holderr.marked.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holderr.marked.setVisibility(View.GONE);
                    holderr.mark.setVisibility(View.VISIBLE);
    
                }
            });
    
            return rowView;
        }
    
    }
    

    在GridView中设置适配器的类

    public class ViewMoreCategory extends AppCompatActivity {
        GridView gridView;
        ImageButton back, filter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_more_category);
            filter = findViewById(R.id.filter_items);
            gridView = findViewById(R.id.product_cat_grid);
            String name[] = {"Shampoo", "Oil", "Ghee", "Cream", "Shampoo", "Oil", "Ghee", "Cream", "Shampoo", "Oil"};
            String img[] = {"http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png"};
            int price[] = {500, 200, 500, 100, 50, 330, 200, 300, 900, 400};
            gridView.setAdapter(new Category_Adapter(ViewMoreCategory.this, name, img, price));
             getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            getSupportActionBar().setDisplayShowCustomEnabled(true);
            getSupportActionBar().setCustomView(R.layout.custom_toolbar);
            View view = getSupportActionBar().getCustomView();
            back = view.findViewById(R.id.backtoHome);
            gridView.setAdapter(new Category_Adapter(this, name, img, price));
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(ViewMoreCategory.this, Dashboard.class));
                }
            });
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Milad    7 年前

    我认为你需要保持所选物品的位置

    1. 将属性添加到 Model class 如下所示:
      private int state;
    2. 获取所选项目的位置,然后更改 state 所有物
    3. 最后改变你的形象 imageView 在里面 getView 在你的 Adapter 你需要的广告

    希望对你有帮助