【问题标题】:increment counter which click of button in recyclerview在recyclerview中单击按钮的增量计数器
【发布时间】:2016-03-10 13:49:20
【问题描述】:

我正在开发使用 volley 获取数据并使用 recyclerview 在提要中显示的应用程序。我在每张卡片中都有一个按钮,当点击该按钮时,将计算点击次数并将其显示在该特定卡片的文本视图中。它工作得很好,但是当我从卡片滚动到另一张卡片然后滚动回上一张卡片(即重新加载卡片)时,点击计数从零开始点击按钮,而不是从它的值继续在重新加载之前。我究竟做错了什么?这是我的代码

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 android-recyclerview counter


    【解决方案1】:

    在 android 视图被重用时,您需要为超级英雄添加一个私有变量 count 设置为零,并且对于每个按钮单击,递增计数并显示它

    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());
    

    【讨论】:

    • 在添加此代码之前,我是否需要更改我的 getter 和 setter?我收到“get(位置)”错误。 @deepakjohn141
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多