Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. ////SETTING UP THE ADAPTER
  2. private void setupAdapter() {
  3.  
  4.  
  5. //stops lag for recycler view for load
  6. commentsRecyclerView.setHasFixedSize(true);
  7. commentsAdapter = new CommentsAdapter(commentsList, TypeFaceProvider.getTypeFace(this, 0),
  8. TypeFaceProvider.getTypeFace(this, 1), TypeFaceProvider.getTypeFace(this, 2));
  9. LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
  10. mLayoutManager.setReverseLayout(true);
  11. mLayoutManager.setStackFromEnd(true);
  12.  
  13. commentsRecyclerView.setLayoutManager(mLayoutManager);
  14.  
  15. commentsRecyclerView.setItemAnimator(new DefaultItemAnimator());
  16.  
  17. commentsAdapter.setOnEntryClickListener(new CommentsAdapter.OnEntryClickListener() {
  18. @Override
  19. public void onEntryClick(View view, int position) {
  20. //DatabaseComment comment = commentsList.get(position);
  21. // TextView deleteBtn = (TextView) view.findViewById(R.id.commentUserRemove);
  22. // if (view == deleteBtn) {
  23. //
  24. // //used to remove the comment from db and the list
  25. // db.removeSingleComment(comment);
  26. // dbCommentsList.remove(position);
  27. // commentsAdapter.notifyDataSetChanged();
  28. //
  29. // } else {
  30. takeToUserProfile(commentsList.get(position));
  31. // }
  32. }
  33. });
  34.  
  35. commentsRecyclerView.setAdapter(commentsAdapter);
  36. commentsRecyclerView.scrollToPosition(0);
  37.  
  38. if(commentsList.size() < 1) {
  39. noCommentsResults.setVisibility(View.VISIBLE);
  40. }
  41.  
  42. hideProgressDialog();
  43. }
  44.  
  45.  
  46. ////////////THE ADAPTER CLASS
  47.  
  48. public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  49.  
  50. private static OnEntryClickListener mOnEntryClickListener;
  51. private List<ConversationComment> dbCommentsList;
  52. private Typeface typeFace, italicTypeface, boldTypeface;
  53.  
  54. public CommentsAdapter(List<ConversationComment> comments, Typeface myTypeface, Typeface myTypefaceItalic, Typeface myTypefaceBold) {
  55.  
  56. dbCommentsList = comments;
  57. typeFace = myTypeface;
  58. italicTypeface = myTypefaceItalic;
  59. boldTypeface = myTypefaceBold;
  60.  
  61. Log.d(Constants.DEBUG, "THE COMMENT SIZE " + dbCommentsList.size());
  62. }
  63.  
  64. public void setOnEntryClickListener(OnEntryClickListener onEntryClickListener) {
  65. mOnEntryClickListener = onEntryClickListener;
  66. }
  67.  
  68. @Override
  69. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  70. switch (viewType) {
  71. case 0:
  72. return new MyFeatureViewHolder(LayoutInflater.from(parent.getContext())
  73. .inflate(R.layout.comment_business_item, parent, false));
  74. case 1:
  75. return new MyViewHolder(LayoutInflater.from(parent.getContext())
  76. .inflate(R.layout.comment_user_item, parent, false));
  77. }
  78.  
  79. return new MyViewHolder(LayoutInflater.from(parent.getContext())
  80. .inflate(R.layout.comment_user_item, parent, false));
  81.  
  82.  
  83. }
  84.  
  85. @Override
  86. public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  87.  
  88. int pos = getItemViewType(position);
  89.  
  90. //is a business comment
  91. if (pos == 0) {
  92. MyFeatureViewHolder featureViewHolder = (MyFeatureViewHolder) holder;
  93.  
  94. ConversationComment dbComment = dbCommentsList.get(position);
  95.  
  96. featureViewHolder.commentCompany.setTypeface(boldTypeface);
  97. featureViewHolder.commentCompanyMsg.setTypeface(typeFace);
  98. featureViewHolder.commentCompanyDate.setTypeface(italicTypeface);
  99.  
  100. featureViewHolder.commentCompany.setText(dbComment.getUsername());
  101. featureViewHolder.commentCompanyMsg.setText(dbComment.getMsg());
  102.  
  103. Calendar date = Calendar.getInstance();
  104. date.setTimeInMillis(dbComment.getDateCommented());
  105. int newMonth = date.get(Calendar.MONTH) + 1;
  106. String commentDateTxt = (newMonth + "." + date.get(Calendar.DAY_OF_MONTH) + "." + date.get(Calendar.YEAR));
  107.  
  108. featureViewHolder.commentCompanyDate.setText(commentDateTxt);
  109.  
  110.  
  111. }
  112.  
  113.  
  114.  
  115. //anything greater than 0 is a user comment - with emoji
  116. if (pos == 1) {
  117. MyViewHolder myViewHolder = (MyViewHolder) holder;
  118.  
  119. // if (dbCommentsList.get(position).getIsChanged() == 1) {
  120. // myViewHolder.commentUserRemove.setVisibility(View.VISIBLE);
  121. // } else {
  122. // myViewHolder.commentUserRemove.setVisibility(View.GONE);
  123. // }
  124. Log.d(Constants.DEBUG, "THE comment " + dbCommentsList.get(position).getMsg());
  125.  
  126.  
  127. ConversationComment dbComment = dbCommentsList.get(position);
  128.  
  129. myViewHolder.commentUsername.setTypeface(boldTypeface);
  130. myViewHolder.commentUserMsg.setTypeface(typeFace);
  131. myViewHolder.commentUserDate.setTypeface(italicTypeface);
  132.  
  133. myViewHolder.commentUsername.setText(dbComment.getUsername());
  134. myViewHolder.commentUserMsg.setText(dbComment.getMsg());
  135.  
  136. Calendar date = Calendar.getInstance();
  137. date.setTimeInMillis(dbComment.getDateCommented());
  138.  
  139. //Note only one plus one because of new comments added will
  140. int newMonth = date.get(Calendar.MONTH) + 1;
  141. String commentDateTxt = (newMonth + "." + date.get(Calendar.DAY_OF_MONTH) + "." + date.get(Calendar.YEAR));
  142.  
  143. myViewHolder.commentUserDate.setText(commentDateTxt);
  144.  
  145. int[] commentsImageList = new int[]{R.drawable.e1, R.drawable.e1, R.drawable.e2, R.drawable.e3, R.drawable.e4,
  146. R.drawable.e5, R.drawable.e6, R.drawable.e7, R.drawable.e8, R.drawable.e9, R.drawable.e10,
  147. R.drawable.e11, R.drawable.e12, R.drawable.e13, R.drawable.e14,
  148. R.drawable.e15, R.drawable.e16, R.drawable.e17, R.drawable.e18, R.drawable.e19};
  149. myViewHolder.emojiIcon.setImageResource(commentsImageList[dbComment.getEmoji()]);
  150.  
  151.  
  152. }
  153.  
  154.  
  155. }
  156.  
  157. @Override
  158. public int getItemCount() {
  159.  
  160. Log.d(Constants.DEBUG, "THE comment " + dbCommentsList.size());
  161. return dbCommentsList.size();
  162. }
  163.  
  164. @Override
  165. public int getItemViewType(int position) {
  166. if (dbCommentsList.get(position).getEmoji() == 0) {
  167. return 0;
  168. } else {
  169. return 1;
  170. }
  171. }
  172.  
  173.  
  174. public interface OnEntryClickListener {
  175. void onEntryClick(View view, int position);
  176. }
  177.  
  178. public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  179. public TextView commentUsername, commentUserMsg, commentUserDate, commentUserRemove;
  180. public ImageView emojiIcon;
  181.  
  182.  
  183. public MyViewHolder(View view) {
  184. super(view);
  185. commentUsername = (TextView) view.findViewById(R.id.commentUsername);
  186. commentUserMsg = (TextView) view.findViewById(R.id.commentUserMsg);
  187. commentUserDate = (TextView) view.findViewById(R.id.commentUserDate);
  188. commentUserRemove = (TextView) view.findViewById(R.id.commentUserRemove);
  189. emojiIcon = (ImageView) view.findViewById(R.id.emojiIcon);
  190.  
  191. view.setOnClickListener(this);
  192. commentUserRemove.setOnClickListener(this);
  193.  
  194. }
  195.  
  196. @Override
  197. public void onClick(View v) {
  198. if (mOnEntryClickListener != null) {
  199. mOnEntryClickListener.onEntryClick(v, getAdapterPosition());
  200.  
  201. }
  202. }
  203. }
  204.  
  205. public class MyFeatureViewHolder extends RecyclerView.ViewHolder {
  206. public TextView commentCompany, commentCompanyMsg, commentCompanyDate;
  207. public ImageView emojiIcon;
  208.  
  209.  
  210. public MyFeatureViewHolder(View view) {
  211. super(view);
  212. commentCompany = (TextView) view.findViewById(R.id.commentCompany);
  213. commentCompanyMsg = (TextView) view.findViewById(R.id.commentCompanyMsg);
  214. commentCompanyDate = (TextView) view.findViewById(R.id.commentCompanyDate);
  215. emojiIcon = (ImageView) view.findViewById(R.id.emojiIcon);
  216.  
  217.  
  218. }
  219.  
  220.  
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement