Moo*_*oom 5 xml android scrollview android-recyclerview nestedscrollview
我在将 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>
Run Code Online (Sandbox Code Playgroud)
这是我的 onCreateView() 来自 Fragment,它正在膨胀包含 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;
}
Run Code Online (Sandbox Code Playgroud)
我看过这篇关于这个问题的帖子:
How to put RecyclerView inside NestedScrollView?
但遗憾的是,它没有提到该问题的最终解决方案。
澄清一下: RecyclerView 滚动完美,它在正确的时间显示,但问题是它立即呈现其所有子元素,这意味着可能有数百个元素,即使屏幕一次最多只显示 5-6 个。
如果需要更多信息,请随时提出问题。
------- 编辑 -------
经过其他解决方案的多次失败尝试后,我最终使用了 Jeeva Nandhan 的解决方案。
在问这个问题之前,我知道这是一个可能的解决方案,但我有 11 种不同的可能视图需要适应 RecyclerView,所以我希望避免它。
使用不同的 ViewTypes 后,它工作得很好。我担心由于大量的 ViewType,它会非常低效,但它非常流畅。
我也遇到过这个问题...这是因为两者在加载数据方面scrollview
都RecyclerView
不同,因为ScrollView
在这种情况下充当父级,并且我们在代码中使用以下行。
setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)
这将使滚动缓慢并根据Recyclerview
数据出现挂起问题。
我用来解决这个问题的一种方法是将标头添加到Recyclerview
..
这里我就解释清楚。
让我们假设这recyclerview
是在我们的活动中。
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Run Code Online (Sandbox Code Playgroud)
适配器类将是这样的,我们将在其中添加标头
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这样就不需要了NestedScrollView
,所有的视图都会在 RecyclerView
行为中起作用......
希望这有帮助:)