效果如上,代码adapter使用的是BaseRecyclerViewAdapterHelper代码如下
class SpecialtyClassifyActivity : BaseMvpActivity(), SpecialtyClassifyContract.View {
override fun getLayoutViewId(): Int {
return R.layout.activity_special_classify
}
private val mAdaper: SpecialClassifyAdapter by lazy {
SpecialClassifyAdapter()
}
private val mNameAdaper: SpecialNameAdapter by lazy {
SpecialNameAdapter()
}
private val mSaveNamekey = linkedMapOf()
private val mSaveContentKey=linkedMapOf()
private var mOldPos = 0
override fun initView(mRootView: View, savedInstanceState: Bundle?) {
mRecyclerSpecialName.run {
adapter = mNameAdaper
}
mRecyclerSpecialContent.run {
adapter = mAdaper
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val manager = recyclerView.layoutManager as LinearLayoutManager
val position = manager.findFirstCompletelyVisibleItemPosition()
val item = mAdaper.getItem(position)
if (mSaveNamekey.containsKey(item!!.type)) {
val namePos = mSaveNamekey.get(item.type)!!
if (namePos != mOldPos) {
mNameAdaper.setNewData(resetValue(mNameAdaper.data, namePos))
val nameManager=mRecyclerSpecialName.layoutManager as LinearLayoutManager
nameManager.scrollToPositionWithOffset(namePos,0)
}
}
}
})
}
mNameAdaper.run {
setOnItemClickListener { _, _, position ->
val item = mNameAdaper.getItem(position)
if (mSaveContentKey.containsKey(item!!.type)){
val contentPos=mSaveContentKey.get(item.type)
val manager=mRecyclerSpecialContent.layoutManager as LinearLayoutManager
manager.scrollToPositionWithOffset(contentPos!!,0)
}
}
}
mNameAdaper.setNewData(getTitleName())
mAdaper.setNewData(getContent())
}
override fun initData() {
}
private fun resetValue(data: MutableList, pos: Int): MutableList {
data.get(pos).select = true
data.get(mOldPos).select = false
mOldPos = pos
return data
}
private fun getContent(): MutableList {
val data = mutableListOf()
data.add(SpecialClassifyEntity("计算机专业", 1))
data.add(SpecialClassifyEntity("计算机专业", 1))
data.add(SpecialClassifyEntity("计算机专业", 1))
data.add(SpecialClassifyEntity("计算机专业", 1))
data.add(SpecialClassifyEntity("会计专业", 2))
data.add(SpecialClassifyEntity("会计专业", 2))
data.add(SpecialClassifyEntity("金融专业", 3))
data.add(SpecialClassifyEntity("金融专业", 3))
data.add(SpecialClassifyEntity("护理专业", 4))
data.add(SpecialClassifyEntity("护理专业", 4))
data.add(SpecialClassifyEntity("竞技专业", 5))
data.add(SpecialClassifyEntity("竞技专业", 5))
data.add(SpecialClassifyEntity("美术专业", 6))
data.add(SpecialClassifyEntity("美术专业", 6))
data.add(SpecialClassifyEntity("美发专业", 7))
var oldType=0
for (i in data.indices) {
val entity = data[i]
if (oldType!=entity.type){
mSaveContentKey.put(entity.type, i)
oldType=entity.type
}
}
Log.e("tag",mSaveContentKey.toString())
return data
}
private fun getTitleName(): MutableList {
val data = mutableListOf()
data.add(SpecialNameEntity("计算机专业", 1, true))
data.add(SpecialNameEntity("会计专业", 2, false))
data.add(SpecialNameEntity("金融专业", 3, false))
data.add(SpecialNameEntity("护理专业", 4, false))
data.add(SpecialNameEntity("竞技专业", 5, false))
data.add(SpecialNameEntity("美术专业", 6, false))
data.add(SpecialNameEntity("美发专业", 7, false))
data.add(SpecialNameEntity("篮球专业", 8, false))
data.add(SpecialNameEntity("足球专业", 9, false))
data.add(SpecialNameEntity("羽毛球专业", 10, false))
data.add(SpecialNameEntity("高尔夫球专业", 11, false))
data.add(SpecialNameEntity("乒乓球专业", 12, false))
data.add(SpecialNameEntity("橄榄球专业", 13, false))
data.add(SpecialNameEntity("棒球专业", 14, false))
data.add(SpecialNameEntity("蹴鞠专业", 15, false))
for (i in data.indices) {
val entity = data[i]
mSaveNamekey.put(entity.type, i)
}
return data
}
}