0

In my app right now I am working with two lists. One is a List of an Object and the other the a List of Lists of the same object. I have a method in my Activity that transforms a list of Object into a List of Lists of object. And to my adapter I am passing the list of lists through this call:

mAdapter.swap(transformList(pipe.mList));

The method swap is responsible for passing the transformed list to my Adapter:

public void swap(List<List<Feed>> feedList) {

    if (finallist == null) {
      finallist = new ArrayList<>();
    }
    finallist.clear();
    finallist.addAll(feedList);
  }

The transformation checks if there are images in a row and puts all of them inside a single list (I am trying to implement something similar to WhatsApp image grouping). So I have a bunch of messages, they can be text messages, files, images, etc. In case of the last one, I group them in a single list.

Let me give a scenario as an example:

I have four images and 1 text message in my original array. The transformation puts all the four images into a single List of objects and de text message in another list of objects (both of them are inserted in my list of lists).

I thought about two ways to handle this transformation: 1 - do it inside the Adapter and 2 - do it in my Activity and pass the modified list to the Adapter. Each one of this solutions generated a different problem.

By following the steps in 1, I was able to display all of the content almost in the way I wanted. The grouping was working just fine! The problem is that if the original array had a length equals to 30, and the transformed array's length was decreased to 12. The RecyclerView would show all of the remaining 18 items as empty states (so after the transformation it wasn't handling the removing items properly).

By following the steps in 2, I was not able to display all of the content. Just the first element of my array. And I would get a IndexOutOfBoundsException in RecyclerView happens java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder error message. But I was not able to identify the problem. I checked many questions here and none of them helped me.

This is my Adapter class:

public class FeedAdapter extends BaseSkeletonAdapter<Feed> implements FeedHolder.FeedHolderListener{
  private static final int HOLDER_COMMENT = 1;
  private static final int HOLDER_IMAGE = 2;
  private static final int HOLDER_FILE = 3;
  private static final int HOLDER_AUDIO = 4;
  private static final int HOLDER_MARKER = 5;
  private static final int HOLDER_EMPTY = 6;
  private static final int HOLDER_GROUP = 7;

  private final FeedItemListener mListener;
  private final int mAvatarSize;
  private final String mUserId;
  private final int mPictureSize;
  private final int mSkeletonColor;
  public static List<List<Feed>> finallist;

  public FeedAdapter(FeedItemListener listener, String userId, int avatarSize, int pictureSize, int skeletonColor) {
    super(2);
    mListener = listener;
    mUserId = userId;
    mAvatarSize = avatarSize;
    mPictureSize = pictureSize;
    mSkeletonColor = skeletonColor;
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType){
      case HOLDER_COMMENT:
      case HOLDER_IMAGE:
      case HOLDER_FILE:
      case HOLDER_MARKER:
      case HOLDER_AUDIO:
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_feed, parent, false);
        return new FeedHolder(view, this, mPictureSize);
      case HOLDER_GROUP:
        System.out.println("É um grupo!!!");
        View viewGroup = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_feed_group,parent,false);
        return new FeedHolder(viewGroup, this, mPictureSize);
      case HOLDER_EMPTY:
      default:
        View empty = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_empty, parent, false);
        return new EmptyPlaceholderViewHolder(empty, R.string.placeholder_feed_empty_title, R.string.placeholder_feed_empty_description, R.drawable.ic_feed_placeholder);
    }
  }

  @Override
  protected void onBind(RecyclerView.ViewHolder holder, int position) {

    if(!(holder instanceof EmptyPlaceholderViewHolder)){
      //Feed feed = finallist.get(position).get(0);

      if (holder instanceof FeedHolder) {
        if (position < finallist.size()) {
          if (mUserId.equals(finallist.get(position).get(0).getCreatedById())) {
            ((FeedHolder) holder).onBind(finallist.get(position), mUserId, mAvatarSize);
          } else {
            ((FeedHolder) holder).onBind(finallist.get(position), mUserId, mAvatarSize);
          }
        }
      }
    }
  }

  @Override
  protected void onBind(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {

    if (payloads.isEmpty()) {
      onBindViewHolder(holder, position);
    }else {

      if (holder instanceof FeedHolder) {
        ((FeedHolder) holder).onBind(finallist.get(position), payloads, mUserId, mAvatarSize);
      }
    }
  }

  @Override
  protected void setHolderSkeleton(RecyclerView.ViewHolder holder) {
    if (holder instanceof FeedHolder) {
      ((FeedHolder) holder).setHolderSkeleton(R.drawable.rounded_skeleton, mSkeletonColor);
    }
  }

  @Override
  protected void clearHolderSkeleton(RecyclerView.ViewHolder holder) {
    if (holder instanceof FeedHolder) {
      ((FeedHolder) holder).clearHolderSkeleton();
    }
  }

  @Override
  public int getItemViewType(int position) {
    if(mSkeletonMode){
      return HOLDER_COMMENT;
    } if (finallist != null && finallist.size() > 0 && position >= 0 && position < finallist.size()) {
      System.out.println("Tamanho total: " + finallist.size());
      if (finallist.get(position).size() > 1) {
        System.out.println("Tamanho do grupo: " + finallist.get(position).size());
        return HOLDER_GROUP;
      } else {
      Feed feed = finallist.get(position).get(0);
      if (feed != null) {
          String type = feed.getFeedType();
          if (type != null) {
            switch (type) {
              case FEED_IMAGE:
                return HOLDER_IMAGE;
              case FEED_AUDIO:
                return HOLDER_AUDIO;
              case FEED_FILE:
                return HOLDER_FILE;
              case FEED_MARKER:
                return HOLDER_MARKER;
              case FEED_COMMENT:
              default:
                return HOLDER_COMMENT;
            }
          }
        }
      }
      return HOLDER_COMMENT;
    }else {
      System.out.println("Tá vazia!");
      return HOLDER_EMPTY;
    }
  }

  public List<Feed> getItems() {
    return returnList(finallist);
  }

  public List<List<Feed>> getListItems() {
    return finallist;
  }

  public void swap(List<List<Feed>> feedList) {

    if (finallist == null) {
      finallist = new ArrayList<>();
    }
    finallist.clear();
    finallist.addAll(feedList);
  }

  @Override
  public void toggleLike(final int pos){
    if(mListener != null && pos >= 0 && pos < finallist.size()){
      mListener.toggleLike(finallist.get(pos).get(0));
    }
  }

  @Override
  public void onLongClick(final int pos, final View v) {
    if (mListener != null && pos >= 0 && pos < finallist.size()) {
      mListener.onLongClick(finallist.get(pos).get(0), v);
    }
  }

  @Override
  public int onAudioActionClicked(final int pos, final int progress) {
    if (mListener != null) {
      return mListener.onAudioActionClicked(pos, finallist.get(pos).get(0), progress);
    }else {
      return 0;
    }
  }

  @Override
  public void onClick(int pos) {
    if (finallist!=null && pos >= 0 && pos<finallist.size()) {
      Feed feed = finallist.get(pos).get(0);
      if (feed != null && mListener != null) {
        mListener.onClick(feed);
      }
    }
  }

  public interface FeedItemListener {
    void toggleLike(@NonNull Feed feed);
    void onLongClick(@NonNull Feed feed, @NonNull View v);
    void onClick(@NonNull Feed feed);
    int onAudioActionClicked(int pos, @NonNull Feed feed, final int progress);
  }

  private void transformList(List<Feed> mItems) {
    finallist = new ArrayList<>();
    for (int i = 0; i< mItems.size();i++) {
      List<Feed> feed = new ArrayList<>();
      feed.add(mItems.get(i));
      finallist.add(feed);
    }

    int j = 0;
    List<String> list = new ArrayList<>();
    List<Integer> indexList = new ArrayList<>();
    //System.out.println("Tamanho: " + mItems.size());
    for (int i = 0; i < mItems.size(); i++) {

      if (!mItems.get(i).getFeedType().equals("filePicture")) {
        if (j >= 4) {
          String temp = "";
          for (int k = 0; k < j; k++) {
            temp = temp +  "->" + Integer.toString(i - (k+1));
            if (k != 0) {
              finallist.get(i - 1).add(finallist.get(i - (k + 1)).get(0));
              indexList.add(i - (k+1));
            }

          }
          list.add(temp);
        }
        j = 0;
      } else {
        j = j + 1;
      }
      if (i == mItems.size()-1) {
        //System.out.println("Imagem por ultimo!");
        if (j >= 4) {
          //System.out.println("Grupo vem por ultimo!");
          String temp = "";
          for (int k = 0; k < j; k++) {
            temp = temp +  "->" + Integer.toString(i - (k));
            if (k != 0) {
              finallist.get(i).add(finallist.get(i - (k)).get(0));
              indexList.add(i - (k));
            }
          }
          list.add(temp);
        }
      }
    }
    Collections.sort(indexList);
    int aux = 0;
    for (int i = 0; i < indexList.size();i++) {
      //System.out.println("Valor da posição:  " + indexList.get(i)+ "\nTipo: "+ finallist.get((indexList.get(i).intValue())+aux).get(0).getFeedType()
      //        +"\nValor da posição + i: " + (indexList.get(i)+aux) + "\nAux: " + aux);
      finallist.remove((indexList.get(i).intValue())+aux);
      //notifyItemRangeRemoved(0, finallist.size());
      //notifyDataSetChanged();
      aux = aux - 1;
    }

    /*for (int i = 0; i< finallist.size(); i++){
      if (finallist.get(i).size() > 1) {
        System.out.println("groupImage: " + finallist.get(i).size());
      } else {
        System.out.println(finallist.get(i).get(0).getFeedType());
      }
    }*/
    //returnList(finallist);
    notifyItemRangeRemoved(0, returnList(finallist).size() - finallist.size() - 1);
    //notifyItemRangeInserted(0, finallist.size());
  }

  public List<Feed> returnList(List<List<Feed>> lists) {
    List<Feed> list = new ArrayList<>();
    if (lists != null) {
      for (int i = 0; i < lists.size(); i++) {
        if (lists.get(i).size() > 1) {
          for (int j = 0; j < lists.get(i).size(); j++) {
            list.add(lists.get(i).get(j));
          }
        } else {
          list.add(lists.get(i).get(0));
        }
      }
      System.out.println("Tamanho de list: " + list.size());
    }

    return list;
  }

}

And this is my Activity:

public abstract class FeedActivity extends UltraBaseActivity implements FeedAdapter.FeedItemListener, AudioManager.OnAudioFocusChangeListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
  private static final String EXTRA_PROJECT_ID = "extra_project_id";
  private static final String EXTRA_PROJECT_NAME = "extra_project_name";
  private static final String EXTRA_RESOURCE_ID = "extra_resource_id";
  private static final String EXTRA_RESOURCE_NAME = "extra_resource_name";
  private static final String EXTRA_RESOURCE_KIND = "extra_resource_kind";

  @BindView(R.id.swipeLayout) SwipeRefreshLayout mRefreshLayout;
  @BindView(R.id.recyclerView) RecyclerView mRecyclerView;
  @BindView(R.id.feedCreateFragment) View mFeedCreateLayout;
  @BindView(R.id.mic) ImageView mMicView;

  private WeakReference<FeedCreateFragment> mFeedCreateFragment;
  protected FeedViewModel mViewModel;
  protected FeedAdapter mAdapter;
  private Feed mLongClick;
  private Toolbar mToolbar;
  protected int mScrollTo = -1;
  protected WrapContentLinearLayoutManager mLayoutManager;

  private String mPlayingFeedId;
  private int mPlayingPos;
  private int mActionResourcePause = R.drawable.ic_pause_black_24dp;
  private int mActionResourcePlay = R.drawable.ic_play_black_24dp;
  private MediaPlayer mPlayer;
  private AudioManager mAudioManager;
  protected Handler mAudioHandler;
  protected Runnable mUpdateAudioHolderRunnable = new Runnable() {
    @Override
    public void run() {
      try {
        if (mPlayer != null && mPlayer.isPlaying()) {
          notifyAdapterAudioUpdate(mPlayer.getCurrentPosition(), mActionResourcePause);
          mAudioHandler.postDelayed(this, 100);
        } else {
          mAudioHandler.removeCallbacks(mUpdateAudioHolderRunnable);
        }
      } catch (IllegalStateException e){
        MyLog.e(TAG, "Error while updating seed bar", e);
        mAudioHandler.removeCallbacks(mUpdateAudioHolderRunnable);
      }
    }
  };

  public static void start(@NonNull Context context, @NonNull String projectId, @NonNull  String projectName, @NonNull String resourceId, @NonNull String resourceName, @NonNull String resourceKind){
    Intent intent = setIntent(context, projectId, projectName, resourceId, resourceName, resourceKind);
    if (intent == null) return;
    context.startActivity(intent);
  }

  public static void startForResult(@NonNull Fragment fragment, @NonNull String projectId, @NonNull  String projectName, @NonNull String resourceId, @NonNull String resourceName, @NonNull String resourceKind){
    Intent intent = setIntent(fragment.getContext(), projectId, projectName, resourceId, resourceName, resourceKind);
    if (intent == null) return;
    fragment.startActivityForResult(intent, Constants.Intents.INTENT_REQUEST_VIEW_FEED);
  }

  @Nullable
  protected static Intent setIntent(@NonNull Context context, @NonNull String projectId, @NonNull String projectName, @NonNull String resourceId, @NonNull String resourceName, @NonNull String resourceKind) {
    Intent intent;
    if (resourceKind.equals(Task.ROUTE)) {
      intent = new Intent(context, FeedTaskActivity.class);
    }else if(resourceKind.equals(Chat.ROUTE)){
      intent = new Intent(context, FeedChatActivity.class);
    } else {
      MyLog.e(TAG, "Error invalid resource Kind - " + resourceKind);
      return null;
    }
    intent.putExtra(EXTRA_PROJECT_ID, projectId);
    intent.putExtra(EXTRA_PROJECT_NAME, projectName);
    intent.putExtra(EXTRA_RESOURCE_ID, resourceId);
    intent.putExtra(EXTRA_RESOURCE_NAME, resourceName);
    intent.putExtra(EXTRA_RESOURCE_KIND, resourceKind);
    return intent;
  }

  public FeedActivity() {
    super(R.layout.activity_feed);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    isLogged();
    init(getIntent(), savedInstanceState);
    super.onCreate(savedInstanceState);
    initAdapter();

    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    mAudioHandler = new Handler();

    mViewModel.subscribe()
        .compose(this.<Resource<List<Feed>>>bindUntilEvent(ActivityEvent.DESTROY))
        .flatMap(flatMapDiffUtils())
        .subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(onNext(), onError(), onCompleted());
  }

  @NonNull
  private Func1<Resource<List<Feed>>, Observable<FeedViewModel.Pipe>> flatMapDiffUtils() {
    return new Func1<Resource<List<Feed>>, Observable<FeedViewModel.Pipe>>() {
      @Override
      public Observable<FeedViewModel.Pipe> call(Resource<List<Feed>> r) {
        if (mViewModel.hasResource()) {
          List<Feed> old = mAdapter.getItems();
          MyLog.i(TAG, "New length: " + (r.data != null ? r.data.size() : 0) + " - Old: " + old.size());
          final FeedDiffCallback cb = new FeedDiffCallback(old, r.data, mUser.getId());
          final DiffUtil.DiffResult result = DiffUtil.calculateDiff(cb);
          return Observable.just(new FeedViewModel.Pipe(r.data, result, r.status == Status.LOADING && (r.data ==null || r.data.size() == 0)));
        } else {
          MyLog.i(TAG, "Loading resource from server");
          return Observable.empty();
        }
      }
    };
  }

  private Action1<? super FeedViewModel.Pipe> onNext() {
    return new Action1<FeedViewModel.Pipe>() {
      @Override
      public void call(FeedViewModel.Pipe pipe) {
        if (pipe != null) {
          initFeedFragment();

          mRefreshLayout.setRefreshing(false);
          pipe.mDiffResult.dispatchUpdatesTo(mAdapter);
          mAdapter.setSkeletonMode(pipe.mSkeletonMode);
          //List<List<Feed>> list = new ArrayList<>();
          //list = tranformList(pipe.mList);
          mAdapter.swap(transformList(pipe.mList));
          System.out.println("Tamanho desse troço: " + transformList(pipe.mList).size());
          //mAdapter.notifyDataSetChanged();
          //mAdapter.notifyItemRangeRemoved(0, pipe.mList.size());

          if (mScrollTo == -1) {
            mRecyclerView.scrollToPosition(mAdapter.getItemCount()-1);
          }else {
            mRecyclerView.scrollToPosition(mScrollTo);
          }

          updateMenuOptions();
          showLoading(false);
        }
      }
    };
  }

  protected Action0 onCompleted() {
    return new Action0() {
      @Override
      public void call() {
        MyLog.i(TAG, "Finishing feed activity");
        finish();
      }
    };
  }

  protected Action1<Throwable> onError() {
    return new Action1<Throwable>() {
      @Override
      public void call(Throwable throwable) {
        MyLog.e(TAG, "Error", throwable);
      }
    };
  }

  @Override
  protected void initToolbar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    if(mToolbar !=null) {
      if (mViewModel != null) {
        mToolbar.setTitle(mViewModel.getProjectName());
        mToolbar.setSubtitle(mViewModel.getResourceName());
      }
      mToolbar.setSubtitleTextColor(ContextCompat.getColor(this, R.color.palette_black));
      mToolbar.setNavigationIcon(R.drawable.ic_action_back_black);
    }
    setSupportActionBar(mToolbar);

    if (mToolbar != null) {
      mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          onBackPressed();
        }
      });
    }
  }

  protected void updateTitle(){
    if(mToolbar !=null && mViewModel != null) {
      mToolbar.setTitle(mViewModel.getProjectName());
      mToolbar.setSubtitle(mViewModel.getResourceName());
    }
  }

  @Override
  protected void userUpdated(User user) { }

  private void init(Intent i, Bundle b){
    if (i != null) {
      initViewModel(
          i.getStringExtra(EXTRA_PROJECT_ID),
          i.getStringExtra(EXTRA_PROJECT_NAME),
          i.getStringExtra(EXTRA_RESOURCE_ID),
          i.getStringExtra(EXTRA_RESOURCE_NAME),
          i.getStringExtra(EXTRA_RESOURCE_KIND));
    }else if(b != null){
      initViewModel(
          b.getString(EXTRA_PROJECT_ID),
          b.getString(EXTRA_PROJECT_NAME),
          b.getString(EXTRA_RESOURCE_ID),
          b.getString(EXTRA_RESOURCE_NAME),
          b.getString(EXTRA_RESOURCE_KIND));
    }else {
      MyLog.i(TAG, "Error while initializing view model");
      finish();
    }
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(EXTRA_PROJECT_ID, mViewModel.getProjectId());
    outState.putString(EXTRA_PROJECT_NAME, mViewModel.getProjectName());
    outState.putString(EXTRA_RESOURCE_ID, mViewModel.getResourceId());
    outState.putString(EXTRA_RESOURCE_NAME, mViewModel.getResourceName());
    outState.putString(EXTRA_RESOURCE_KIND, mViewModel.getResourceKind());
  }

  private void initAdapter(){
    mAdapter = new FeedAdapter(
        this,
        mUser.getId(),
        getResources().getDimensionPixelSize(R.dimen.task_avatar_size),
        (int) (AndroidUtils.getScreenWidth(this) * 0.6),
        ContextCompat.getColor(this, R.color.bg_skeleton)
    );
    mRefreshLayout.setColorSchemeResources(R.color.yellow, android.R.color.darker_gray, R.color.yellow_edit_note, android.R.color.background_dark);
    mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
      @Override
      public void onRefresh() {
        mScrollTo = -1;
        mViewModel.reload();
      }
    });
    mLayoutManager = new WrapContentLinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);
  }

  private void initFeedFragment(){
    if(!(mFeedCreateFragment != null && mFeedCreateFragment.get() != null && getSupportFragmentManager().findFragmentById(R.id.feedCreateFragment) instanceof FeedCreateFragment)){
      mFeedCreateFragment = new WeakReference<>(FeedCreateFragment.newInstance(mViewModel.getProjectId(), mViewModel.getResourceId(), mViewModel.getResourceKind(), mViewModel.getResourceUsers()));
      getSupportFragmentManager()
          .beginTransaction()
          .add(R.id.feedCreateFragment, mFeedCreateFragment.get())
          .commitAllowingStateLoss();

      if (mViewModel.canCreateFeed()) {
        mFeedCreateLayout.setVisibility(View.VISIBLE);
      }else {
        mFeedCreateLayout.setVisibility(View.GONE);
      }
    }
  }

  protected abstract void initViewModel(String projectId, String projectName, String resourceId, String resourceName, String resourceKind);
  protected abstract void updateMenuOptions();

}

This is part of the Error (the text reached the maximum size):

10-18 17:29:14.702 23722-23722/com.construct.test E/WrapContentLinearLayoutManager: IndexOutOfBoundsException in RecyclerView happens
                                                                                    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{615b449 position=3 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent}
                                                                                        at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5297)
                                                                                        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5479)
                                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                                        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                                        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                                        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                                        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                                        at com.construct.v2.adapters.WrapContentLinearLayoutManager.onLayoutChildren(WrapContentLinearLayoutManager.java:20)
halfer
  • 19,824
  • 17
  • 99
  • 186
Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50

1 Answers1

0

My problem was caused by the BaseSkeletonAdapter<Feed> that my FeedAdapter was extending. BaseSkeletonAdapter was using the original list and not the List of Lists I had to use. So I created a new class SkeletonAdapterFeed that is basically equal to the previous one, but the new one is receiving a List of Lists instead of just the List. I know that it sounds a little confusing. I don't have full understanding of the project I am working right now so that's why I don't know everything about the classes, etc.

Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50