Android 我点击项目进入回收器视图,它改变了两个项目视图的背景色
我是kotlin的android新手。我有一个线性布局的回收视图。当我在itemView上单击时,我会更改背景以选择它。但该应用程序更改了两个项目视图的背景。我不知道发生了什么。这是我的代码: 月经活动Android 我点击项目进入回收器视图,它改变了两个项目视图的背景色,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,我是kotlin的android新手。我有一个线性布局的回收视图。当我在itemView上单击时,我会更改背景以选择它。但该应用程序更改了两个项目视图的背景。我不知道发生了什么。这是我的代码: 月经活动 class MenuActivity : AppCompatActivity() { private val section = SectionParameters.builder().itemResourceId(R.layout.list_item_grid_menu)
class MenuActivity : AppCompatActivity() {
private val section = SectionParameters.builder().itemResourceId(R.layout.list_item_grid_menu)
.headerResourceId(R.layout.header_layout)
.build()
private val categoryService= CategoriaServiceDbImpl()
private val manageSharedPrefrences = ManageSharedPrefrences()
private var namedbanko = ""
private var defaultSection = 1
private var indexDefaultSection = 0
private var categories = emptyList<Categoria>()
var categoryListAdapter: CategoryListGridRecycleAdapter? = null
var summaryRecycleAdapter: SumaryRecyclerAdapter? = null
var viewSection:View? = null
var posIni:Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_menu)
namedbanko = manageSharedPrefrences.getValueSharedPreference(SessionEnum.CONNECTION.KEY,SessionEnum.SESSION.KEY,this)
/**
* Secciones
*/
sections(defaultSection,indexDefaultSection)
/**
* Lista de productos
*/
val categoryDefault = this.categories.get(this.indexDefaultSection);
val sectionAdapter = SectionedRecyclerViewAdapter()
val productosService = ProductoServiceDbImpl()
productosService.namedb = namedbanko
val listProducts = productosService.getProductoByTipo(defaultSection)
title_section_selected.text = categoryDefault.nombre
sectionAdapter.addSection(ComponentStatelessMenu(this,"",listProducts,section ))
val recycleView: RecyclerView = recyclerlist_menu
val glm = GridLayoutManager(this,2)
glm.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
when (sectionAdapter.getSectionItemViewType(position)) {
SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER -> return 2
else -> return 1
}
}
}
//recycleView.layoutManager = LinearLayoutManager (this)
recycleView.layoutManager = glm
recycleView.setNestedScrollingEnabled(true)
recycleView.adapter = sectionAdapter
val recyclerViewSummary : RecyclerView = recycler_summary
val layoutManager = LinearLayoutManager(this)
recyclerViewSummary.layoutManager =layoutManager
this.summaryRecycleAdapter = SumaryRecyclerAdapter(this)
recyclerViewSummary.adapter = this.summaryRecycleAdapter
}
fun sections(selectedSection:Number,position:Int){
this.categoryListAdapter = CategoryListGridRecycleAdapter(this,selectedSection)
val layoutSections = LinearLayoutManager(this)
layoutSections.orientation = LinearLayoutManager.HORIZONTAL
recycler_sections_menu.layoutManager = layoutSections
recycler_sections_menu.overScrollMode = View.OVER_SCROLL_IF_CONTENT_SCROLLS
if(this.categories.isEmpty()){
this.categories = getSections()
}else{
var i = 0
while(i<this.categories.size){
this.categories.get(i).selected = StateEnum.N.VALUE
i++
}
}
categoryListAdapter?.setCategoryList(this.categories)
recycler_sections_menu.adapter = categoryListAdapter
if(position > 0){
recycler_sections_menu.scrollToPosition(position.toInt()-1)
}
/*val namedb = connectionService.getNameDb(this.applicationContext)
categoryService.namedb = namedb
val listCategory = categoryService.listProductByCategory(0)
val section_category = SectionParameters.builder().itemResourceId(R.layout.list_item_section)
.headerResourceId(R.layout.header_layout)
.build()
val sectionAdapterCategory : SectionedRecyclerViewAdapter = SectionedRecyclerViewAdapter()
sectionAdapterCategory.addSection(CategoriasStatelessSection(getString(R.string.title_sections),listCategory,section_category))
val recycleView: RecyclerView = recycler_sections_menu
val lm = LinearLayoutManager(this)
lm.orientation = LinearLayoutManager.HORIZONTAL
recycleView.layoutManager = lm
recycleView.setNestedScrollingEnabled(true)
recycleView.adapter = sectionAdapterCategory*/
}
fun refreshProductsSection(category: Categoria){
val sectionAdapter : SectionedRecyclerViewAdapter = SectionedRecyclerViewAdapter()
val productosService = ProductoServiceDbImpl()
productosService.namedb = namedbanko
val listProducts = productosService.getProductoByTipo(category.codigo)
sectionAdapter.addSection(ComponentStatelessMenu(this,"",listProducts,section ))
val recycleView: RecyclerView = recyclerlist_menu
val glm = GridLayoutManager(this,2)
glm.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
when (sectionAdapter.getSectionItemViewType(position)) {
SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER -> return 2
else -> return 1
}
}
}
//recycleView.layoutManager = LinearLayoutManager (this)
recycleView.layoutManager = glm
recycleView.setNestedScrollingEnabled(true)
recycleView.adapter = sectionAdapter
}
fun changeColorSelectedSection(view: View,position: Int){
if(this.viewSection!=null){
this.viewSection!!.setBackgroundResource(0)
}
view.setBackgroundColor(getColor(R.color.colorSectionSelected))
categoryListAdapter!!.notifyDataSetChanged()
this.viewSection = view
}
fun getSections():List<Categoria>{
categoryService.namedb = namedbanko
return categoryService.listCategorias()
}
}
var selectedPosition: Int = -1
...
fun changeColorSelectedSection(position: Int) {
selectedPosition = position
categoryListAdapter?.notifyDataSetChanged()
}
您使用的是RecyclerView,它可以回收视图。因此,当您更改特定视图的背景,并且在列表中向下滚动时再次使用该视图时,背景将与当前为该项目设置的背景相同。例如,如果我在屏幕上看到5个项目,并单击项目1使其处于选中状态,则项目1的视图现在处于选中状态。但当我向下滚动到项目6,项目1离开屏幕时,项目1的视图将被重新用于项目6,并且仍处于其状态
为了解决这个问题,您不应该跟踪选定的视图,而应该跟踪选定的项目,然后根据它所代表的项目更新视图。在RecyclerView中,一个视图不是用于一个项目,而是用于多个项目
下面是一个代码更改最少的解决方案:
月经活动
class MenuActivity : AppCompatActivity() {
private val section = SectionParameters.builder().itemResourceId(R.layout.list_item_grid_menu)
.headerResourceId(R.layout.header_layout)
.build()
private val categoryService= CategoriaServiceDbImpl()
private val manageSharedPrefrences = ManageSharedPrefrences()
private var namedbanko = ""
private var defaultSection = 1
private var indexDefaultSection = 0
private var categories = emptyList<Categoria>()
var categoryListAdapter: CategoryListGridRecycleAdapter? = null
var summaryRecycleAdapter: SumaryRecyclerAdapter? = null
var viewSection:View? = null
var posIni:Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_menu)
namedbanko = manageSharedPrefrences.getValueSharedPreference(SessionEnum.CONNECTION.KEY,SessionEnum.SESSION.KEY,this)
/**
* Secciones
*/
sections(defaultSection,indexDefaultSection)
/**
* Lista de productos
*/
val categoryDefault = this.categories.get(this.indexDefaultSection);
val sectionAdapter = SectionedRecyclerViewAdapter()
val productosService = ProductoServiceDbImpl()
productosService.namedb = namedbanko
val listProducts = productosService.getProductoByTipo(defaultSection)
title_section_selected.text = categoryDefault.nombre
sectionAdapter.addSection(ComponentStatelessMenu(this,"",listProducts,section ))
val recycleView: RecyclerView = recyclerlist_menu
val glm = GridLayoutManager(this,2)
glm.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
when (sectionAdapter.getSectionItemViewType(position)) {
SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER -> return 2
else -> return 1
}
}
}
//recycleView.layoutManager = LinearLayoutManager (this)
recycleView.layoutManager = glm
recycleView.setNestedScrollingEnabled(true)
recycleView.adapter = sectionAdapter
val recyclerViewSummary : RecyclerView = recycler_summary
val layoutManager = LinearLayoutManager(this)
recyclerViewSummary.layoutManager =layoutManager
this.summaryRecycleAdapter = SumaryRecyclerAdapter(this)
recyclerViewSummary.adapter = this.summaryRecycleAdapter
}
fun sections(selectedSection:Number,position:Int){
this.categoryListAdapter = CategoryListGridRecycleAdapter(this,selectedSection)
val layoutSections = LinearLayoutManager(this)
layoutSections.orientation = LinearLayoutManager.HORIZONTAL
recycler_sections_menu.layoutManager = layoutSections
recycler_sections_menu.overScrollMode = View.OVER_SCROLL_IF_CONTENT_SCROLLS
if(this.categories.isEmpty()){
this.categories = getSections()
}else{
var i = 0
while(i<this.categories.size){
this.categories.get(i).selected = StateEnum.N.VALUE
i++
}
}
categoryListAdapter?.setCategoryList(this.categories)
recycler_sections_menu.adapter = categoryListAdapter
if(position > 0){
recycler_sections_menu.scrollToPosition(position.toInt()-1)
}
/*val namedb = connectionService.getNameDb(this.applicationContext)
categoryService.namedb = namedb
val listCategory = categoryService.listProductByCategory(0)
val section_category = SectionParameters.builder().itemResourceId(R.layout.list_item_section)
.headerResourceId(R.layout.header_layout)
.build()
val sectionAdapterCategory : SectionedRecyclerViewAdapter = SectionedRecyclerViewAdapter()
sectionAdapterCategory.addSection(CategoriasStatelessSection(getString(R.string.title_sections),listCategory,section_category))
val recycleView: RecyclerView = recycler_sections_menu
val lm = LinearLayoutManager(this)
lm.orientation = LinearLayoutManager.HORIZONTAL
recycleView.layoutManager = lm
recycleView.setNestedScrollingEnabled(true)
recycleView.adapter = sectionAdapterCategory*/
}
fun refreshProductsSection(category: Categoria){
val sectionAdapter : SectionedRecyclerViewAdapter = SectionedRecyclerViewAdapter()
val productosService = ProductoServiceDbImpl()
productosService.namedb = namedbanko
val listProducts = productosService.getProductoByTipo(category.codigo)
sectionAdapter.addSection(ComponentStatelessMenu(this,"",listProducts,section ))
val recycleView: RecyclerView = recyclerlist_menu
val glm = GridLayoutManager(this,2)
glm.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
when (sectionAdapter.getSectionItemViewType(position)) {
SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER -> return 2
else -> return 1
}
}
}
//recycleView.layoutManager = LinearLayoutManager (this)
recycleView.layoutManager = glm
recycleView.setNestedScrollingEnabled(true)
recycleView.adapter = sectionAdapter
}
fun changeColorSelectedSection(view: View,position: Int){
if(this.viewSection!=null){
this.viewSection!!.setBackgroundResource(0)
}
view.setBackgroundColor(getColor(R.color.colorSectionSelected))
categoryListAdapter!!.notifyDataSetChanged()
this.viewSection = view
}
fun getSections():List<Categoria>{
categoryService.namedb = namedbanko
return categoryService.listCategorias()
}
}
var selectedPosition: Int = -1
...
fun changeColorSelectedSection(position: Int) {
selectedPosition = position
categoryListAdapter?.notifyDataSetChanged()
}
类别列表视图持有者
...
fun onBindItemView(itemSection: Categoria, position: Int) {
...
val backgroundResourceId = if (position == menuActivity.selectedPosition) R.color.colorSectionSelected else 0
itemView.setBackgroundResource(backgroundResourceId)
}