增量计数器,在recyclerview中单击按钮 [英] increment counter which click of button in recyclerview

查看:71
本文介绍了增量计数器,在recyclerview中单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用,该应用使用齐射来获取数据,并使用recyclerview在供稿中显示.我在每张卡中都有一个按钮,当单击该按钮时,将计算点击次数并将其显示在该特定卡的textview中.它工作正常,但是当我从卡上滚动到另一张卡,然后再滚动回到上一张卡(即,该卡已重新加载)时,单击按钮时,点击计数从零开始,而不是从其具有的值开始继续重新加载之前.我究竟做错了什么?这是我的代码

I am working on app that fetches data using volley and displays in feed using recyclerview.i have a button in each card that when clicked, will count the number of clicks and display it in a textview in that particular card. It works perfectly but when i scroll away from the card to another card and then scroll back to the previous card (ie. the card is reloaded,) the click count starts from zero upon clicking the button instead of continuing from the value that it had before reloading. What am I doing wrong? this is my code

package net.simplifiedcoding.myfeed;
import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Belal on 11/9/2015.
 */

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {


    //Imageloader to load image
    private ImageLoader imageLoader;
    private Context context;

    //List to store all superheroes
    List<SuperHero> superHeroes;

    //Constructor of this class
    public CardAdapter(List<SuperHero> superHeroes, Context context){
        super();
        //Getting all superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.superheroes_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;

    }



    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {



        //Getting the particular item from the list
        SuperHero superHero =  superHeroes.get(position);

        //Loading image from url
        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.image, android.R.drawable.ic_dialog_alert));

        //Showing data on the views
        holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
        holder.textViewName.setText(superHero.getName());
        holder.textViewPublisher.setText(superHero.getPublisher());
        holder.textViewLikes.setText(superHero.getLikes());
        //increment counter and display in textview when button is clicked
        holder.custom_button.setOnClickListener(new View.OnClickListener() {

            int count = 0 ;
            @Override
            public void onClick(View v) {
                count ++;
                holder.txtCount.setText(String.valueOf(count));

                }

            });
    }



    @Override
    public int getItemCount() {
        return superHeroes.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        //Views
        public NetworkImageView imageView;
        public TextView textViewName;
        public TextView textViewPublisher;
        public TextView textViewLikes;
        public TextView txtCount;
        public ImageButton custom_button;

        //Initializing Views
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
            textViewLikes = (TextView) itemView.findViewById(R.id.textViewlikes);
            txtCount = (TextView)itemView.findViewById(R.id.txtCount);
            custom_button = (ImageButton) itemView.findViewById(R.id.custom_button);



        }


    }

}

推荐答案

在重用android视图中,您需要为设置为零的超级英雄添加一个私有变量计数,并为每个按钮单击添加增量计数并显示

In android views are reused, you need to add a private variable count for superheroes set to zero and for each button click, increment count and display it

holder.custom_button.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            superHero.get(position).setCount(superHero.get(position).getCount+1)
            holder.txtCount.setText(superHero.get(position).getCount());

            }

        });
holder.txtCount.setText(superHero.get(position).getCount());

这篇关于增量计数器,在recyclerview中单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