Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving scroll to full view with recycler view inside it

I want to implment a screen where i have a layout on the top and below that i have a recycler view like this :

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    tools:context="com.app.InstHomeDir.Fragments.PendingDocument"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_bg"
   >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="15dp"
        >

    <android.support.v7.widget.CardView

        android:background="@color/white"
        app:cardElevation="2dp"
        app:cardCornerRadius="2dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <com.app.InstHomeDir.Util.Roboto_Edit_Text_Bold
             android:textColor="@color/bl2d2d2d"
             android:padding="5dp"
            android:text="DOCUMENT LIST"
                android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
            <com.app.InstHomeDir.Util.Roboto_EditText
             android:padding="5dp"
             android:textColor="#6f6f6f"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
                android:textSize="12sp"
            android:text="@string/Pending_Doc"
            android:layout_height="wrap_content"/>
        </LinearLayout>


    </android.support.v7.widget.CardView>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_penddoc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_anchor="@id/toolbar_layout"
        app:layout_anchorGravity="bottom|center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

    </android.support.v7.widget.RecyclerView>


    </LinearLayout>



</RelativeLayout>

now i wish to give scroll view to the entire view, but when i do that the scrolling of the screen is no smooth, as there are two scrolls available on the screen, how do I solve this issue? Can anyone help me?

like image 752
Parth Anjaria Avatar asked Mar 14 '16 09:03

Parth Anjaria


People also ask

What is nested scroll view?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

How do I scroll to the bottom of my recycler view?

Recyclerview scroll to bottom using scrollToPositon. After setting the adapter, then call the scrollToPosition function to scroll the recycler view to the bottom.


2 Answers

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.

like image 80
Parth Anjaria Avatar answered Sep 28 '22 09:09

Parth Anjaria


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

like image 27
Spirrow Avatar answered Sep 28 '22 10:09

Spirrow