Hi, I'm using the SlideInBottomAnimationAdapter in my Adapter as it is in the documentation:
vRecyclerView.setAdapter(new SlideInBottomAnimationAdapter(new AlphaInAnimationAdapter(getAdapter())));
But im getting type = 0 in the method onCreateViewHolder
from my own adapter:
@Override
public PublicationViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
View v;
switch (type) {
case USER_PUBLICATION_TYPE:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_no_images, null);
return new UserPublicationViewHolder(v);
case USER_PUBLICATION_TYPE_W_1_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_1_img, null);
return new UserPublicationSingleImageViewHolder(v);
case USER_PUBLICATION_TYPE_W_2_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_2_img, null);
return new UserPublicationTwoImagesViewHolder(v);
case USER_PUBLICATION_TYPE_W_3_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_3_img, null);
return new UserPublicationThreeImagesViewHolder(v);
case USER_PUBLICATION_TYPE_W_4_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_4_img, null);
return new UserPublicationFourImagesViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_no_images, null);
return new SocialNetworkPublicationViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE_W_1_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_1_img, null);
return new SocialNetworkPublicationSingleImageViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE_W_2_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_2_img, null);
return new SocialNetworkPublicationTwoImagesViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE_W_3_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_3_img, null);
return new SocialNetworkPublicationThreeImagesViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE_W_4_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_4_img, null);
return new SocialNetworkPublicationFourImagesViewHolder(v);
case EVENT_PUBLICATION_TYPE:
v = LayoutInflater.from(getActivity()).inflate(R.layout.view_holder_event_publication, null);
return new EventPublicationViewHolder(v);
case PLACE_PUBLICATION_TYPE:
v = LayoutInflater.from(getActivity()).inflate(R.layout.view_holder_place_publication, null);
return new PlacePublicationViewHolder(v);
}
throw new RuntimeException("No type matched! Type="+type);
}
I have several views as you can see but I didn't think that this will be a problem, is theres some work around?
I show you the full adapter but theres nothing from another world. Maybe I need to call some super? But the onCreateViewHolder method is abstract so I know what should I do there.
public class PublicationAdapter extends RecyclerView.Adapter<PublicationViewHolder> {
public static final int USER_PUBLICATION_TYPE = 1;
public static final int EVENT_PUBLICATION_TYPE = 2;
public static final int PLACE_PUBLICATION_TYPE = 7;
private static final int USER_PUBLICATION_TYPE_W_1_IMG = 3;
private static final int USER_PUBLICATION_TYPE_W_2_IMG = 4;
private static final int USER_PUBLICATION_TYPE_W_3_IMG = 5;
private static final int USER_PUBLICATION_TYPE_W_4_IMG = 6;
public static final int SOCIALNETWORK_PUBLICATION_TYPE = 8;
private static final int SOCIALNETWORK_PUBLICATION_TYPE_W_1_IMG = 9;
private static final int SOCIALNETWORK_PUBLICATION_TYPE_W_2_IMG = 10;
private static final int SOCIALNETWORK_PUBLICATION_TYPE_W_3_IMG = 11;
private static final int SOCIALNETWORK_PUBLICATION_TYPE_W_4_IMG = 12;
private ArrayList<Publication> publications = new ArrayList<Publication>();
public ArrayList<Publication> getPublications() {
return publications;
}
public void setPublications(ArrayList<Publication> publications) {
this.publications = publications;
}
@Override
public int getItemViewType(int position) {
Publication publication = publications.get(position);
if (publication instanceof UserPublication) {
int length = publication.getMedias().size();
switch (length) {
case 1:
return USER_PUBLICATION_TYPE_W_1_IMG;
case 2:
return USER_PUBLICATION_TYPE_W_2_IMG;
case 3:
return USER_PUBLICATION_TYPE_W_3_IMG;
case 4:
return USER_PUBLICATION_TYPE_W_4_IMG;
default:
if (length > 4) {
return USER_PUBLICATION_TYPE_W_4_IMG;
}
break;
}
return USER_PUBLICATION_TYPE;
}
if (publication instanceof SocialNetworkPublication) {
int length = publication.getMedias().size();
switch (length) {
case 1:
return SOCIALNETWORK_PUBLICATION_TYPE_W_1_IMG;
case 2:
return SOCIALNETWORK_PUBLICATION_TYPE_W_2_IMG;
case 3:
return SOCIALNETWORK_PUBLICATION_TYPE_W_3_IMG;
case 4:
return SOCIALNETWORK_PUBLICATION_TYPE_W_4_IMG;
default:
if (length > 4) {
return SOCIALNETWORK_PUBLICATION_TYPE_W_4_IMG;
}
break;
}
return SOCIALNETWORK_PUBLICATION_TYPE;
}
if (publication instanceof EventPublication) {
return EVENT_PUBLICATION_TYPE;
}
if(publication instanceof PlacePublication){
return PLACE_PUBLICATION_TYPE;
}
throw new RuntimeException("Unknown view type in PublicationAdapter");
}
@Override
public PublicationViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
View v;
switch (type) {
case USER_PUBLICATION_TYPE:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_no_images, null);
return new UserPublicationViewHolder(v);
case USER_PUBLICATION_TYPE_W_1_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_1_img, null);
return new UserPublicationSingleImageViewHolder(v);
case USER_PUBLICATION_TYPE_W_2_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_2_img, null);
return new UserPublicationTwoImagesViewHolder(v);
case USER_PUBLICATION_TYPE_W_3_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_3_img, null);
return new UserPublicationThreeImagesViewHolder(v);
case USER_PUBLICATION_TYPE_W_4_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_4_img, null);
return new UserPublicationFourImagesViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_no_images, null);
return new SocialNetworkPublicationViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE_W_1_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_1_img, null);
return new SocialNetworkPublicationSingleImageViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE_W_2_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_2_img, null);
return new SocialNetworkPublicationTwoImagesViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE_W_3_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_3_img, null);
return new SocialNetworkPublicationThreeImagesViewHolder(v);
case SOCIALNETWORK_PUBLICATION_TYPE_W_4_IMG:
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_user_publication_w_4_img, null);
return new SocialNetworkPublicationFourImagesViewHolder(v);
case EVENT_PUBLICATION_TYPE:
v = LayoutInflater.from(getActivity()).inflate(R.layout.view_holder_event_publication, null);
return new EventPublicationViewHolder(v);
case PLACE_PUBLICATION_TYPE:
v = LayoutInflater.from(getActivity()).inflate(R.layout.view_holder_place_publication, null);
return new PlacePublicationViewHolder(v);
}
throw new RuntimeException("No type matched! Type="+type);
}
private static final String INTERACTION_LIKE = "LIKE";
private static final String INTERACTION_DISLIKE = "DISLIKE";
private static final String INTERACTION_LOVE = "LOVE";
private static final String INTERACTION_NONE = "";
@Override
public void onBindViewHolder(final PublicationViewHolder aPublicationHolder, int i) {
switch (publications.get(i).getType()) {
case Publication.TYPE_SOCIAL_NETWORK_PUBLICATION:
bindSocialNetworkPublicationCallbacks(aPublicationHolder, i);
aPublicationHolder.load(publications.get(i), FeedFragment.this);
break;
case Publication.TYPE_USER_PUBLICATION:
bindUserPublicationCallbacks(aPublicationHolder, i);
aPublicationHolder.load(publications.get(i), FeedFragment.this);
break;
case Publication.TYPE_EVENT_PUBLICATION:
bindEventPublicationCallbacks(aPublicationHolder, i);
aPublicationHolder.load(publications.get(i), FeedFragment.this);
break;
case Publication.TYPE_PLACE_PUBLICATION:
bindPlacePublicationCallbacks(aPublicationHolder, i);
aPublicationHolder.load(publications.get(i), FeedFragment.this);
break;
}
if (i == 0) {
onFirstViewHolderBinded(aPublicationHolder);
}
}
private void bindPlacePublicationCallbacks(PublicationViewHolder aPublicationHolder, int i) {
final PlacePublication publication = (PlacePublication) publications.get(i);
aPublicationHolder.load(publication,
new View.OnClickListener() {
@Override
public void onClick(View view) {
onLikeClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onDislikeClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onFavoriteClicked(publication);
}
}, FeedFragment.this);
}
private void bindEventPublicationCallbacks(PublicationViewHolder aPublicationHolder, int i) {
final EventPublication publication = (EventPublication) publications.get(i);
aPublicationHolder.load(publication,
new View.OnClickListener() {
@Override
public void onClick(View view) {
onLikeClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onDislikeClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onFavoriteClicked(publication);
}
}, FeedFragment.this);
}
private void bindUserPublicationCallbacks(PublicationViewHolder aPublicationHolder, int i) {
final UserPublication publication = (UserPublication) publications.get(i);
aPublicationHolder.load(publication,
new View.OnClickListener() {
@Override
public void onClick(View view) {
onLikeClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onDislikeClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onFavoriteClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onEditPostClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onDeletePostClicked(publication);
}
},
new TagCloudLocationFriends.OnTagSelectListener() {
@Override
public void onTagSelected(MinikastTag tag, int position) {
if(tag.getType() == 2){
GoTo.placeDetail(getActivity(), publication.getLocation().getId(), publication.getLocation().getName());
}else if (tag.getType() == 3){
User aUser = new User(-1);
aUser.setProfileId(String.valueOf(tag.getUserID()));
GoTo.user(getActivity(), aUser);
}
}
}, FeedFragment.this);
}
private void bindSocialNetworkPublicationCallbacks(PublicationViewHolder aPublicationHolder, int i) {
final SocialNetworkPublication publication = (SocialNetworkPublication) publications.get(i);
aPublicationHolder.load(publication,
new View.OnClickListener() {
@Override
public void onClick(View view) {
onLikeClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onDislikeClicked(publication);
}
},
new View.OnClickListener() {
@Override
public void onClick(View view) {
onFavoriteClicked(publication);
}
},
new TagCloudLocationFriends.OnTagSelectListener() {
@Override
public void onTagSelected(MinikastTag tag, int position) {
if(tag.getType() == 2){
GoTo.placeDetail(getActivity(), publication.getLocation().getId(), publication.getLocation().getName());
}else if (tag.getType() == 3){
User aUser = new User(-1);
aUser.setProfileId(String.valueOf(tag.getUserID()));
GoTo.user(getActivity(), aUser);
}
}
}, FeedFragment.this);
}
private void onDeletePostClicked(UserPublication publication) {
showDialogDeletePost(publication.getId(), mCurrentPosts.indexOf(publication));
}
private void onEditPostClicked(UserPublication publication) {
GoTo.editPost(getActivity(), publication);
}
private void removeInteraction(Publication publication, String oldInteraction){
RemovePostInteraction operation = new RemovePostInteraction(publication, String.valueOf(publication.getId()), StartupSharedPreferences.getUserId(), oldInteraction);
operation.setmToken(getToken());
sendOperationRequest(operation);
}
private void addInteraction(Publication publication, String newInteraction){
SavePostInteractionOperation operation = new SavePostInteractionOperation(publication, String.valueOf(publication.getId()), StartupSharedPreferences.getUserId(), newInteraction);
operation.setmToken(getToken());
sendOperationRequest(operation);
}
private void onLikeClicked(Publication publication) {
if(publication.getRelationship().equals(INTERACTION_LIKE)){
publication.setRelationship(INTERACTION_NONE);
removeInteraction(publication, INTERACTION_LIKE);
}else{
publication.setRelationship(INTERACTION_LIKE);
addInteraction(publication, INTERACTION_LIKE);
}
}
private void onFavoriteClicked(Publication publication) {
if(publication.getRelationship().equals(INTERACTION_LOVE)){
publication.setRelationship(INTERACTION_NONE);
removeInteraction(publication, INTERACTION_LOVE);
}else{
publication.setRelationship(INTERACTION_LOVE);
addInteraction(publication, INTERACTION_LOVE);
}
}
private void onDislikeClicked(Publication publication) {
if(publication.getRelationship().equals(INTERACTION_DISLIKE)){
publication.setRelationship(INTERACTION_NONE);
removeInteraction(publication, INTERACTION_DISLIKE);
}else{
publication.setRelationship(INTERACTION_DISLIKE);
addInteraction(publication, INTERACTION_DISLIKE);
}
}
@Override
public long getItemId(int position) {
return publications.get(position).getId();
}
@Override
public int getItemCount() {
return publications.size();
}
public void updatePublication(Publication modifP) {
int idx = publications.indexOf(modifP);
publications.set(idx, modifP);
}
}
I also set the hasStableItems to true. I still can call the super.getItemViewType()
but if I do so, It will still crach in the onCreateViewHolder