RecyclerView滞后于第一(4-5)次滚动

时间:2018-07-31 06:57:01

标签: android performance android-recyclerview

我已经为我的项目使用RecyclerView创建了自己的CalendarYearView列表。对于前几个滚动条(4-5个滚动条),它会滞后,但之后速度更快。我无法找出问题所在。

我尝试添加setHasFixedSize(true)setHasStableIds(true)。但是这两种情况都不起作用。

它在所有设备中都在发生,我尝试过 Samsung S8 moto G3 Xiomi MI Pad

我已在下面附加了文件,请给我一些解决此问题的建议。

YearListActivity

class YearsListActivity : BaseActivity(), SimpleYearCallback {
private var currYear: Int = 0
private var currPos: Int = 0
var l: ArrayList<Int> = ArrayList()
private var mAdapter: YearsRecyclerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_years_list)
    init()
}

override fun onPostCreate(savedInstanceState: Bundle?) {
    super.onPostCreate(savedInstanceState)
    setTitle(getString(R.string.tit_choose_year), false)
}

override fun onStart() {
    super.onStart()
    refresh()
}

override fun init() {
    mAdapter = YearsRecyclerAdapter(this, l)
    mAdapter?.callback = this
    val llm = LinearLayoutManager(this)
    llm.orientation = RecyclerView.VERTICAL
    recycler?.layoutManager = llm
    val snapHelper = PagerSnapHelper()
    snapHelper.attachToRecyclerView(recycler)
    recycler?.adapter = mAdapter
}

override fun refresh() {
    l.clear()
    for (i in GAuth.getInstance().minYear..GAuth.getInstance().maxYear) {
        l.add(i)
    }
    currYear = CalendarUtil.calendar().get(Calendar.YEAR)
    currPos = currYear - GAuth.getInstance().minYear
    recycler?.scrollToPosition(currPos)
    mAdapter?.notifyDataSetChanged()
}

override fun onMonthClicked(month: Int, year: Int) {
    ActivityManager.goHomeActivity(this, CalendarUtil.calendar(year, month).timeInMillis)
}
}

YearRecyclerAdapter

class YearsRecyclerAdapter(private var mContext: Context,
                       private var l: List<Int>)
