gpt4 book ai didi

android - NedtedScrollView 中的 RecyclerView 为每个项目调用 onCreateViewHolder

转载 作者:太空宇宙 更新时间:2023-11-03 13:42:19 27 4
gpt4 key购买 nike

我有一个案例,我需要在 NestedScrollView 中使用 RecyclerView。问题是在初始化方法期间 onCreateViewHolderonBindViewHolder 调用列表中的每个项目(例如 100 个项目)。

所以屏幕上没有回收 View ,在更复杂的情况下,我在性能方面遇到了很大的问题。

我的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());
}
}

我的.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>

我的适配器代码:

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);
}
}
}

在初始化期间我有这个日志:

    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

最佳答案

我以前遇到过这个问题,根据我的发现,不可能有这个设置并让 RecyclerView 实际回收 View 。

RecyclerView 通过询问其父 View 有多大来计算它必须显示多少个 View 。
ScrollView 要求它的子级膨胀它的所有 View ,因此它可以计算自己的高度,并查看它应该能够滚动多少。

这会导致冲突,因此这意味着 RecyclerView 将膨胀其所有 subview ,这基本上使 RecyclerView 变得无用。

我的解决方法是让所有元素成为 RecyclerView 的一部分,然后为不同的 View 类型设置不同的 View 。从而消除了ScrollView。

关于android - NedtedScrollView 中的 RecyclerView 为每个项目调用 onCreateViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49464840/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com