Pab*_*blo 4 android android-recyclerview android-nestedscrollview
我有一个案例,当我需要使用RecyclerViewinside 时NestedScrollView。问题是在 init 方法onCreateViewHolder和onBindViewHolder调用列表中的每个项目(示例中为 100 个项目)期间。
所以屏幕上没有回收视图,在更复杂的情况下,我有很大的性能问题。
我的代码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());
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码.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>
Run Code Online (Sandbox Code Playgroud)
我的适配器代码:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在初始化期间我有这个日志:
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
Run Code Online (Sandbox Code Playgroud)
我以前遇到过这个问题,根据我的发现,不可能进行此设置并使 RecyclerView 实际回收视图。
RecyclerView 通过询问它的父视图有多大来计算它必须显示多少个视图。
ScrollView 要求它的子级扩展它的所有视图,以便它可以计算自己的高度,并查看它应该能够滚动多少。
这会导致冲突,因此这意味着 RecyclerView 将膨胀其所有子视图,这基本上会使 RecyclerView 变得无用。
我的解决方法是让所有元素成为 RecyclerView 的一部分,然后为不同的视图类型设置不同的视图。从而消除了ScrollView。
| 归档时间: |
|
| 查看次数: |
859 次 |
| 最近记录: |