2015-07-21 69 views
0

我需要recyclerview將其最後一項滾動到其中間。我的意思是我需要使用LinearLayoutManager的recyclerview的las元素,而不是停止在recyclerview的底部,而是滾動到它的中間。 這是一個簡單的方法來實現這一點?recyclerview中的滾動區域

+0

對不起,但你的問題還不清楚,你是否想要當你到達RecyclerView的最後一個項目時,你會回到RecyclerView的中間項目? –

+0

你可以添加一個空的頁腳查看並手動設置它的高度以匹配最後一項的一半高度?這將實現滾動距離最後一項高度一半的列表的效果。 – Guardanis

回答

0

您可以使用RecyclerView.ItemDecoration在最後一項上放置額外的底部邊距。它最終會看起來像這樣:

public class TopMarginDecoration 
     extends RecyclerView.ItemDecoration { 

    @Override 
    public void getItemOffsets(outRect, view, parent, state) { 

     int position = parent.getChildAdapterPosition(view); 

     if (position + 1 == parent.getAdapter().getItemCount()) { 
      // get height of recyclerview 
      // get height of child view 
      // calculate required space to center the child view 
      outRect.bottom += calculatedSpace; 
     } 
    } 
} 

您可以使用RecyclerView.addItemDecoration(…)在活動或片段綁了一起。

0

final LinearLayoutManager llm = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 
     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
      super.onScrolled(recyclerView, dx, dy); 
      if (recyclerView.getAdapter().getItemCount() == (llm.findLastCompletelyVisibleItemPosition() + 1)) { 
       Toast.makeText(MainActivity.this, "Sholled", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    });