Home » Android » java – Get item<class> from recyclerview onClick?-Exceptionshub

java – Get item<class> from recyclerview onClick?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

Here i setting with pagination.
First i have arraylist contains 5 elements.

At 0...value_1,
At 1...value_2,
At 2...value_3,
At 3...value_4,
At 4...value_5,

at end of page i add more items and call

mAdapter.notifyDataSetChanged();

But by the use of this function , recyclerview scroll down to immediately.
So avoiding scrolling down i used

mAdapter.notifyItemRangeInserted(0,newList.size()-1);

This works perfectly, but whenever i click on itemview i got this position.

Click At value_1, 0
Click At value_2, 1
Click At value_3, 2
Click At value_4, 3
Click At value_5, 4

Click At value_6, 0
Click At value_7, 1
Click At value_8, 2
Click At value_9, 3
Click At value_10 4

So how to rearrange position from 0 to 9 simultaneously from value_1 to value_10 ?

In Adapter

private OnButtonClickListener onButtonClickListener= null;

public interface OnButtonClickListener {
        void onButtonClick(Post post, int i);

    }
    public void setOnClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener= onButtonClickListener;
    }
    postHolder.Button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    if (onButtonClickListener==null)return;
                    onButtonClickListener.onButtonClick(mPost.get(i),i);

                }
            });

In Activity

 mAdapter.setOnClickListener(new PostAdapter.OnButtonClickListener () {
            @Override
            public void onButtonClick(Post post, int i)
            {
                String key=post.getPostid();
                Log.i("positionpost",String.valueOf(i));

        });
How to&Answers: