2

I have a list view that has expandable rows. When i click on the row, i inflate a different xml layout which is very similar (item_view(left) and item_view_expanded(right)) and has almost all the same views, but some sub views appear in different sizes (for example the image view is enlarged) and there is one additional button in the expanded view (Save Photo).

Collapsed View Expanded View

Upon converting this file to kotlin and trying to access the views using the kotlin synthetic extensions, i receive the error Overload Resolution Ambiguity. This makes sense because there are indeed two xml files which have the same ids and are both imported in this file. Most stackoverflow posts i see (such as this one) are resolved by removing one of the import statements or changing the ids. But i want the ids to match because i want to have one shared ViewHolder that can bind both of these different views. After all, the text views and images are all the same content, just in a different visual layout.

I could do this in Java with butterknife because i simply annotate the button that doesn't exist in the collapsed layout (download) as nullable and do some null checks in the binding. I'm wondering how i can have two xml layouts use the same binding since almost everything is the same. Can this be done in kotlin using the synthetic extensions?

Here is the java code for my ViewHolder

   public static class ItemViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.tvName) TextView mNameTextView;
    @BindView(R.id.tvRelationship) TextView mRelationshipTextView;
    @BindView(R.id.tvLifespan) TextView mLifespanTextView;
    @BindView(R.id.topDivider) View mTopDividerView;
    @BindView(R.id.bottomDivider) View mBottomDividerView;
    @BindView(R.id.imageView) ImageView mImageView;
    @Nullable
    @BindView(R.id.savePhoto) TextView mSavePhoto;


    public ItemViewHolder(final View view) {
        super(view);
        ButterKnife.bind(this, view);
    }

    private void bindItem(final int position, final PathPerson pathPerson, final PathDetailAdapter pathDetailAdapter) {
        // item
        mNameTextView.setTag(position);

        PathUtilities.loadRoundedLeftCornersImageForGender(pathPerson, mImageView);
        mNameTextView.setText(pathPerson.getFullName());
        itemView.setOnClickListener(v -> {
            pathPerson.toggleExpanded();
            pathDetailAdapter.notifyItemChanged(position);
        });
        if (mSavePhoto != null) {
            mSavePhoto.setOnClickListener(v -> {
                // TODO: 9/7/17 download original
                Toast.makeText(itemView.getContext(), R.string.downloading, Toast.LENGTH_SHORT).show();
            });
        }

        PathUtilities.setLifespan(pathPerson, mLifespanTextView);
        PathUtilities.setRelationshipTextAndColorForGender(pathPerson, mRelationshipTextView);

        if (position == 1) {
            mTopDividerView.setVisibility(View.VISIBLE);
            mBottomDividerView.setVisibility(View.VISIBLE);
        } else if (position == pathDetailAdapter.mPathList.size() - 1) {
            mTopDividerView.setVisibility(View.GONE);
            mBottomDividerView.setVisibility(View.GONE);
        } else {
            mTopDividerView.setVisibility(View.GONE);
            mBottomDividerView.setVisibility(View.VISIBLE);
        }
    }
}
Daniel George
  • 484
  • 1
  • 3
  • 9

1 Answers1

0

Turns out the problem is fixed by removing one of the import statements. In my case I needed to keep the one that had the extra download button (the super-set of the two)

Daniel George
  • 484
  • 1
  • 3
  • 9