简体   繁体   English

如何在ScrollView中添加RecyclerView

[英]How to add RecyclerView in ScrollView

I need to add a RecyclerView in ScrollView. 我需要在ScrollView中添加RecyclerView。 The RecyclerView should wrap its contents and shouldn't be be scrollable, but the entire scroll view should be scrollable. RecyclerView应该包装其内容,并且不能滚动,但是整个滚动视图应该可以滚动。 Can this be done an how? 能做到这一点吗?

If you look at the Recycler view adpater class 如果您查看Recycler视图adpater类

you will be having getITemViewType function 您将拥有getITemViewType函数

from that you can manage to put you scrollview content in one type of item 由此,您可以将滚动浏览内容放在一种类型的项目中

@Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_TYPE_HEADER1;
        } else if (position == 1) {
            return VIEW_TYPE_HEADER2;
        } else {
            return VIEWTYEPITEM;
        }
    }


@Override
    public RecyclerView.ViewHolder onCreateViewHolder(
            final ViewGroup viewGroup, int viewType) {

        if (viewType == VIEWTYEPITEM) {
            //return item view
        } else if (viewType == VIEW_TYPE_HEADER2) {
            //return item view 2
        } else {
            //return item view 1

        }

    }

Main purpose of RecyclerView (as well as of old ListView and GridView ) is performance optimization in cases when you have so many views in the list, that they do not fit on the screen and therefore are scrollable. RecyclerView (以及旧的ListViewGridView )的主要目的是优化性能, GridView列表中的视图过多,视图不适合在屏幕上滚动。

If your RecyclerView is so small that there are not enough views to fill it or there is some constant (and small) amount of views - then you don't need to use it at all. 如果您的RecyclerView很小,以至于没有足够的视图来填充它,或者有一定数量的视图(恒定),那么您根本就不需要使用它。 You won't win anything. 你什么都赢不了。 Moreover, when you have one scrollable view inside of another scrollable view - how do you expect it to work? 此外,当您在另一个可滚动视图中有一个可滚动视图时,您希望它如何工作? Which view should scroll when? 何时滚动哪个视图? It is ambiguous that's why it is not possible to do. 这就是模棱两可的原因,这是不可能的。

On the other hand, if you have a lot of views, then you better of using just RecyclerView without a ScrollView . 另一方面,如果您有很多视图,那么最好只使用RecyclerView而不使用ScrollView In such situation it's common to add some sort of header or footer views which are arbitrarily big. 在这种情况下,通常会添加一些任意大的页眉或页脚视图。 Since RecyclerView is already scrollable, they will work as you want it to work. 由于RecyclerView已经可以滚动,因此它们可以按您希望的方式工作。 @SHASHIDHAR MANCHUKONDA exlained this idea in his answer to your question. @SHASHIDHAR MANCHUKONDA在回答您的问题时对此想法进行了解释。

Stop the scrolling of the ScrollView. 停止滚动查看ScrollView。 Is is perfect upon 5.0. 5.0完美。

public class UnScrollView extends ScrollView{
private int downX;
private int downY;
private int mTouchSlop;

public UnScrollView(Context context) {
    super(context);
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}

public UnScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}

public UnScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    int action = e.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            downX = (int) e.getRawX();
            downY = (int) e.getRawY();
            break;
        case MotionEvent.ACTION_MOVE:
            int moveY = (int) e.getRawY();
            if (Math.abs(moveY - downY) > mTouchSlop) {
                return true;
            }
    }
    return super.onInterceptTouchEvent(e);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM