How to add RecyclerView in ScrollView

人盡茶涼 提交于 2019-12-11 19:24:32

问题


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?


回答1:


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

        }

    }



回答2:


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.




回答3:


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);
}

}



来源:https://stackoverflow.com/questions/31879646/how-to-add-recyclerview-in-scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!