Mec*_*cid 189 android android-recyclerview
RecyclerView里面怎么用NestedScrollView?
RecyclerView设置适配器后内容不可见.
UPDATE布局代码已更新.
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/keyline_1">
</RelativeLayout>
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#e5e5e5" />
<android.support.v7.widget.RecyclerView
android:id="@+id/conversation"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
Rah*_*yay 198
用您的recyclerView替换,
<android.support.v7.widget.RecyclerView
android:id="@+id/conversation"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
这里,
app:layout_behavior="@string/appbar_scrolling_view_behavior"
Run Code Online (Sandbox Code Playgroud)
将管理其余的事情.
还有一件事,不需要将您的recyclerView放在NestedScrollView中
smb*_*now 117
更新1
自Android支持库23.2.0以来,setAutoMeasureEnabled(true)为LayoutManagers 添加了方法.它使RecyclerView能够包装它的内容并且像魅力一样工作.
http://android-developers.blogspot.ru/2016/02/android-support-library-232.html
所以只需添加以下内容:
LayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setAutoMeasureEnabled(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)
更新2
由于setAutoMeasureEnabled不推荐使用27.1.0 ,因此您应该使用重写方法提供LayoutManager的自定义实现isAutoMeasureEnabled()
但在使用RecyclerView的许多情况后,我强烈建议不要在包装模式下使用它,因为这不是它的目的.尝试使用具有多个项目类型的普通单个RecyclerView重构整个布局.或者使用我在下面描述的LinearLayout方法作为最后的手段
老答案(不推荐)
你可以RecyclerView在里面使用NestedScrollView.首先,您应该实现自己的自定义LinearLayoutManager,它会让您RecyclerView包装其内容.例如:
public class WrappingLinearLayoutManager extends LinearLayoutManager
{
public WrappingLinearLayoutManager(Context context) {
super(context);
}
private int[] mMeasuredDimension = new int[2];
@Override
public boolean canScrollVertically() {
return false;
}
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final int heightMode = View.MeasureSpec.getMode(heightSpec);
final int widthSize = View.MeasureSpec.getSize(widthSpec);
final int heightSize = View.MeasureSpec.getSize(heightSpec);
int width = 0;
int height = 0;
for (int i = 0; i < getItemCount(); i++) {
if (getOrientation() == HORIZONTAL) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
heightSpec,
mMeasuredDimension);
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
measureScrapChild(recycler, i,
widthSpec,
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
View view = recycler.getViewForPosition(position);
if (view.getVisibility() == View.GONE) {
measuredDimension[0] = 0;
measuredDimension[1] = 0;
return;
}
// For adding Item Decor Insets to view
super.measureChildWithMargins(view, 0, 0);
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(
widthSpec,
getPaddingLeft() + getPaddingRight() + getDecoratedLeft(view) + getDecoratedRight(view),
p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(
heightSpec,
getPaddingTop() + getPaddingBottom() + getDecoratedTop(view) + getDecoratedBottom(view),
p.height);
view.measure(childWidthSpec, childHeightSpec);
// Get decorated measurements
measuredDimension[0] = getDecoratedMeasuredWidth(view) + p.leftMargin + p.rightMargin;
measuredDimension[1] = getDecoratedMeasuredHeight(view) + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
}
Run Code Online (Sandbox Code Playgroud)
在那之后使用它LayoutManager为您的RecyclerView
recyclerView.setLayoutManager(new WrappingLinearLayoutManager(getContext()));
Run Code Online (Sandbox Code Playgroud)
但你也应该调用这两种方法:
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);
Run Code Online (Sandbox Code Playgroud)
这里setNestedScrollingEnabled(false)禁用滚动RecyclerView,因此它不会拦截滚动事件NestedScrollView.并setHasFixedSize(false)确定适配器内容的更改可以更改大小RecyclerView
重要提示:这个解决方案在某些情况下很小,并且在性能方面存在问题,所以如果您有很多项目,RecyclerView我建议使用LinearLayout基于自定义的列表视图实现,为它创建适配器模拟并制作它表现得像ListView或RecyclerView
Ash*_*rma 93
1)您需要使用上面的支持库23.2.0(或)
2)和RecyclerView高度wrap_content.
3) recyclerView.setNestedScrollingEnabled(false)
但通过这样做,回收者模式不起作用.(即所有视图将立即加载,因为wrap_content需要完整的高度,RecyclerView因此它将立即绘制所有子项View.没有视图将被回收).除非确实需要,否则尽量不要使用此模式util.尝试使用viewType,并补充说,需要滚动到所有其它视图RecyclerView,而不是使用RecyclerView在Scrollview.性能影响将非常高.
为了简单起见"它只是像LinearLayout所有子视图一样"
zay*_*ayn 34
你可以用它android:fillViewport="true"来NestedScrollView衡量RecyclerView.在RecyclerView将填补剩余的高度.所以如果你想滚动它NestScrollView,你可以设置RecyclerView's minHeight.
Vig*_*dar 26
简单地添加它recyclerView.setNestedScrollingEnabled(false);之前setAdapter为我工作.我没有添加app:layout_behavior="@string/appbar_scrolling_view_behavior"任何地方,也没有设置任何自定义布局管理器
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Some Text..."
android:padding="15dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:padding="15dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Quick Links"
android:textColor="@color/black"
android:textStyle="bold"
android:textAllCaps="true"
android:paddingLeft="20dp"
android:drawableLeft="@drawable/ic_trending_up_black_24dp"
android:drawablePadding="10dp"
android:layout_marginBottom="10dp"
android:textSize="16sp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#efefef"/>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
小智 19
这对我有用
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
Mar*_*ler 14
因为androidx它被称为androidx.core.widget.NestedScrollView- 它像黄油一样滚动isScrollContainer并measureAllChildren启用了属性:
<!-- Scrolling Content -->
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="true"
android:measureAllChildren="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:splitMotionEvents="false"
android:verticalScrollbarPosition="right"/>
</androidx.core.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
尝试使用此库 - https://github.com/serso/android-linear-layout-manager.
库的LayoutManager使RecyclerView包装其内容.在这种情况下,RecyclerView将"与内部视图一样大",因此它不会有滚动条,用户将使用NestedScrollView的滚动功能.因此,它不会像"可滚动内部可滚动"一样模棱两可.
小智 7
你可以检查一个简单的测试代码
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
这是我用来避免滚动问题的代码:
mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.getLayoutManager().setAutoMeasureEnabled(true);
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(false);
Run Code Online (Sandbox Code Playgroud)
小智 6
我在NestedScrollView中使用了RecyclerView,它对我有用.我必须记住的唯一问题是NestedScrollView只接受一个子视图.所以在我的情况下,我使用了LienearLayout viewgroup,它包含了我的RecyclerView以及我需要的其他一些视图.
我遇到一个问题,将我的RecyclerView放在NestedScrollView中.我意识到滚动我的RecyclerView的内容松了一口气.
我后来意识到我的RecyclerView正在接收滚动事件,因此与NestedScrollView的滚动行为相冲突.
所以为了解决这个问题,我不得不用这种方法禁用我的RecyclerView的滚动功能 movieListNewRecyclerView.setNestedScrollingEnabled(false);
你可以在我的Instagram上查看我实际做过的简短视频.这是我的Instagram句柄ofelix03
我在NestedScrollView中有Viewpager和RecyclerView。添加以下行后
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);
Run Code Online (Sandbox Code Playgroud)
我解决了慢滚动和滚动滞后的问题。
小智 5
如果你想在NestedScrollView中使用RecyclerView这是一个简单的技巧,只需设置:
回收视图
recyclerView.setHasFixedSize(false) (java/kt)
机器人:nestedScrollingEnabled =“假”
安卓:layout_height =“wrap_content”
android:overScrollMode="从不"
嵌套滚动视图
这对我来说是工作,你也可以在 NestedScrollView 中使用许多 RecyclerView 。
| 归档时间: |
|
| 查看次数: |
168573 次 |
| 最近记录: |