How to get correct item position after native ads are added to recycler view



In my android app I’m trying to implement mopub native ads. Before using these native ads my app was functioning correctly as expected. Now it gives outofbound exceptions and incorrect items when list item is clicked. I’m using recyclerview and using infinite scrolling, it gets item from my api and displays item correctly, but when clicking on item it gives incorrect item.

Here is my recyclerview adapter below.

public class PostRecycleAdapter extends RecyclerView.Adapter<com.example.adapters.PostRecycleAdapter.PostViewHolder> implements Callback<List<Post>> {

    private Context mContext;
    private List<Post> mPosts;
    // Allows to remember the last item shown on screen
    private int lastPosition = -1;
    //    Pagination
    private Pagination mPagination;

    public PostRecycleAdapter(Context context, List<Post> posts) {
        mContext = context;
        mPosts = posts;

    public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent, false);

        PostViewHolder viewHolder = new PostViewHolder(view);
        return viewHolder;

    public void onBindViewHolder(PostViewHolder holder, int position) {
        // Here you apply the animation when the view is bound
        setAnimation(holder.mPostName, position);

        //check for last item
        if ((position >= getItemCount() - 1)) {
            // Loading next set of list items on scroll
            if (mPagination != null && !mPagination.getOutOfRange() && mPagination.getNextPage() != null){

    public int getItemCount() {
        return mPosts == null ? 0 : mPosts.size();

     * Here is the key method to apply the animation
    private void setAnimation(View viewToAnimate, int position)
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition)
            Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left);
            lastPosition = position;

    public class PostViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView mPostName;
        public ImageView mAnchorImage;

        public PostViewHolder(View itemView) {

            mPostName = (TextView) itemView.findViewById(;
            mAnchorImage = (ImageView) itemView.findViewById(;


        public void bindPost(Post post) {

            String postImage = post.getPostImage();

        public void onClick(View view) {
            int position = getAdapterPosition(); // gets item position
            Post post = mPosts.get(position);
            int postId = post.getId();
            String postName = post.getName();

            Intent intent = new Intent(mContext, InfoActivity.class);
            intent.putExtra(MyAppConstants.POST_ID, postId);
            intent.putExtra(MyAppConstants.POST_NAME, postName);

    public void success(List<Post> posts, Response response) {
        if (mPosts == null) {
            mPosts = posts;
        } else {


        List<Header> headerList = response.getHeaders();
        for(Header header : headerList) {
            if ("X-Pagination".equals(header.getName())) {
                Gson gson = new Gson();
                mPagination = gson.fromJson(header.getValue(), Pagination.class);
        } FeedLoaded(mPagination));

    public void failure(RetrofitError error) {
        Log.d("Call", " : Failed => " + error); ErrorLoadingFeed(error));

    public void clearData() {
        if (mPosts != null) {

    public List<Post> getPosts() {
        return mPosts;

and here is how I’m requesting ads in my app.

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_feed,
                container, false);

        ButterKnife.bind(this, rootView);


        // RecyclerView
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));

        adapter = new PostRecycleAdapter(getActivity(), null);

        // Mopub
        // Create an ad renderer and view binder that describe your native ad layout.
        ViewBinder myViewBinder = new ViewBinder.Builder(R.layout.native_ad_list)

        MoPubNativeAdPositioning.MoPubServerPositioning adPositioning = MoPubNativeAdPositioning.serverPositioning();

        MoPubStaticNativeAdRenderer myRenderer = new MoPubStaticNativeAdRenderer(myViewBinder);

        // Pass the recycler Adapter your original adapter.
        myMoPubAdapter = new MoPubRecyclerAdapter(getActivity(), adapter, adPositioning);


        // Set up the recycler view

        return rootView;

and onResume

    public void onResume() {

        MyApplication.bus.register(this); Runnable() {
            public void run() {

        // Mopub ads

        // Optional targeting parameters
        RequestParameters parameters = new RequestParameters.Builder()
                //.keywords("your target words here")

        // Request ads when the user returns to this activity
        myMoPubAdapter.loadAds(getActivity().getString(R.string.mopub_native_ad_unit_id_feed), parameters);

Please help me solve this issue.



Thank you for reaching out. Do you still continue to see this issue with the latest SDK? If so please let us know so we can debug this issue further