2016-12-28 46 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

你能帮助我吗? –

相关问题