NestedScrollView 内部的 RecyclerView 导致 RecyclerView 膨胀所有元素
RecyclerView inside NestedScrollView causes RecyclerView to inflate all elements
我在将 RecyclerView 放置在 NestedScrollView 中时遇到问题,这会导致呈现 RecyclerView 适配器的所有元素。
这是一个相当大的问题,因为 RecyclerView 显示的列表可能包含数百个元素。
目前这会造成相当大的延迟(很明显),因为它必须一次渲染所有视图,并且不能像 RecyclerView 通常那样重用任何已经膨胀的视图。
这是我当前的XML(删除了一些臃肿以最小化它):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="90dp">
<!-- Some content -->
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Some more content -->
</LinearLayout>
<!-- Product list -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
这是我的来自 Fragment 的 onCreateView(),它正在膨胀包含 NestedScrollView 和 RecyclerView 的视图:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.category_content_fragment, container, false);
ButterKnife.bind(this, root);
List<Product> products = new ArrayList<>(); //This is populated by other means, not relevant to the issue
productsRecyclerView.setNestedScrollingEnabled(false);
productsRecyclerView.setHasFixedSize(true);
productsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
ProductsContentAdapter productsContentAdapter = new ProductsContentAdapter(products);
productsRecyclerView.setAdapter(productsContentAdapter);
return root;
}
我已经看到这个 post 关于这个问题:
How to put RecyclerView inside NestedScrollView?
但令人遗憾的是,它没有提到问题的最终解决方案。
澄清一下:
RecyclerView 完美滚动,它在正确的时间显示,但问题是它会立即呈现其所有子项,这意味着可能有数百个元素,即使屏幕一次最多只显示 5-6 个。
如果需要更多信息,请随时提问。
--------编辑------
在多次尝试其他解决方案失败后,我最终使用了 Jeeva Nandhan 的解决方案。
在问这个问题之前,我知道这是一个可能的解决方案,但我有 11 个不同的可能视图需要适合 RecyclerView,所以我想避免它。
使用不同的 ViewType 后,它完美地工作。我担心由于大量的 ViewType 会非常低效,但它非常光滑。
如果您有大量数据要显示,第一次只显示一些数据而不是在滚动时使用 loadMoreListener 获取下一个数据。
我也遇到过这个问题...这是因为scrollview
和RecyclerView
在加载数据时是不同的,因为ScrollView
充当parent 在这种情况下,我们在代码中使用以下行。
setNestedScrollingEnabled(false);
根据 Recyclerview
数据,这将导致滚动变慢和挂起问题。
我用来解决这个问题的一种方法是将 header 添加到 Recyclerview
..
这里我解释清楚。
让我们假设这个 recyclerview
在我们的 activity.
中
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
适配器class将是这样的,我们将在其中添加header
public class SampleAdapter extends RecyclerView.Adapter {
private final int BODY = 1;
private final int HEADER = 2;
private List<String> data = null;
SampleAdapter(List<String> data) {
this.data = data;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return HEADER;
}
return BODY;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case HEADER:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_header_layout, parent, false);
return new HeaderViewHolder(view);
default:
//Setting the Body view...
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_details, parent, false);
return new BodyViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof BodyViewHolder) {
//Set the body content....
if (data != null && data.size() > 0) {
/** Since we have added one cell for header,
* we need to decrement the position and render the body view.
*
*/
int bodyPosition = position - 1;
}
} else if (holder instanceof HeaderViewHolder) {
//Set the header content...
}
}
@Override
public int getItemCount() {
//Sice we are going to add header, we are supposed increase the count by one...
return data.size() + 1;
}
}
这样就不需要 NestedScrollView
并且所有视图都将在 RecyclerView
行为中工作...
希望这对您有所帮助:)
我在将 RecyclerView 放置在 NestedScrollView 中时遇到问题,这会导致呈现 RecyclerView 适配器的所有元素。
这是一个相当大的问题,因为 RecyclerView 显示的列表可能包含数百个元素。
目前这会造成相当大的延迟(很明显),因为它必须一次渲染所有视图,并且不能像 RecyclerView 通常那样重用任何已经膨胀的视图。
这是我当前的XML(删除了一些臃肿以最小化它):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="90dp">
<!-- Some content -->
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Some more content -->
</LinearLayout>
<!-- Product list -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
这是我的来自 Fragment 的 onCreateView(),它正在膨胀包含 NestedScrollView 和 RecyclerView 的视图:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.category_content_fragment, container, false);
ButterKnife.bind(this, root);
List<Product> products = new ArrayList<>(); //This is populated by other means, not relevant to the issue
productsRecyclerView.setNestedScrollingEnabled(false);
productsRecyclerView.setHasFixedSize(true);
productsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
ProductsContentAdapter productsContentAdapter = new ProductsContentAdapter(products);
productsRecyclerView.setAdapter(productsContentAdapter);
return root;
}
我已经看到这个 post 关于这个问题:
How to put RecyclerView inside NestedScrollView?
但令人遗憾的是,它没有提到问题的最终解决方案。
澄清一下: RecyclerView 完美滚动,它在正确的时间显示,但问题是它会立即呈现其所有子项,这意味着可能有数百个元素,即使屏幕一次最多只显示 5-6 个。
如果需要更多信息,请随时提问。
--------编辑------
在多次尝试其他解决方案失败后,我最终使用了 Jeeva Nandhan 的解决方案。
在问这个问题之前,我知道这是一个可能的解决方案,但我有 11 个不同的可能视图需要适合 RecyclerView,所以我想避免它。
使用不同的 ViewType 后,它完美地工作。我担心由于大量的 ViewType 会非常低效,但它非常光滑。
如果您有大量数据要显示,第一次只显示一些数据而不是在滚动时使用 loadMoreListener 获取下一个数据。
我也遇到过这个问题...这是因为scrollview
和RecyclerView
在加载数据时是不同的,因为ScrollView
充当parent 在这种情况下,我们在代码中使用以下行。
setNestedScrollingEnabled(false);
根据 Recyclerview
数据,这将导致滚动变慢和挂起问题。
我用来解决这个问题的一种方法是将 header 添加到 Recyclerview
..
这里我解释清楚。
让我们假设这个 recyclerview
在我们的 activity.
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
适配器class将是这样的,我们将在其中添加header
public class SampleAdapter extends RecyclerView.Adapter {
private final int BODY = 1;
private final int HEADER = 2;
private List<String> data = null;
SampleAdapter(List<String> data) {
this.data = data;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return HEADER;
}
return BODY;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case HEADER:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_header_layout, parent, false);
return new HeaderViewHolder(view);
default:
//Setting the Body view...
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inflate_details, parent, false);
return new BodyViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof BodyViewHolder) {
//Set the body content....
if (data != null && data.size() > 0) {
/** Since we have added one cell for header,
* we need to decrement the position and render the body view.
*
*/
int bodyPosition = position - 1;
}
} else if (holder instanceof HeaderViewHolder) {
//Set the header content...
}
}
@Override
public int getItemCount() {
//Sice we are going to add header, we are supposed increase the count by one...
return data.size() + 1;
}
}
这样就不需要 NestedScrollView
并且所有视图都将在 RecyclerView
行为中工作...
希望这对您有所帮助:)