Android Horizontal RecyclerView选中项背景未重置问题求助
Got it, let's sort out this selection issue in your Horizontal RecyclerView. The core problem here is that you're not resetting the background of the previously selected month when a new one is tapped. Here's a straightforward, efficient way to handle single-item selection properly:
Step 1: Track the Selected Position in Your Adapter
First, add a variable in your RecyclerView Adapter to keep track of which position is currently selected. Initialize it with your current month's position on startup:
// Inside your RecyclerView Adapter class private var selectedPosition: Int = Calendar.getInstance().get(Calendar.MONTH) // Sets current month as initial selection
If you're using Java:
// Inside your Java Adapter class private int selectedPosition = Calendar.getInstance().get(Calendar.MONTH);
Step 2: Update Background in onBindViewHolder
In the onBindViewHolder method, set the item's background based on whether it matches the selectedPosition:
override fun onBindViewHolder(holder: MonthViewHolder, position: Int) { val monthName = monthsList[position] holder.monthTextView.text = monthName // Apply background color based on selection state holder.itemView.setBackgroundColor( if (position == selectedPosition) Color.RED else Color.WHITE ) // Handle item click to update selection holder.itemView.setOnClickListener { // Save the previous selected position to reset it later val previousSelectedPos = selectedPosition // Update to the new selected position selectedPosition = holder.adapterPosition // Notify adapter to refresh only the affected items (more efficient than full reload) notifyItemChanged(previousSelectedPos) notifyItemChanged(selectedPosition) } }
Java equivalent:
@Override public void onBindViewHolder(@NonNull MonthViewHolder holder, int position) { String monthName = monthsList.get(position); holder.monthTextView.setText(monthName); // Set background based on selection holder.itemView.setBackgroundColor( position == selectedPosition ? Color.RED : Color.WHITE ); // Handle click event holder.itemView.setOnClickListener(v -> { int previousSelectedPos = selectedPosition; selectedPosition = holder.getAdapterPosition(); notifyItemChanged(previousSelectedPos); notifyItemChanged(selectedPosition); }); }
Step 3: Ensure Your Item Layout is Compatible
Make sure the root view of your list item doesn't have a hardcoded background that could override the adapter's settings. For example, your item layout (like item_month.xml) should have a default white background (or transparent, if you prefer) so the adapter can dynamically update it:
<!-- Example item layout root view --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/monthTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="16dp" android:background="@android:color/white" <!-- Default background --> android:textSize="16sp"/>
Why This Works
- We maintain a single
selectedPositionvariable to track the active item, so we always know which one to highlight. - When a new item is clicked, we refresh both the previous selected item (to reset its background to white) and the new selected item (to set it to red). Using
notifyItemChangedinstead ofnotifyDataSetChangedis more efficient because it only updates the necessary items, not the entire list. - The initial selection is set to the current month, which aligns with your existing working functionality.
That should resolve the issue—now only the currently selected month will have a red background, and all others will revert to white. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Venkat Maddy

