RecyclerView默认显示最后一项

时间:2018-05-29 09:45:20

标签: android android-recyclerview

我有一个包含两个不同项目的回收者视图。

第一个位置应显示一组按钮,而其余项目显示有关特定联系人的详细信息。

这是适配器代码

class CustomersAdapter(private val context: Context, private val customersListener: CustomerListener) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val contacts: ArrayList<Contact> = ArrayList()
    private var lastPosition = -1
    private val colorGenerator = ColorGenerator.MATERIAL

    companion object {
        const val ITEM_TYPE_CONTACT = 1
        const val ITEM_TYPE_BUTTONS = 2
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == ITEM_TYPE_BUTTONS) {
            val v = LayoutInflater.from(context).inflate(R.layout.item_group_buttons, parent, false)
            GroupViewHolder(v)
        } else {
            val v = LayoutInflater.from(context).inflate(R.layout.item_customer, parent, false)
            ViewHolderContact(v)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder.itemViewType == ITEM_TYPE_CONTACT) {
            val contact = contacts[position - 1]
            (holder as ViewHolderContact).bind(contact)
        }
        showAnimation(holder.itemView, position)
    }

    override fun getItemCount(): Int {
        return contacts.size + 1
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) ITEM_TYPE_BUTTONS
        else ITEM_TYPE_CONTACT
    }

    private fun showAnimation(viewToAnimate: View, position: Int) {
        if (position > lastPosition) {
            val animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left)
            viewToAnimate.animation = animation
            lastPosition = position
        }
    }

    interface CustomerListener {
        fun onCustomerClicked(contact: Contact)
    }

    inner class ViewHolderContact(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bind(contact: Contact) {

            itemView.icon_image.visibility = View.GONE
            itemView.icon_text.visibility = View.VISIBLE

            itemView.tv_text.text = contact.name.firstChar()
            itemView.tv_customer_name.text = contact.name

            itemView.tv_customer_progress.text = "${contact.profileCompleteness}% completed"

            itemView.progress_customer.progress = contact.profileCompleteness

            val icon = itemView.icon_profile.background as GradientDrawable
            icon.setColor(colorGenerator.getColor(contact.name ?: ""))

            itemView.setOnClickListener { customersListener.onCustomerClicked(contacts[adapterPosition]) }

        }
    }

    inner class GroupViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    fun setContacts(contacts: ArrayList<Contact>) {
        val callback = DiffUtilCallback(this.contacts, contacts)
        val diffResult = DiffUtil.calculateDiff(callback)
        diffResult.dispatchUpdatesTo(this)

        this.contacts.clear()
        this.contacts.addAll(contacts)
    }
}

但是当设置适配器时,回收站视图会显示最后一项,我必须添加类似recyclerView.smoothScrollTo(0);的内容,但是当超过1000时它会成为一个问题。为什么会发生这种情况?当我刚接触联系人项目而不是异类回收者视图时,这并没有发生。

1 个答案:

答案 0 :(得分:0)

试试这个

if (mRecyclerView != null) {
    getHandler().post(new Runnable() {
        @Override
        public void run() {
            mRecyclerView.scrollToPosition(0);
            if (position == 0) {
                LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
                layoutManager.scrollToPositionWithOffset(0, 0);
            }
        }
    });
}