: RecyclerView.Adapter<YearsRecyclerAdapter.SimpleYearHolder>() {

var callback: SimpleYearCallback? = null

override fun getItemCount(): Int {
    return l.size
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
        : SimpleYearHolder {
    return SimpleYearHolder(LayoutInflater.from(mContext)
            .inflate(R.layout.view_simple_year_view_static,
                    parent, false))
}

override fun onBindViewHolder(holder: SimpleYearHolder
                              , position: Int) {
    val year = l[position]
    holder.itemView.title.text = year.toString()
    for (i in 0 until CalendarData.MONTHS) {
        holder.initSMV(holder.itemView.
                findViewById(ResourceUtil.getId(mContext,
                "month$i", ResourceUtil.RES_ID)),
                year, 0)
    }
}

inner class SimpleYearHolder(itemView: View)
    : RecyclerView.ViewHolder(itemView) {
    var year: Int = 0

    fun initSMV(smv: SimpleMonthView, year: Int, month: Int) {
        smv.millis = CalendarUtil.calendar(year, month).timeInMillis
        smv.setOnClickListener {
            callback?.onMonthClicked(month, year)
        }
        smv.refresh()
    }
}
}

SimpleMonthView

class SimpleMonthView : LinearLayout, ViewInitializer {
var millis: Long = 0
var l: ArrayList<String>? = ArrayList()
var cal: Calendar? = null
var title: CustomTextView? = null
private var isCurrentMonth = false
private var todayDate: Int? = -1

constructor(context: Context)
        : super(context) {
    initView()
}

constructor(context: Context, attrs: AttributeSet?)
        : super(context, attrs) {
    initView()
}

override fun initAttrs(attrs: AttributeSet?) {

}

override fun initView() {
    View.inflate(context,
            R.layout.view_simple_month_view_static,
            this)
    title = findViewById(R.id.title)
}

override fun refresh() {
    title?.text =
            CalendarUtil.getMonthName(CalendarUtil.calendar(millis))
    generateGrid()
}

override fun destroy() {

}

private fun generateGrid() {
    cal = CalendarUtil.calendar(millis)
    if (CalendarUtil.isSameMonth(millis,
                    CalendarUtil.calendar().timeInMillis)) {
        isCurrentMonth = true
        todayDate = CalendarUtil.calendar()
                .get(Calendar.DAY_OF_MONTH)
    }
    var count = CalendarData.WEEK
    if (CalendarUtil.getMonthStartPos(cal)!! !=
            Calendar.SUNDAY)
        for (i in 1 until CalendarUtil.getMonthStartPos(cal)!!) {
            count++
        }
    for (i in 1..CalendarUtil.getMonthMaxDays(cal)!!) {
        val tv = findViewById<SquareTextView> 

(ResourceUtil.getId(上下文,                     “ day $ count”,                     ResourceUtil.RES_ID))             tv.text = i.toString()             tv.setTextColor(                     如果(isCurrentMonth && i == todayDate)                         红色                     其他                         ContextCompat.getColor(context,                                 R.color.default_txt_color)             )             数++         }     }

}

布局/ activity_years_list

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@android:color/white"
    android:layout_height="match_parent">

    <include
        layout="@layout/view_toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <include
        layout="@layout/view_recycler"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_container"/>
</android.support.constraint.ConstraintLayout>

布局/ adapter_simple_year_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/space_4dp"
android:orientation="vertical">

<chuchu.guruji.scheduler.view.CustomTextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/space_4dp"
    android:fontFamily="@font/roboto_thin"
    android:gravity="center"
    android:textColor="@color/year_header"
    android:textSize="@dimen/txt_large"
    android:textStyle="italic|bold" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month0"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month1"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month2"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth"
            android:layout_gravity="center" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month3"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month4"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month5"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month6"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month7"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month8"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month9"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month10"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

        <chuchu.guruji.scheduler.view.calendar.SimpleMonthView
            android:id="@+id/month11"
            android:paddingStart="@dimen/space_4dp"
            android:paddingEnd="@dimen/space_4dp"
            style="@style/SimpleMonthView.EqWidth" />

    </LinearLayout>
</LinearLayout>
</LinearLayout>

布局/ view_simple_month_view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText">

<chuchu.guruji.scheduler.view.CustomTextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fontFamily="@font/roboto_bold"
    android:gravity="center_vertical"
    android:paddingStart="@dimen/space_4dp"
    android:textSize="@dimen/txt_tiny" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day0"
        style="@style/CustomTextView.Thin"
        android:text="S" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day1"
        style="@style/CustomTextView.Thin"
        android:text="M" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day2"
        style="@style/CustomTextView.Thin"
        android:text="T" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day3"
        style="@style/CustomTextView.Thin"
        android:text="W" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day4"
        style="@style/CustomTextView.Thin"
        android:text="T" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day5"
        style="@style/CustomTextView.Thin"
        android:text="F" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day6"
        style="@style/CustomTextView.Thin"
        android:text="S" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day7"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day8"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day9"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day10"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day11"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day12"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day13"
        style="@style/CustomTextView.Thin" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day14"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day15"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day16"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day17"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day18"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day19"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day20"
        style="@style/CustomTextView.Thin" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day21"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day22"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day23"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day24"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day25"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day26"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day27"
        style="@style/CustomTextView.Thin" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day28"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day29"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day30"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day31"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day32"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day33"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day34"
        style="@style/CustomTextView.Thin" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day35"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day36"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day37"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day38"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day39"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day40"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day41"
        style="@style/CustomTextView.Thin" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day42"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day43"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day44"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day45"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day46"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day47"
        style="@style/CustomTextView.Thin" />

    <chuchu.guruji.scheduler.view.SquareTextView
        android:id="@+id/day48"
        style="@style/CustomTextView.Thin" />
</LinearLayout>
</LinearLayout>

style.xml

<style name="CustomTextView.Thin">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_weight">1</item>
    <item name="android:gravity">center</item>
    <item name="android:textSize">@dimen/txt_tiny</item>
    <item name="fontFamily">@font/roboto_light</item>
    <item name="android:textColor">@color/default_txt_color</item>
</style>

我最后的Year列表视图

enter image description here

0 个答案:

没有答案