2016-12-28 43 views
0

我已經嘗試了所有從stackoverflow上的問題的答案,但我仍然無法弄清楚這一點。在我的一個回收站中,點擊一個按鈕後,我可以滾動到所需的位置。在不同的回收站(與第一個回收站類似),我無法滾動到所需的位置。Recyclerview scrollToPosition不起作用

這是recyclerview奏效代碼:

LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); 
layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 

Button button = (Button) view.findViewById(R.id.button2); 
button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     newFeedRecyclerView.smoothScrollToPosition(2); 
    } 
}); 

FeedRecyclerAdapter adapter = new FeedRecyclerAdapter(view.getContext(), allRows); 
newFeedRecyclerView.setHasFixedSize(true); 
newFeedRecyclerView.setLayoutManager(layoutManager); 
newFeedRecyclerView.addItemDecoration(new VerticalSpaceItemDecorator(30)); 
newFeedRecyclerView.setAdapter(adapter); 

這是沒有工作的recyclerview:

allImageRecycleView.addItemDecoration(new VerticalSpaceItemDecorator(30)); 
allImageRecycleView.setNestedScrollingEnabled(false); 
allImageRecycleView.setHasFixedSize(true); 
LinearLayoutManager layoutManager = new >LinearLayoutManagerWithSmoothScroller(getContext()); 

Button button = (Button)view.findViewById(R.id.button3); 
button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     allImageRecycleView.smoothScrollToPosition(2); 
    } 
}); 
layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
allImageRecycleView.setLayoutManager(layoutManager); 

allImageRecycleView.setAdapter(allImageRecyclerAdapter); 

非常感謝你閱讀此。

回答

0

嘗試用這種

layoutManager.scrollToPosition(index); 
+0

感謝您的回答。此解決方案適用於第一個回收站視圖,但不適用於第二個。我一直在試圖弄明白爲什麼。 –

+0

你能幫我嗎? –

0

使用

layoutManager.scrollToPositionWithOffset(int position, int offset) 

scrollToPositionWithOffset(INT位置,詮釋偏移)滾動到與解決佈局 開始定偏移量處 指定的適配器位置。

源:https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html

+0

謝謝你的回答。此解決方案適用於第一個回收站視圖,但不適用於第二個。我一直在試圖弄明白爲什麼。 –

+0

你能幫助我嗎? –

相關問題