I want to use a different layout for data which is contain text only and text + image in Recyclerview.
I have created two layout, layout_post for text only data, and layout_post_with_image for text + image data.
so far my code is working to show of two different layout in RecycleView, but the problem is it only show layout_post_with_image in the first data, layout_post in the second data, and in the third and on its show layout_post_with_image, whatever data i put, the layout shows the same pattern. so anyone could help me?
here the viewholder
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.myapp.R;
import java.awt.font.TextAttribute;
public class PostViewHolder extends RecyclerView.ViewHolder {
public TextView textPost;
public TextView textTime;
public ImageView postImage;
public PostViewHolder(View itemView){
super(itemView);
textPost = (TextView) itemView.findViewById(R.id.post_description);
textTime = (TextView) itemView.findViewById(R.id.post_time);
postImage = (ImageView) itemView.findViewById(R.id.post_image);
}
}
here the model
package com.example.myapp.model;
public class Posts {
public String time, post, uid, image;
public Posts() {
}
public Posts(String time, String post, String uid, String image) {
this.time = time;
this.post = post;
this.uid = uid;
this.image = image;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
and here the homefragment
postListView = (RecyclerView)rootView.findViewById(R.id.all_user_post_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
postListView.setHasFixedSize(true);
postListView.setLayoutManager(linearLayoutManager);
firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(PostsRef, Posts.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, PostViewHolder>(firebaseRecyclerOptions) {
private final static int TEXT_ONLY = 1;
private final static int TEXT_WITH_IMAGE= 0;
public int getItemViewType(int position){
switch (position){
case 1:
return TEXT_ONLY;
default:
return TEXT_WITH_IMAGE;
}
}
@Override
protected void onBindViewHolder(@NonNull PostViewHolder holder, int position, @NonNull Posts model) {
if(holder.postImage !=null){
holder.textTime.setText(model.getTime());
holder.textPost.setText(model.getPost());
Picasso.get().load(model.getImage()).into(holder.postImage);
}else{
holder.textTime.setText(model.getTime());
holder.textPost.setText(model.getPost());
}
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
int layoutRes = 0;
switch (viewType) {
case TEXT_ONLY:
layoutRes = R.layout.layout_posts;
break;
case TEXT_WITH_IMAGE:
layoutRes = R.layout.layout_posts_with_image;
break;
}
View view = LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
return new PostViewHolder(view);
}
};
firebaseRecyclerAdapter.startListening();
postListView.setAdapter(firebaseRecyclerAdapter);
return rootView;
UPDATE : i think rather than use two different layout, its more easier use one layout and set visibility to every element you want to change.