5

Tôi đang cố gắng tạo RelativeLayout có CoordinatorLayout và LinearLayout ở phía dưới và tìm thấy một số hành vi lạ mà tôi không thể giải quyết. Đây là cách bố trí của tôiRecycleview trong coordinatorlayout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/sender" 
     android:background="@android:color/white" 
     android:fitsSystemWindows="true"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?actionBarSize" 
       android:background="?colorPrimary" 
       app:layout_scrollFlags="scroll|enterAlways" /> 

     </android.support.design.widget.AppBarLayout> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/messages_list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    </android.support.design.widget.CoordinatorLayout> 

    <LinearLayout 
     android:id="@+id/sender" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:gravity="center_vertical" 
     android:background="@android:color/white"> 

     <EditText 
      android:id="@+id/inputText" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:inputType="text" /> 

     <Button 
      android:fontFamily="sans-serif" 
      style="?android:attr/borderlessButtonStyle" 
      android:textAppearance="?android:textAppearanceButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/send" 
      android:textColor="?colorPrimary" 
      android:id="@+id/send"/> 

    </LinearLayout> 

</RelativeLayout> 

Sau khi thay đổi dữ liệu trong bộ chuyển đổi tôi cố gắng để di chuyển đến yếu tố cuối cùng (ví dụ bằng cách recyclerView.smoothScrollToPosition (size);) và tất cả tôi thấy là một phần của quan điểm cuối cùng (kích thước không đầy đủ). Nếu recycleview không lồng vào CoordinatorLayout - tất cả đều hoạt động như mong đợi - tôi thấy toàn bộ kích thước phần tử cuối cùng. Làm cách nào tôi có thể thay đổi bố cục để hoạt động chính xác?

+0

gì là kỳ vọng và những gì không hoạt động ..? – Psypher

+0

Tôi mong rằng sau khi cuộn đến vị trí cuối cùng tôi sẽ thấy lần xem cuối cùng không bị cắt. Ngay bây giờ tôi chỉ nhìn thấy một phần của xem (có vẻ như nó không được cuộn xuống dưới cùng của xem cuối cùng - chỉ là một phần rất ít của xem có thể nhìn thấy). Nếu recycleview di chuyển ra khỏi CoordinatorLayout tất cả hoạt động chính xác - scrollToPosition hoạt động như tôi mong đợi - xem lần cuối có thể nhìn thấy mà không cần cắt bất kỳ thứ gì. Nhưng tôi hy vọng nó sẽ cùng một hành vi trong CoordinatorLayout quá – dmtrlbdv

+0

xin lỗi, quên thêm. Tôi đang sử dụng LinearLayoutManager và com.android.support:design:22.2.0 – dmtrlbdv

Trả lời

0

trong mã của bạn loại bỏ app:layout_scrollFlags="scroll|enterAlways" từ Toolbar và thay vào đó thêm nó vào AppBarLayout vì vậy nó phải như thế này

<android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_scrollFlags="scroll|enterAlways"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?actionBarSize" 
      android:background="?colorPrimary"/> 

</android.support.design.widget.AppBarLayout> 
0

Hãy thử cách này:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginBottom="16dp"> 

Điểm chính là sử dụng để thêm android: layout_marginBottom đến hoạt động của bạn

0

Yếu tố cuối cùng là cắt bớt vì RecyclerView không phải là hoàn toàn có thể nhìn thấy trên màn hình. Nó đang được đẩy xuống bởi AppBar mở rộng. Lưu ý rằng khi cuộn theo cách thủ công, AppBar sẽ thu nhỏ khi bạn đến phần tử cuối cùng.

gì làm việc tốt nhất trong trường hợp của tôi là chỉ đơn giản là thu hẹp các AppBar trước khi cuộn:

AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appbar); 
appBarLayout.setExpanded(false, false); 
recyclerView.smoothScrollToPosition(position); 

Tôi tưởng tượng bạn có thể cải thiện về giải pháp này bằng cách sụp đổ nó chỉ khi cần thiết.

Nếu không thu gọn được AppBar, bạn có thể thêm phần đệm dưới cùng chiều cao như AppBar mở rộng. Tuy nhiên, sẽ có trục trặc khác (ví dụ như khi di chuyển đến một vị trí mà đã có trong RecyclerView, nhưng chỉ bên ngoài của màn hình)

0

Issue dường như là smoothScrollToPosition() sẽ âm thầm di chuyển RecyclerView mà không để cho CoordinatorLayout về cuộn là xảy ra . Đây là những gì tôi nghĩ ra. Điều tốt đẹp về nó là nó chỉ nên cuộn AppBarLayout nếu bạn có đủ các mục trong Bộ điều hợp.

final AppBarLayout layout = (AppBarLayout) findViewById(R.id.appbar); 
    layout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { 
     @Override 
     public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { 
      lastVerticalOffset = verticalOffset; 
     } 
    }); 
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 
     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
      super.onScrolled(recyclerView, dx, dy); 
      if (tryCollapseAppbarOnNextScroll && lastVerticalOffset != -layout.getTotalScrollRange()) { 
       layout.setExpanded(false); 
       tryCollapseAppbarOnNextScroll = false; 
      } 
     } 
    }); 

Bây giờ bất cứ khi nào bạn gửi một tin nhắn, thực hiện điều này:

tryCollapseAppbarOnNextScroll = true; 
recyclerView.smoothScrollToPosition(adapter.getItemCount()-1); 
Các vấn đề liên quan