user1796624
user1796624

Reputation: 4007

How to add RecyclerView in ScrollView

I need to add a RecyclerView in ScrollView. The RecyclerView should wrap its contents and shouldn't be be scrollable, but the entire scroll view should be scrollable. Can this be done an how?

Upvotes: 0

Views: 1215

Answers (3)

Sanisy
Sanisy

Reputation: 11

Stop the scrolling of the ScrollView. Is is perfect upon 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);
}

}

Upvotes: 1

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23952

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.

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. 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. 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. @SHASHIDHAR MANCHUKONDA exlained this idea in his answer to your question.

Upvotes: 2

SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

If you look at the Recycler view adpater class

you will be having getITemViewType function

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

        }

    }

Upvotes: 3

Related Questions