Created
January 8, 2018 16:06
-
-
Save cuber5566/199f52bfbd161cd29c9882903a5ec50d to your computer and use it in GitHub Desktop.
RecyclerView multiType
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
package com.dpick.screen.post.detail | |
import android.content.Context | |
import android.content.res.Resources | |
import android.support.v7.widget.RecyclerView | |
import android.view.LayoutInflater | |
import android.view.ViewGroup | |
import com.dpick.R | |
import com.dpick.model.Comment | |
import com.dpick.model.Post | |
import com.dpick.provider.resource.ResourceProvider | |
import com.dpick.screen.post.holder.* | |
import com.dpick.util.parser.CommentParser | |
import com.dpick.util.parser.ContentParser | |
import com.dpick.util.parser.PostParser | |
class PostDetailAdapter( | |
private var context: Context, | |
private var postFooterListener: PostFooterHolder.OnActionClickListener, | |
private val commentTextClickListener: ContentParser.OnTextClickListener, | |
private val resourceProvider: ResourceProvider = ResourceProvider.getInstance() | |
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
var post: Post? = null | |
var commentList: List<Comment>? = null | |
private var postContentArray: ArrayList<PostParser.PostData>? = null | |
private var commentContentArray: ArrayList<CommentParser.CommentData>? = null | |
private val layoutInflater = LayoutInflater.from(context) | |
private var postContentSize: Int = 0 | |
private var commentContentSize: Int = 0 | |
fun setPost(post: Post): PostDetailAdapter { | |
this.post = post | |
postContentArray = PostParser.parse(post, resourceProvider.getColor(R.color.colorPrimary), commentTextClickListener) | |
notifyDataSetChanged() | |
return this | |
} | |
fun setCommentList(commentList: List<Comment>): PostDetailAdapter { | |
this.commentList = commentList | |
commentContentArray = CommentParser.parse(commentList, resourceProvider.getColor(R.color.colorPrimary), commentTextClickListener) | |
notifyDataSetChanged() | |
return this | |
} | |
override fun getItemCount(): Int { | |
postContentSize = postContentArray?.size ?: 0 | |
commentContentSize = commentContentArray?.size ?: 0 | |
return postContentSize + commentContentSize | |
} | |
override fun getItemViewType(position: Int): Int { | |
val postPosition = getPostPositionOffset(position) | |
val commentPosition = getCommentPositionOffset(position) | |
return when { | |
postPosition < postContentSize -> getPostType(postPosition) | |
commentPosition < commentContentSize -> getCommentType(commentPosition) | |
else -> NONE | |
} | |
} | |
private fun getPostType(position: Int): Int { | |
val content = postContentArray!![position] | |
return when (content.type) { | |
PostParser.POST_HEADER -> POST_HEADER | |
ContentParser.TEXT -> POST_CONTENT_TEXT | |
ContentParser.IMAGE -> POST_CONTENT_IMAGE | |
ContentParser.OPEN_GRAPH -> POST_CONTENT_OPEN_GRAPH | |
ContentParser.TWITTER -> POST_CONTENT_TWITTER | |
ContentParser.YOUTUBE -> POST_CONTENT_YOUTUBE | |
PostParser.POST_FOOTER -> POST_FOOTER | |
else -> NONE | |
} | |
} | |
private fun getCommentType(position: Int): Int { | |
val content = commentContentArray!![position] | |
return when (content.type) { | |
CommentParser.COMMENT_HEADER -> COMMENT_HEADER | |
ContentParser.TEXT -> COMMENT_CONTENT_TEXT | |
ContentParser.IMAGE -> COMMENT_CONTENT_IMAGE | |
ContentParser.OPEN_GRAPH -> COMMENT_CONTENT_OPEN_GRAPH | |
ContentParser.TWITTER -> COMMENT_CONTENT_TWITTER | |
ContentParser.YOUTUBE -> COMMENT_CONTENT_YOUTUBE | |
CommentParser.COMMENT_FOOTER -> COMMENT_FOOTER | |
else -> NONE | |
} | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder? { | |
val holder = when (viewType) { | |
POST_HEADER -> PostHeaderHolder(layoutInflater.inflate(PostHeaderHolder.LAYOUT_ID, parent, false)) | |
POST_CONTENT_TEXT -> TextHolder(layoutInflater.inflate(TextHolder.LAYOUT_ID, parent, false)) | |
POST_CONTENT_IMAGE -> ImageHolder(layoutInflater.inflate(ImageHolder.LAYOUT_ID, parent, false)) | |
POST_CONTENT_OPEN_GRAPH -> OpenGraphHolder(layoutInflater.inflate(OpenGraphHolder.LAYOUT_ID, parent, false)) | |
POST_CONTENT_TWITTER -> TwitterHolder(layoutInflater.inflate(TwitterHolder.LAYOUT_ID, parent, false)) | |
POST_CONTENT_YOUTUBE -> YoutubeHolder(layoutInflater.inflate(YoutubeHolder.LAYOUT_ID, parent, false)) | |
POST_FOOTER -> PostFooterHolder(layoutInflater.inflate(PostFooterHolder.LAYOUT_ID, parent, false)) | |
COMMENT_HEADER -> CommentHeaderHolder(layoutInflater.inflate(CommentHeaderHolder.LAYOUT_ID, parent, false)) | |
COMMENT_CONTENT_TEXT -> TextHolder(layoutInflater.inflate(TextHolder.LAYOUT_ID, parent, false)) | |
COMMENT_CONTENT_IMAGE -> ImageHolder(layoutInflater.inflate(ImageHolder.LAYOUT_ID, parent, false)) | |
COMMENT_CONTENT_OPEN_GRAPH -> OpenGraphHolder(layoutInflater.inflate(OpenGraphHolder.LAYOUT_ID, parent, false)) | |
COMMENT_CONTENT_TWITTER -> TwitterHolder(layoutInflater.inflate(TwitterHolder.LAYOUT_ID, parent, false)) | |
COMMENT_CONTENT_YOUTUBE -> YoutubeHolder(layoutInflater.inflate(YoutubeHolder.LAYOUT_ID, parent, false)) | |
COMMENT_FOOTER -> CommentFooterHolder(layoutInflater.inflate(CommentFooterHolder.LAYOUT_ID, parent, false)) | |
else -> null | |
} | |
// setting comment default padding | |
when (viewType) { | |
COMMENT_HEADER, | |
COMMENT_CONTENT_TEXT, | |
COMMENT_CONTENT_IMAGE, | |
COMMENT_CONTENT_OPEN_GRAPH, | |
COMMENT_CONTENT_TWITTER, | |
COMMENT_CONTENT_YOUTUBE, | |
COMMENT_FOOTER -> { | |
val param: RecyclerView.LayoutParams = holder!!.itemView.layoutParams as RecyclerView.LayoutParams | |
param.leftMargin = DEFAULT_PADDING | |
param.rightMargin = DEFAULT_PADDING | |
} | |
} | |
return holder | |
} | |
override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) { | |
val postPosition = getPostPositionOffset(position) | |
val commentPosition = getCommentPositionOffset(position) | |
val text = when { | |
postPosition in 0..(postContentSize - 1) -> postContentArray!![postPosition].text | |
commentPosition in 0..(commentContentSize - 1) -> commentContentArray!![commentPosition].text | |
else -> "" | |
} | |
when (holder) { | |
// Post | |
is PostHeaderHolder -> holder.bindTo(post!!) | |
is PostFooterHolder -> holder.bindTo(post!!, postFooterListener) | |
// Comment | |
is CommentHeaderHolder -> holder.bindTo(commentContentArray!![commentPosition].comment) | |
is CommentTextHolder -> holder.bindTo(commentContentArray!![commentPosition].text) | |
// Content | |
is TextHolder -> holder.bindTo(text) | |
is ImageHolder -> holder.bindTo(context, text.toString()) | |
is OpenGraphHolder -> holder.bindTo(context, text.toString()) | |
is TwitterHolder -> holder.bindTo(context, text.toString()) | |
is YoutubeHolder -> holder.bindTo(context, text.toString()) | |
} | |
} | |
private fun getPostPositionOffset(position: Int): Int { | |
return position | |
} | |
private fun getCommentPositionOffset(position: Int): Int { | |
return position - postContentSize | |
} | |
companion object { | |
const val NONE = 0x00 | |
const val POST_HEADER = 0x10 | |
const val POST_CONTENT_TEXT = 0x20 | |
const val POST_CONTENT_IMAGE = 0x21 | |
const val POST_CONTENT_OPEN_GRAPH = 0x23 | |
const val POST_CONTENT_TWITTER = 0x24 | |
const val POST_CONTENT_YOUTUBE = 0x25 | |
const val POST_FOOTER = 0x30 | |
const val COMMENT_HEADER = 0x40 | |
const val COMMENT_CONTENT_TEXT = 0x51 | |
const val COMMENT_CONTENT_IMAGE = 0x52 | |
const val COMMENT_CONTENT_OPEN_GRAPH = 0x53 | |
const val COMMENT_CONTENT_TWITTER = 0x54 | |
const val COMMENT_CONTENT_YOUTUBE = 0x55 | |
const val COMMENT_FOOTER = 0x60 | |
val DEFAULT_PADDING = (Resources.getSystem().displayMetrics.density * 8).toInt() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment