这几天开发项目,scrollview中嵌套了recyclerview,滚动的时候scrollview会不滚动,出现recyclerview的布局往上滚动出现被遮挡的现象,但是这个不是我想要的,于是在网上找解决方法,可能有人会觉得直接禁止recyclerview滚动就可以了,做法是重新recyclerview的layoutmanager
public class MyGridLayoutManager extends GridLayoutManager {
private boolean isScrollEnabled = true;
public MyGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public MyGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public MyGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
public void setScrollEnabled(boolean flag) {
this.isScrollEnabled = flag;
}
@Override
public boolean canScrollVertically() {
return isScrollEnabled && super.canScrollVertically();
}
}
这样recyclerview是不滚动了,但是会发现 recyclerview的有一部分UI直接不见了,相当于直接滚动到底部固定了,于是随着scrollview滚动,并不能达到我想要的效果。
想要实现这个效果,最简单的解决方法是:
在recyclerview外层在包裹一层layout,如RelativeLayout。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/row_list_item">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
// Disabled nested scrolling since Parent scrollview will scroll the content.
mRecyclerView.setNestedScrollingEnabled(false);
// specify an adapter (see also next example)
mAdapter = new SimpleListAdapter(DataSetProvider.generateDataset());
问题解决。