I have a case when I need to have RecyclerView
inside NestedScrollView
.
The problem is that during init methods onCreateViewHolder
and onBindViewHolder
calls for EVERY item in list (100 items in example).
So there is no recycling views on the screen and in the more complex case I have great problems with performance.
Code of my Activity
:
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyListAdapter());
}
}
Code of my .xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/refresh"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"/>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
Code of my Adapter:
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.Holder>{
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
Log.d("TESTING", "onCreate: " + view.hashCode());
return new Holder(view);
}
@Override
public void onBindViewHolder(Holder holder, int position) {
Log.d("TESTING", "onBind position: " + position + ", view: " + holder.view.hashCode());
holder.setContent(position % 2 == 0 ? R.color.colorPrimary : R.color.colorAccent);
}
@Override
public int getItemCount() {
return 100;
}
class Holder extends RecyclerView.ViewHolder {
private View view;
public Holder(View itemView) {
super(itemView);
view = itemView;
}
public void setContent(int colorRes) {
view.setBackgroundResource(colorRes);
}
}
}
And during init I have this logs:
onCreate: 147045034
onBind position: 0, view: 147045034
onCreate: 235006520
onBind position: 1, view: 235006520
onCreate: 16439158
onBind position: 2, view: 16439158
onCreate: 84373988
onBind position: 3, view: 84373988
onCreate: 146076930
onBind position: 4, view: 146076930
onCreate: 12306512
onBind position: 5, view: 12306512
onCreate: 167862094
onBind position: 6, view: 167862094
onCreate: 220010876
... logs for items 7 - 94
onCreate: 189894540
onBind position: 95, view: 189894540
onCreate: 121777898
onBind position: 96, view: 121777898
onCreate: 38672504
onBind position: 97, view: 38672504
onCreate: 210522038
onBind position: 98, view: 210522038
onCreate: 243811364
onBind position: 99, view: 243811364