我尝试使RecyclerView
平滑滚动。我只加载10个项目,加载时带有ProgressBar
,加载后消失。但是我的RecyclerView
停留在第一个元素上。
我用过:
recyclerView.setNestedScrollingEnabled(false);
没有成功。
class SummeryRecyclerViewAdapter(
var context: Context, val resultList: ArrayList<Result>
) : RecyclerView.Adapter<SummeryRecyclerViewAdapter.ViewHolder>() {
lateinit var onloadMoreListener: OnLoadMoreListener
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): SummeryRecyclerViewAdapter.ViewHolder {
return ViewHolder(LayoutInflater.from(parent?.context).inflate(R.layout.sigle_row_for_summery, parent, false))
}
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
// MY CODE IS HERE (ITS LONG LIME CODE)}
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItemViewType(position: Int): Int {
return position
}
override fun getItemCount(): Int {
return resultList.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// MY VIEW ITEMS HERE ...
}
fun setLoadMoreListener(onLoadMoreListener: OnLoadMoreListener) {
this.onloadMoreListener = onLoadMoreListener
}
interface OnLoadMoreListener {
fun onLoadMore()
}
fun addMoreData(articleLists: MutableList<Result>) {
resultList.addAll(articleLists)
notifyItemRangeInserted(resultList.size + 1, articleLists.size)
}
}
我的活动代码:
class SummeryActivity : AppCompatActivity(), SummeryRecyclerViewAdapter.OnLoadMoreListener {
private lateinit var summeryRecyclerViewAdapter: SummeryRecyclerViewAdapter
var last_no: Int = 0
var isLoading: Boolean = false
var countLoad: Int = 1
val resultHandler = ResultHandler(this)
var exam_id: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_summery)
window.statusBarColor = resources.getColor(R.color.colorPrimaryDark)
summery_btnCloseActivity.setOnClickListener { finish() }
exam_id = intent.getIntExtra("examId", 0)
val myData = resultHandler.getResultData(exam_id)
Log.e("myData", "${myData}")
summeryRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
summeryRecyclerViewAdapter = SummeryRecyclerViewAdapter(this, getItems())
summeryRecyclerView.setNestedScrollingEnabled(false);
summeryRecyclerView.adapter = summeryRecyclerViewAdapter
summeryRecyclerViewAdapter.setLoadMoreListener(this)
}
fun getItems(): ArrayList<Result> {
val userList: ArrayList<Result>
userList = resultHandler.getResultData(exam_id, "10", last_no)
last_no = userList.size
return userList
}
override fun onLoadMore() {
if (resultHandler.getResultData(exam_id).size != last_no) {
if (!isLoading) {
isLoading = true
Handler().postDelayed({
load_progress_summery.visibility = View.VISIBLE
summeryRecyclerView.smoothScrollToPosition(summeryRecyclerViewAdapter.itemCount)
}, 100)
}
Handler().postDelayed({
isLoading = false
load_progress_summery.visibility = View.GONE
val userList: ArrayList<Result>
userList = resultHandler.getResultData(exam_id, "10", last_no)
last_no += userList.size
countLoad += 1
summeryRecyclerViewAdapter.addMoreData(userList)
}, 10 * 100)
} else {
load_progress_summery.visibility = View.GONE
}
}
}