This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.BindingHolder> { | |
private List<Post> mPosts; | |
private Context mContext; | |
private boolean mIsUserPosts; | |
public PostAdapter(Context context, boolean isUserPosts) { | |
mContext = context; | |
mIsUserPosts = isUserPosts; | |
mPosts = new ArrayList<>(); | |
} | |
@Override | |
public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
ItemPostBinding postBinding = DataBindingUtil.inflate( | |
LayoutInflater.from(parent.getContext()), | |
R.layout.item_post, | |
parent, | |
false); | |
return new BindingHolder(postBinding); | |
} | |
@Override | |
public void onBindViewHolder(BindingHolder holder, int position) { | |
ItemPostBinding postBinding = holder.binding; | |
postBinding.setViewModel(new PostViewModel(mContext, mPosts.get(position), mIsUserPosts)); | |
} | |
@Override | |
public int getItemCount() { | |
return mPosts.size(); | |
} | |
public void setItems(List<Post> posts) { | |
mPosts = posts; | |
notifyDataSetChanged(); | |
} | |
public void addItem(Post post) { | |
mPosts.add(post); | |
notifyDataSetChanged(); | |
} | |
public static class BindingHolder extends RecyclerView.ViewHolder { | |
private ItemPostBinding binding; | |
public BindingHolder(ItemPostBinding binding) { | |
super(binding.cardView); | |
this.binding = binding; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment