2017-04-20 102 views
0

我有一個包含多個項目的回收站。我需要顯示從底部開始的項目,即第一項應顯示在底部,在從底部到頂部滾動後,將顯示更多項目。 爲此,我嘗試的是,Android回收視圖 - 在底部顯示第一項並從下往上滾動顯示下一個項目

linearlayoutManager.setStackFromEnd(true); 

這有助於從底部開始項目。但是,如果有多個項目,比同時放入屏幕的尺寸大,則隨着項目數量的增加,第一個項目會在頂部移動。

我也

linearlayoutManager.scrollToPosition(0); 

這種嘗試並沒有改變任何東西。

這是顯示我在找什麼的圖像。

enter image description here

有列表中的數個項目以上這仍然是不可見的。

這裏的外觀當這些項目滾動時, enter image description here

我已經在圖像中檢查了此視圖的視圖層次結構,其中只顯示了回收視圖和其中的項目。 recyclerview的高度是match_parent,即沒有任何項目的頂部空間顯示在GOOGLE地圖回收站下方的另一個視圖。 我只需要知道如何在不使用更多額外視圖的情況下實現recyclerview的此功能。

任何幫助將不勝感激..!

回答

0

寫這條線在XML中從底部

android:stackFromBottom="true" 

<android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:stackFromBottom="true"> 

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

啓動列表,第二件事扭轉你的數組列表前加上適配器

+0

由於亞太區首席技術官Matt。它是stackFromEnd,用於爲recyclerview佈局管理器從底部添加項目。我已經嘗試過,也用於反轉arraylist,但不管怎樣,列表項都被填滿,並且項目顯示在屏幕上。 –

1

嘗試使用逆轉LinearLayoutManager

LinearLayoutManager linearlayoutManager = new LinearLayoutManager(context, VERTICAL, true); 
1

嘗試初始化您的線性佈局如下

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, true); 

third boolean表示它應該反轉項目或不反轉。 但請記住,您的數據也應該從最新到最舊排序。

+0

感謝您的回覆。我試過你的代碼行。但它不符合我的期望。一旦啓動,我需要首先在底部獲得第一個項目,然後它應該滾動到頂部以供下一個項目使用。 –

+0

你是什麼意思「滾動到頂部的下一個項目」? –

+1

請通過我在問題中發佈的圖像。第一張圖片顯示了recyclerview的初始狀態,僅顯示多個項目中的1-2個。將這些項目拖動/滾動到頂部後,會顯示列表中的更多項目。 –

1

這些屬性添加到您的recyclerview(XML)

android:paddingTop="500dp"   
android:clipToPadding="false" 

您還可以設置它在這樣的代碼:

recyclerView.setPadding(0, 500, 0, 0); 
recyclerView.setClipToPadding(false); 

這是針對不同屏幕尺寸

int height = recyclerView.getHeight() 
recyclerView.setPadding(0, height - 200, 0, 0); 
recyclerView.setClipToPadding(false); 

如果您不知道如何獲取屏幕寬度,高度並將dp轉換爲px

所以這裏是鏈接:How to get screen width and heightConverting pixels to dp

0

好像你想要的是在你的回收站適配器單獨視圖類型,這並不表明什麼,但就是地圖的高度。這將允許您從列表開始的底部向上滾動,並繼續看到不同的視圖。

一個簡單的例子:

public class ScreenBottomRecyclerAdapter extends RecyclerView.Adapter { 
    final int VIEW_TYPE_MAP = 0; 
    final int VIEW_TYPE_ROW = 1; 

    @Override 
    public int getItemCount() { 
     return dataset.length + 1; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     if (position == 0) { 
      return VIEW_TYPE_MAP; 
     } else { 
      return VIEW_TYPE_ROW; 
     } 
    } 

    @Override 
    public RecyclerView.ViewHolder createViewHolder(ViewGroup parent, int viewType) { 
     switch (viewType) { 
      case VIEW_TYPE_MAP: return new ViewHolder(null); // This is an empty recycler row so you can see and interact with the map, it will need to be the height of the map so you'll probably want to make a new view type that the same height but doesn't show anything and doesn't intercept touches 
      case VIEW_TYPE_ROW: return new RowViewHolder(someInflatedView); // This will be the rows you are already creating 
     } 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     switch (getItemViewType(position)) { 
      case VIEW_TYPE_ROW: holder.update(position - 1) // Do your normal update here, but remember that your data will be off by 1 for the map view row in the recycler 
     } 
    } 
} 
相關問題