0

I am trying not to use ListView again and I am trying to get familiar with RecyclerView. I have no problem with using RecyclerView until I try to use HORIZONTAL RecyclerView inside VERTICAL RecyclerView and I made a search and found answer here, so thank you. I also hope to find answer to my question.

I am making a VERTICAL RecyclerView with CardView as an item with OnClickListener to deal with ViewHolder clicks, I have no problem on that. The problem come when trying to make a share button on the CardView once clicked a balloon appears using GreenDroid Library with three choices Facebook, Twitter and Google+ when on of those is clicked I ask for the position of clicked view using getAdapterPosition and it returns wrong value all the time. This is my code ..

 public class ViewHolder extends RecyclerView.ViewHolder implements
        OnClickListener {

    private ImageView imgViewItem;
    private TextView textViewTitle;
    private ImageView imgViewShare;

    public ViewHolder(View view) {
        super(view);

        imgViewItem = (ImageView) view
                .findViewById(R.id.img_view_item_no_description);
        imgViewItem.setAdjustViewBounds(true);
        textViewTitle = (TextView) view
                .findViewById(R.id.text_view_title_no_description);

        prepareQuickActionGrid();
        imgViewShare = (ImageView) view
                .findViewById(R.id.img_view_share_no_description);

        imgViewShare.setOnClickListener(this);

        view.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {

        if (v.equals(imgViewShare)) {
            grid.show(v);
        } else {

            Intent intent = new Intent(context,
                    McDonaldsPagerActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Constants.TITLES_TAG, titles);
            intent.putExtra(Constants.IMAGES_TAG, imgs);
            intent.putExtra(Constants.ID_TAG, id);
            context.startActivity(intent);
        }

    }

and prepareQuickActionGrid() ..

private void prepareQuickActionGrid() {
        grid = new QuickActionGrid(context);
        grid.addQuickAction(new MyQuickAction(context,
                R.drawable.facebook_50x50, R.string.facebook));
        grid.addQuickAction(new MyQuickAction(context,
                R.drawable.twitter_50x50, R.string.twitter));
        grid.addQuickAction(new MyQuickAction(context,
                R.drawable.g_plus_50x50, R.string.g_plus));
        grid.setOnQuickActionClickListener(new OnQuickActionClickListener() {
            @Override
            public void onQuickActionClicked(QuickActionWidget widget,
                    int gridPosition) {
                // the problem is here .. wrong value is returned all the time
                int position = getLayoutPosition();
                Log.d("position", "" + position);
                // wrong position results in wrong title and wrong image
                switch (gridPosition) {
                case 0:
                    share(Constants.FACEBOOK, imgs[position],
                            titles[position]);
                    break;
                case 1:
                    share(Constants.TWITTER, imgs[position],
                            titles[position]);
                    break;
                case 2:
                    share(Constants.G_PLUS, imgs[position],
                            titles[position]);
                    break;
                }
            }
        });
}

I try to use getAdapterPosition(), but it also returns wrong value. What's wrong in my code. Any suggestions to overcome this problem.

Ahmed Mostafa
  • 918
  • 1
  • 12
  • 20

1 Answers1

-1

A quick way to fix this problem would be to put the code inside the onBindViewHolder method where you have access to the position.

pgiitu
  • 1,671
  • 1
  • 15
  • 23
  • I try it, but still giving wrong value and I don't know why.Thank you. – Ahmed Mostafa Oct 23 '15 at 11:22
  • 1
    `onBindViewHolder` used to fill the `RecyclerView` items with data, the `position` in this method will be the position of the last filled item not of the clicked item. – Ahmed Mostafa Oct 23 '15 at 11:36