简体   繁体   English

滚动到全视图与里面的回收者视图

[英]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? 谁能帮我?

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. 现在问题是当我在滚动视图中添加整个视图时,同一视图中有2个滚动。 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. 奇迹般有效。

Usually it's not good practice to have several onScrolls attributes nested. 通常,嵌套多个onScrolls属性并不是一种好习惯。

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()) 要做到这一点,你必须在你的适配器中添加以下代码(没有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 还有像这样的图书馆

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

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