Reputation: 373
After many hours of researching I am finally consulting official help.
I have a RecyclerView.Adapter and RecyclerView.ViewHolders that worked perfectly. But for some reasons I do not understand, RecyclerView.Adapter.onBindViewHolder is not called properly.
private class AttendeeAdapter extends RecyclerView.Adapter<AttendeeHolder> {
/*FIELDS*/
private List<Attendee> mAttendeeList;
/*CONSTRUCTORS*/
public AttendeeAdapter(List<Attendee> attendees) {
mAttendeeList = attendees;
//Log.i(TAG, "AttendeeAdapter size: " + getItemCount());
}
Based on the Log message (the item count as the size of the list as expected), I assume the AttendeeAdapter was properly instantiated.
So I expect onBindViewHolder(VH, int) method would be called as many times as the size of the List but it is not. The method is called only ONCE!
/*METHODS*/
@Override
public AttendeeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View itemView = layoutInflater.inflate(R.layout.list_attendee, parent, false);
return new AttendeeHolder(itemView);
}
@Override
public void onBindViewHolder(AttendeeHolder holder, int position) {
Attendee attendee = mAttendeeList.get(position);
holder.bindAttendee(attendee, position);
Log.i(TAG, "Binding ViewHolder #" + position);
/* Binding ViewHolder #0 and that's it */
}
@Override
public int getItemCount() {
return mAttendeeList.size();
}
}
My AttendeeHolder (extending RecyclerView.ViewHolder) goes as the following:
private class AttendeeHolder extends RecyclerView.ViewHolder {
/*FIELDS*/
private EditText mAttendeeNameEditText;
private Attendee mAttendee;
/*CONSTRUCTOR*/
public AttendeeHolder(View itemView) {
super(itemView);
mAttendeeNameEditText = (EditText) itemView.findViewById(R.id.edit_text_list_item);
mAmountEditTextList = new ArrayList<>(eventMaxCount);
}
/*METHODS*/
public void bindAttendee(Attendee attendee, final int position) {
mAttendee = attendee;
String attendeeName = mAttendee.getName();
// Set the name to the EditText if a name has already been set
if (attendeeName != null) {
mAttendeeNameEditText.setText(attendeeName);
}
}
}
and implemented in the main code as
List<Attendee> attendees = AttendeeLab.get().getAttendeeList();
mAttendeeAdapter = new AttendeeAdapter(attendees);
mAmountRecyclerView.setAdapter(mAttendeeAdapter);
I guess the code would work (I think I have not made any change) but the gradle dependencies might not be properly set. That was where I tried modifying recyclerview-v7:23.3.0 to recyclerview-v7:23.1.0 or whatever (None of them worked).
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:recyclerview-v7:23.1.2'
}
Any help or comment would be appreciated. I wish I would be saying good bye to the headache after several hours from now.
Upvotes: 36
Views: 16839
Reputation: 41
Check the code where you are setting up your adapter in Activity/Fragment in mine case there was an orientation issue.
activityAddBusinessBinding.rvImageList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
Upvotes: 1
Reputation: 542
In my case the recyclerview's single item layout's root layout(constraint layout) height was set to match_parent instead of 'wrap_content', changed it to 'wrap_content' and problem is fixed
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<TextView
android:id="@+id/tv_city_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="center"
android:text="@{location.name}"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:typeface="serif" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 0
Reputation: 28748
In my case I had to override getItemId()
. When I added different items to an adapter, they had the same id
. And onBindViewHolder
stopped to call when scrolling. So, I changed from
override fun getItemId(position: Int): Long = list[position].id.toLong()
to
override fun getItemId(position: Int): Long = position.toLong()
The adapter also contains these settings:
override fun getItemCount(): Int = list.size
override fun getItemViewType(position: Int): Int = list[position].viewType
this.setHasStableIds(true)
It would be better to use a collection with unique id
s.
Upvotes: 0
Reputation: 817
In a case I came across, was when my recyclerview was scrollable only horizontally, not vertically.
Upvotes: 3
Reputation: 410
Also check if you set the height of your RecyclerView itself to android:layout_height="wrap_content"
OR android:layout_height="match_parent"
. In both cases, it will adapt the height of a single item and appears to be just one, but you can actually scroll the list. Instead, set the height of the RecyclerView manually.
Upvotes: 0
Reputation: 119
For people who have this problem more recently and are running the beta versions of SDK 28 of support library change your target SDK to 27 and use the 27.1.1 version of the support libraries (most recent versions at time of writing).
I was struggling with this problem on the 28.0.0-alpha3 libraries, changed the version to 27.1.1 in my app.gradle and the problem was solved.
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:support-vector-drawable:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
Upvotes: 0
Reputation: 196
Take care of the onBindViewHolder behaviour in the RecyclerView cycles. RecyclerView.ViewHolder position 0 itemView
android:layout_height="match_parent"
occupies the current displayed screen. Scroll and onBindViewHolder should be triggered but make sure getItemCount was set appropriately.
Solution:
android:layout_height="wrap_content"
Solution with ConstraintLayout as inflated itemView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"><!-- IMPORTANT -->
<!-- ... -->
</android.support.constraint.ConstraintLayout>
Upvotes: 15
Reputation: 3513
The problem is not in your code. Make sure you set layout_height
to wrap_content
of RecyclerView child item.
Upvotes: 113
Reputation: 5517
Use the same versions for all your support libraries:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0' //<<< here
}
Also, the RecyclerView
should be added with the 'com.android.support:design:23.3.0'
package - the overwriting of the classor whatever magic Gradle
has done in the build process with this "duplicate" package may have caused your problem.
Upvotes: 1