2015-12-15 26 views
5

Vì vậy, tôi có một ImageView làm nền và trên đầu nó là một RecyclerView ngang, điều ở đây là tôi muốn RecyclerView bắt đầu hiển thị các mục của nó từ nửa đệm của màn hình sang trái để bạn có thể thấy nền hoàn hảo ngay từ đầu và khi bạn cuộn các mục bạn muốn ẩn hình ảnh/nền.RecyclerView ngang với đệm bắt đầu

Lưu ý: hãy kiểm tra Cửa hàng Play để xem những gì tôi đang cố gắng hoàn thành.

enter image description here

Vì vậy, tôi đã làm việc này:

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#000052" 
android:orientation="vertical" > 

     <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="200dp" 
     android:scaleType="centerCrop" 
     android:src="@drawable/trybg5" 
     android:layout_gravity="left" /> 

       <HorizontalScrollView 
       android:id="@+id/frag_home_gallery_recent_container" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       android:layout_gravity="right" 
       android:scrollbars="none" 
       android:layout_weight="1" 
       android:paddingBottom="@dimen/horizontalGallery_content_padding" 
       android:paddingTop="@dimen/horizontalGallery_content_padding"> 


       <LinearLayout 
        android:id="@+id/frag_home_gallery_recientes" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:paddingLeft="150dp" /> 
      </HorizontalScrollView> 
    </RelativeLayout> 

Quan điểm của tôi đã ở đó để lập trình thổi phồng cách bố trí bên trong HorizontalScrollView, và nó làm việc độc đáo. Nhưng với thời gian, tôi đã quyết định chuyển sang RecyclerView và hiện không hoạt động như mong đợi.

Nền xuất hiện, Recycler bắt đầu một nửa màn hình bên phải, nhưng khi di chuyển nó sẽ không di chuyển cho đến khi phía bên trái của màn hình, nó sẽ chỉ che giấu bất cứ nơi nào đệm được thành lập ..

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#000052" 
android:orientation="vertical" > 

    <ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="200dp" 
    android:scaleType="centerCrop" 
    android:src="@drawable/trybg5" 
    android:layout_gravity="left" /> 

      <FrameLayout 
        android:id="@+id/frag_home_gallery_recent_container" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_gravity="right" 
        android:scrollbars="none" 
        android:layout_weight="1" 
        android:paddingBottom="@dimen/horizontalGallery_content_padding" 
        android:paddingTop="@dimen/horizontalGallery_content_padding"> 

        <android.support.v7.widget.RecyclerView 
         android:background="@drawable/gallery_bg" 
         android:id="@+id/frag_home_gallery_recientes" 
         android:scrollbars="none" 
         android:paddingLeft="150dp" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         /> 

       </FrameLayout> 
      </RelativeLayout> 

Bất kỳ ý tưởng làm thế nào tôi có thể làm cho hiệu ứng này hoạt động trong RecyclerView?

Dưới đây là các chi tiết của tái chế:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:id="@+id/discoContainer" 
    android:background="@drawable/gallery_bg" 
    android:layout_margin="5dp" 
    android:padding="2dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/containerDisco" 
     android:orientation="vertical"> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <ImageView 
       android:id="@+id/logoDisco" 
       android:transitionName="DiscoId" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:scaleType="centerCrop" 
       android:contentDescription="LogoDisco" 
       android:windowSharedElementsUseOverlay="false" /> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="a 600m tuyos" 
       android:padding="3dp" 
       android:layout_gravity="center_horizontal" 
       android:maxLines="1" 
       android:gravity="center_horizontal" 
       android:alpha="500" 
       android:background="#46000000" 
       android:textColor="@android:color/white"/> 
     </RelativeLayout> 

     <TextView 
      android:id="@+id/logoTexto" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Probando" 
      android:padding="3dp" 
      android:layout_gravity="center_horizontal" 
      android:maxLines="1" 
      android:gravity="center_horizontal" 
      android:layout_alignBottom="@+id/containerDisco" /> 

    </LinearLayout> 


</LinearLayout> 

Trả lời

9

Thay vì sử dụng đệm, tôi thường đạt được loại này có hiệu lực với một ItemDecoration.

public class PaddingItemDecoration extends RecyclerView.ItemDecoration { 
    private final int size; 

    public PaddingItemDecoration(int size) { 
     this.size = size; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     super.getItemOffsets(outRect, view, parent, state); 

     // Apply offset only to first item 
     if (parent.getChildAdapterPosition(view) == 0) { 
      outRect.left += size; 
     } 
    } 
} 

Sau đó, khi bạn thiết lập của bạn RecyclerView:

int size = ... // Get the offset that you want 
RecyclerView recyclerView = ... 

recyclerView.addItemDecoration(new PaddingItemDecoration(size)); 
+1

làm việc trơn tru. Cảm ơn bạn rất nhiều! Tôi đã không coi đây là một vấn đề Android, nhưng một cái gì đó nhiều khả năng một vấn đề css :) –

+0

Đây là một phiên bản mở rộng cho Kotlin với hỗ trợ RTL: https://gist.github.com/sevar83/1172423f265e1cbb7a04f3d22b12021a – WindRider

+1

hi @ GuillermoLópez , Tôi muốn có hành vi tương tự, ngoại trừ việc tôi có một nút ở bên trái. Làm thế nào chúng ta có thể đạt được điều đó? – Sac

1

Đó là đơn giản hơn nhiều hơn thế, RecyclerView đã có tính năng: chỉ cần đặt android:clipToPadding="false" đến RecyclerView XML, do đó đệm không cắt tắt các mục của bạn.

Vì vậy, ví dụ bạn:

<android.support.v7.widget.RecyclerView 
    android:background="@drawable/gallery_bg" 
    android:id="@+id/frag_home_gallery_recientes" 
    android:scrollbars="none" 
    android:paddingLeft="150dp" 
    android:clipToPadding="false 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
Các vấn đề liên quan