我在将RecyclerView放置在NestedScrollView中时遇到了问题,这会导致呈现回收视图适配器的所有元素。
这是一个相当大的问题,因为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>这是来自膨胀包含NestedScrollView和RecyclerView的视图的片段的NestedScrollView():
@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;
}我看过这篇关于这个问题的文章:
How to put RecyclerView inside NestedScrollView?
但遗憾的是,它并没有提到问题的最终解决办法。
为了澄清: RecyclerView滚动完美,它在正确的时间显示,但问题是它立即呈现它的所有子元素,这意味着可能有数百个元素,即使屏幕一次最多只能显示5-6个元素。
如果需要更多的信息,请随时提问。
-编辑-编辑
在多次尝试其他解决方案失败后,我最终使用了Jeeva Nandhan的解决方案。
在问这个问题之前,我知道这是一个可能的解决方案,但我有11个不同的可能的观点,需要融入RecyclerView,所以我想避免它。
在使用了不同的ViewTypes之后,它工作得非常完美。我担心这将是非常低效的,因为大量的ViewTypes,但它是黄油光滑。
发布于 2017-06-10 06:01:04
我也遇到过这个问题..。这是因为scrollview和RecyclerView在加载数据方面是不同的,因为在本例中ScrollView充当父程序,我们在代码中使用了下面的行。
setNestedScrollingEnabled(false);这将使基于Recyclerview数据的滚动延迟和挂起问题。
我用来解决这个问题的一种方法是在Recyclerview中添加标题。
我会在这里解释清楚。
让我们假设这个recyclerview在我们的活动中。
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>适配器类将如下所示,其中我们将添加标头
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行为中工作.
希望这是有益的:)
发布于 2017-06-10 05:32:34
如果您有大量的数据要显示,第一次只显示一些数据比滚动使用loadMoreListener获取下一个数据。
https://stackoverflow.com/questions/44453846
复制相似问题