【问题标题】:I am using GridView when I scroll it the state of favourite button is changed to default state我在滚动时使用 GridView,收藏按钮的状态更改为默认状态
【发布时间】:2018-10-01 07:12:53
【问题描述】:

我正在使用 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));
            }
        });
    }
}

【问题讨论】:

  • 它不是recyclerview 正确阅读我的问题
  • 上面的链接只是提示您需要保持收藏按钮的状态
  • 它不是recyclerview,但它们的工作方式类似。为了不使用超过所需的内存,视图被回收。所以你需要在你的getView方法中做的是每次设置按钮的状态,否则会重置。
  • @NileshRathod 它在我传递 GridView 时将 RecyclerView 作为参数,它无法解析视图

标签: java android


【解决方案1】:

我认为你需要保持选中项的位置

  1. 将属性添加到您的Model class,如下所示:
    private int state;
  2. 获取选中项的位置,然后更改state属性的值
  3. 最后在您需要的Adapter 广告中更改imageView 中的getView 图像

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多