2016-06-30 13 views
2

recyclerView内のレイアウトの割合が画面に表示される方法がありますか。RecyclerView内のレイアウトが画面上に表示される割合

RecyclerView内で膨張レイアウトが

android:layout_width="match_parent" android:layout_height="match_parent"

のparamsが編集レイアウトファイル

レイアウトファイルを追加しました - custom_card.xml

<?xml version="1.0" encoding="utf-8"? 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/card" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignBottom="@id/imageView" 
     android:layout_alignTop="@id/imageView" 
     android:scaleType="fitCenter" 
     android:src="@drawable/image_view_screen" /> 

</RelativeLayout> 

活動のレイアウトファイルを。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
    <ProgressBar 
     android:visibility="gone" 
     android:layout_width="56dp" 
     android:layout_height="56dp" 
     android:id="@+id/progress_bar" 
     android:layout_centerInParent="true"/> 
</RelativeLayout> 

は、私はそれを考え出した垂直LinearLayoutManager

+0

あなたのレイアウトの幅を取得し、ここでは高さを使用する例 –

+0

ためrecyclerViewの幅と比較することができます。レイアウトの高さは、画面上に表示される部分ではなく、元の寸法になります。 – Harshit

+0

それから私たちに何かを見せてください。スクリーンショット?レイアウトファイル? –

答えて

0

後くらいみてくださいを使用しています。誰にでも役立つなら。

私のレイアウトの高さはmatch_parentだったので、私にとっては簡単でした。しかし、この方法は、いくつかの計算をして全員に有効です。

int firstItemPos=mLayoutManager.findFirstVisibleItemPosition(); 
View firstItemView=mLayoutManager.findViewByPosition(firstItemPos); 
int lastItemPos=mLayoutManager.findLastVisibleItemPosition(); 
View lastItemView=mLayoutManager.findViewByPosition(lastItemPos); 

firstItemPoslastItemPosの間のすべての項目が表示100%です。同様lastItemView-

Math.abs(lastItemView.getY())/lastItemView.getHeight()  
-1

使用このためfirstItemView-

Math.abs(firstItemView.getY())/firstItemView.getHeight() 

について

。あなたのアダプターの ;

private final Rect mCurrentViewRect = new Rect(); 
... your inits... 

onBindViewHolder;

View firstItemView = linearLayoutManager.findViewByPosition(position); 
           int pos= getVisibilityPercents(position); 

これらのmetodsを使用します。

public int getVisibilityPercents(View currentView) { 


    int percents = 100; 

    currentView.getLocalVisibleRect(mCurrentViewRect); 

    int height = currentView.getHeight(); 

    if (viewIsPartiallyHiddenTop()) { 
     percents = (height - mCurrentViewRect.top) * 100/height; 
    } else if (viewIsPartiallyHiddenBottom(height)) { 
     percents = mCurrentViewRect.bottom * 100/height; 
    } 

    return percents; 
} 

private boolean viewIsPartiallyHiddenBottom(int height) { 
    return mCurrentViewRect.bottom > 0 && mCurrentViewRect.bottom < height; 
} 

private boolean viewIsPartiallyHiddenTop() { 
    return mCurrentViewRect.top > 0; 
} 

btw、コンストラクタでアダプタにrecyclerViewとlinearLayoutManagerを取得しないでください。より多くの場合

=>https://github.com/danylovolokh/VideoPlayerManager

関連する問題