How to Save RecyclerView Buttons Clicked Respectfully - Android

I have a RecyclerView that is filled with CardViews. Each of the CardViews has a button that votes for the message.

This is what the button looks like when not pressed,

up vote - not pressed

This is what the button looks like when clicked,

up vote - pressed

My code works to achieve this, but I have a problem as it is a RecyclerView. When I view the posts, the RecyclerView is recycling the previous posts that were upvoted. Thus, the message will show that it voted even though the user never voted for it.

How can I use the buttons for each CardView with care?

This is my adapter

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

    private String[] mDataset;

    Typeface customFont;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTitle;
        public TextView mVoterCounter;
        public ImageButton mVoterButton;
        public ViewHolder(android.support.v7.widget.CardView v) {
            super(v);
            mTitle = (TextView) v.findViewById(R.id.title);
            mVoterCounter = (TextView) v.findViewById(R.id.voter_counter);

            //Initialize voter button
            mVoterButton = (ImageButton)v.findViewById(R.id.voter);

            mVoterButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mVoterButton.setImageResource(R.drawable.ic_voter_pressed);
                }
            });
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public DiscoverRecyclerAdapter(String[] myDataset, Typeface passedFont) {
        mDataset = myDataset;
        customFont = passedFont;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public DiscoverRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_discover, parent, false);
        // set the view size, margins, paddings and layout parameters
        return new ViewHolder((android.support.v7.widget.CardView)v);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTitle.setText(mDataset[position]);
        holder.mTitle.setTypeface(customFont);
        holder.mVoterCounter.setTypeface(customFont);
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

      

+3


source to share


2 answers


along with mDataset you will also need a boolean array: mIsSelected will now size this equal to the size of the mDataSet array, or create a class if you want.

Then in onBindViewHolder do like

 if(mIsSelected[position]
  mVoterButton.setImageResource(R.drawable.ic_voter_pressed);
else
 mVoterButton.setImageResource(R.drawable.ic_voter_unpressed);

      



and click onclick button inside onBindViewHolder like below

 holder.mVoterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              mVoterButton.setImageResource(R.drawable.ic_voter_pressed);
              mIsSelected[position] = true;
            }
        });

      

+4


source


You need to clear the view of previous rows of all previous data at the start of onBindViewHolder.

In your case, it seems that you need to clear all the visibility settings of the view components to what you think is standard. After that, launch the data card.



Being that your passed in dataset is just a string, you need to either call your API or get the invoice / status. Or change your dataset to your own array of objects that tracks all the components needed to customize and write data for each card.

In short: as species come back, you need to clean them up before reusing them.

0


source







All Articles