簡體   English   中英

滾動到全視圖與里面的回收者視圖

[英]Giving scroll to full view with recycler view inside it

我想建立一個屏幕,我在頂部和下面有一個布局,我有一個像這樣的回收站視圖:

<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>

現在我想給整個視圖滾動視圖,但是當我這樣做時,屏幕的滾動並不順暢,因為屏幕上有兩個滾動,我該如何解決這個問題? 誰能幫我?

添加頁眉和頁腳是一種方式,但我發現更容易看到它。 將整個視圖放在滾動視圖中。 現在這將使滾動有點慢,它看起來不會很好,所以要克服這個使用此代碼:

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

現在問題是當我在滾動視圖中添加整個視圖時,同一視圖中有2個滾動。 所以我們面臨這個問題。 現在,如果我們刪除其中一個的滾動屬性,那么它再次順利運行。 因此,我們刪除了回收器視圖的滾動屬性,因為它是孩子。

奇跡般有效。

通常,嵌套多個onScrolls屬性並不是一種好習慣。

你應該做什么,它是為你的回收者添加一個標題,你想要顯示在頂部的布局。 要做到這一點,你必須在你的適配器中添加以下代碼(沒有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);
    }
}

還有像這樣的圖書館

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM