Giving scroll to full view with recycler view inside it

Usually it's not good practice to have several onScrolls attributes nested.

What you should do, it's to add a header to your recycler with the layout you want to show on top. To do that, you have to add in your adapter the following code (there is not a method like in listView listView.addHeader())

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ITEM) {
        //inflate your layout and pass it to view holder
        return new VHItem(null);
    } else if (viewType == TYPE_HEADER) {
        //inflate your layout and pass it to view holder
        return new VHHeader(null);
    }

    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof VHItem) {
        String dataItem = getItem(position);
        //cast holder to VHItem and set data
    } else if (holder instanceof VHHeader) {
        //cast holder to VHHeader and set data for header.
    }
}

@Override
public int getItemViewType(int position) {
    if (isPositionHeader(position))
        return TYPE_HEADER;

    return TYPE_ITEM;
}

private boolean isPositionHeader(int position) {
    return position == 0;
}

private String getItem(int position) {
    return data[position - 1];
}

class VHItem extends RecyclerView.ViewHolder {

    public VHItem(View itemView) {
        super(itemView);
    }
}

class VHHeader extends RecyclerView.ViewHolder {

    public VHHeader(View itemView) {
        super(itemView);
    }
}

There are also libraries like this


Adding a header and footer is a way, but I found an easier view to do it. Put the whole view inside a scroll view. now this will make the scrolling a bit slow and it wont look nice, so to overcome this use this code :

  layoutManager = new LinearLayoutManager(getActivity()){
        @Override
        public boolean canScrollVertically() {
            return false;
        }
    };

now the problem was that there are 2 scrolls in the same view when I add the whole view inside a scroll view. So we face this problem. Now if we remove scrolling property of one of them then it works smoothly again. So we remove the scrolling property of the recycler view since it is the child.

Works like a charm.