Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why don‘t use ViewHolder? #7

Open
JamesLiAndroid opened this issue Feb 15, 2017 · 2 comments
Open

Why don‘t use ViewHolder? #7

JamesLiAndroid opened this issue Feb 15, 2017 · 2 comments

Comments

@JamesLiAndroid
Copy link

I have gotten the Memory data in running,and I found it occupied much more memory size up to 20M.

@qapqap
Copy link
Owner

qapqap commented Feb 15, 2017

I did try ViewHolder, there was no difference in performance, in fact, it causes me some problems. Can you give more details about how did you get 20M, what is the environment of testing? did you use the existing test code?

Can you suggest anything else to improve memory usage? If you are getting images from drawable, try to move images to drawable-nodpi and test memory usage, please let me know if there is any difference in performance..

@j2emanue
Copy link

it works fine with recyclerview. here is the adapter implementation in kotlin. im posting here just to help other people as the listview does not work in all cases. we need to upgrade it to recyclerview. i modified your adapter to suite my needs but here is an example:

`class TimelineViewAdapter constructor(var context:Context,var resource:Int, var RowDataList:ArrayList) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as? TimelineHolder)?.bind(RowDataList[position])
}

override fun getItemCount() = RowDataList.size


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

   return TimelineHolder(with(Extensions) { parent.inflate(R.layout.custom_timeline_row) })
}

inner class TimelineHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {

    fun bind(row: TimelineRow) = with(itemView) {

        if (position == 0 && position == RowDataList.size - 1) {
            crowUpperLine.visibility = View.INVISIBLE
            crowLowerLine.visibility = View.INVISIBLE
        } else if (position == 0) {
            val belowLineSize = row.belowLineSize

            crowUpperLine.visibility = View.INVISIBLE
            crowLowerLine.setBackgroundColor(row.belowLineColor)
            crowLowerLine.layoutParams.width = belowLineSize
        } else if (position == RowDataList.size - 1) {
            val belowLineSize = RowDataList[position - 1].belowLineSize

            crowLowerLine.visibility = View.INVISIBLE
            crowUpperLine.setBackgroundColor(RowDataList[position - 1].belowLineColor)
            crowUpperLine.layoutParams.width = belowLineSize
        } else {
            val belowLineSize = row.belowLineSize
            val belowLineSize2 = RowDataList[position - 1].belowLineSize

            crowLowerLine.setBackgroundColor(row.belowLineColor)
            crowUpperLine.setBackgroundColor(RowDataList.get(position - 1).belowLineColor)
            crowLowerLine.layoutParams.width = belowLineSize
            crowUpperLine.layoutParams.width = belowLineSize2
        }


        crowDate.text = row.date.toString()
        if (row.dateColor != 0)
            crowDate.setTextColor(row.dateColor)
        if (row.title == null)
            crowTitle.visibility = View.GONE
        else {
            crowTitle.text = row.title
            if (row.titleColor != 0)
                crowTitle.setTextColor(row.titleColor)
        }
        if (row.description == null)
            crowDesc.visibility = View.GONE
        else {
            crowDesc.text = row.description
            if (row.descriptionColor != 0)
                crowDesc.setTextColor(row.descriptionColor)
        }


        if (row.image != null) {
            crowImg.setImageBitmap(row.image)
        }

        val pixels = row.imageSize
        crowImg.layoutParams.width = pixels
        crowImg.layoutParams.height = pixels

        if (row.backgroundColor == 0)
            crowBackground.background = null
        else {
            if (row.backgroundSize == -1) {
                crowBackground.layoutParams.width = pixels
                crowBackground.layoutParams.height = pixels
            } else {
                val backgroundSize = row.backgroundSize
                crowBackground.layoutParams.width = backgroundSize
                crowBackground.layoutParams.height = backgroundSize
            }
            val background = crowBackground.background as GradientDrawable
            background.setColor(row.backgroundColor)
        }

        val marginParams = crowImg.layoutParams as ViewGroup.MarginLayoutParams
        marginParams.setMargins(0, pixels / 2 * -1, 0, pixels / 2 * -1)
    }
    }
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants