2017-08-07 68 views
0

這裏有幾個相關的帖子,但沒有一個是完全相同的情況/這些解決方案都沒有爲我工作。Android - RecyclerView開始在中間而不是頂部

我有一個RecyclerView,可以從Firebase中加載一些數據。我正在使用自定義適配器。問題是當視圖加載時,它會在RecyclerView的中間加載。然後我將不得不手動滾動到頂部。

我的佈局非常簡單:

<LinearLayout 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" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:background="@color/grey" 
      android:layout_marginBottom="50dp" 
      > 

<android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/recycler_view_feed" 
     android:layout_marginTop="10dp" 
> 

</android.support.v7.widget.RecyclerView> 


</LinearLayout> 

從火力地堡我的自定義對象加載後,我設置了這樣的RecyclerView(這是一種方法是從onCreateView(這是一個片段)調用):

CompleteWorkoutRecyclerAdapter adapter = new CompleteWorkoutRecyclerAdapter(list, getContext(), 
           getActivity()); 
mRecyclerView.setAdapter(adapter); 
mRecyclerView.setHasFixedSize(false); 
linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true); 
mRecyclerView.setLayoutManager(linearLayoutManager); 

我已經試過這些東西來解決這個問題,沒有運氣:

linearLayoutManager.scrollToPosition(0); 

mRecyclerView.scrollToPosition(0); 

android:focusableInTouchMode="true" 

感謝您的閱讀。如果你們需要更多的代碼,我可以張貼任何你需要

+0

什麼是列表的大小? –

回答

2

首先將您的xml更改爲此

<android.support.v7.widget.RecyclerView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/recycler_view_feed" 
    android:layout_marginTop="10dp"> 

然後將代碼在您的適配器的排佈置的

mRecyclerView.setHasFixedSize(false); 
linearLayoutManager = new LinearLayoutManager(getActivity()); 
mRecyclerView.setLayoutManager(linearLayoutManager); 
CompleteWorkoutRecyclerAdapter adapter = new CompleteWorkoutRecyclerAdapter(list, getContext(), 
           getActivity()); 
mRecyclerView.setAdapter(adapter); 

變化高度WRAP_CONTENT

+0

非常感謝,這固定了它。我想這與您調用佈局管理器/適配器的順序有關係嗎? – theRealOne

0

使用此代碼:

 CompleteWorkoutRecyclerAdapter adapter = new CompleteWorkoutRecyclerAdapter(list, getContext(), 
            getActivity()); 

    linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false); 
    mRecyclerView.setLayoutManager(linearLayoutManager); 
mRecyclerView.setAdapter(adapter); 

希望這將幫助你..

0

使用relative layout而不是linear layout

<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" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/grey" 
      android:layout_marginBottom="50dp" 
      > 

<android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/recycler_view_feed" 
     android:layout_marginTop="10dp" 
     android:layout_alignParentTop="true"> 

</android.support.v7.widget.RecyclerView> 


</RelativeLayout> 
相關問題