如何知道以前和新的职位? [英] how to know the previous and new positions?
问题描述
我有一个带有水平布局的recyclerview,一次只能看到一个视图:
I have a recyclerview with horizontal layout and only one view is visible at a time:
mRecyclerView = findViewById(R.id.rvmain);
mRecyclerView.setOnFlingListener(null);
final SnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(mRecyclerView);
mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MainActivityRVAdapter(postsModels,MainActivity.this);
mRecyclerView.setAdapter(mAdapter);
使用onScrolllistener,每次我滚动时都想知道开始位置和结束位置.怎么知道
using onScrolllistener, everytime I scroll I want to know the starting position and end position. how can know that
我知道知道新职位将是一件事情:
i know it will be something to know the newposition:
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
// get newstate position
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
View centerView = snapHelper.findSnapView(mLayoutManager);
if(centerView != null){
int pos = mLayoutManager.getPosition(centerView);
Log.e("Snapped Item Position:",""+pos);
}
}
}
在滚动开始之前如何知道位置
how to know the position before scrolling started
然后我想比较两者并做点什么,我应该在哪里使用该代码.
then I want to compare both and do something, where should I use that code.
我已按照所提及的解决方案进行了尝试:
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(count == 0) {
View centerView = snapHelper.findSnapView(mLayoutManager);
if(centerView != null){
initial_position = mLayoutManager.getPosition(centerView);
//initial_position2 = ((LinearLayoutManager)mRecyclerView.getLayoutManager()).findFirstVisibleItemPosition();
Log.e("Initial Item Position:",""+initial_position);
//Log.e("Initial Item Position2:",""+initial_position2);
}
}
count ++;
// get newstate position
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
View centerView = snapHelper.findSnapView(mLayoutManager);
if(centerView != null){
int pos = mLayoutManager.getPosition(centerView);
count = 0; // in idle state clear the count again
Log.e("Snapped Item Position:",""+pos);
}
}
}
我得到的结果是:
E/Initial Item Position:: 0
E/Snapped Item Position:: 1
E/Initial Item Position:: 1
E/Snapped Item Position:: 1
E/Initial Item Position:: 1
E/Snapped Item Position:: 1
E/Initial Item Position:: 1
E/Snapped Item Position:: 1
初始位置的最终值与滚动开始处的值不同
The final value of the initial position is not same as the one at the beginning of the scroll
它返回多次位置.我不想检查所有多个职位的最终职位和初始职位之间的差异.我只想在最后检查一下.
And it returns multiple times positions. I don't want to check the difference between final and initial positions for all the multiple positions. I just want to check at the end.
解决我的问题
正如我所见,即使我滚动一个位置,onScrollListener也会运行4次.我发现没有办法控制它运行多少次,但是我一直想只在它第一次运行时才做一些事情.我正在寻找运行pos!= initial_position的代码.我第一次看到的才是正确的,后来才知道是错误的.
As I saw even if i scroll one poistion the onScrollListener runs 4 times. I found there is no way to control how many times it runs, but i was looking to do something only when it runs first time. I am looking to run a code if pos != initial_position. which i saw is true only for the first time and later its false.
我的问题是,无论何时我要进入与以前的视图都不相同的任何视图时,都要重置字体.
My problem was whenever i land at any view which is not same as the previous view i want to reset the font.
最终代码:
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(count == 0) {
View centerView = snapHelper.findSnapView(mLayoutManager);
if(centerView != null){
initial_position = mLayoutManager.getPosition(centerView);
//initial_position2 = ((LinearLayoutManager)mRecyclerView.getLayoutManager()).findFirstVisibleItemPosition();
Log.e("Initial Item Position:",""+initial_position);
//Log.e("Initial Item Position2:",""+initial_position2);
}
count ++;
}
// get newstate position
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
View centerView = snapHelper.findSnapView(mLayoutManager);
if(centerView != null){
int pos = mLayoutManager.getPosition(centerView);
if(pos != initial_position ){
TextView textView2 = (TextView) centerView.findViewById(R.id.postsmodel_description);
if(textsize == 0){
textsize = textView2.getTextSize();
}
textView2.setTextSize(pixelsToSp(textsize));
}
count = 0; // in idle state clear the count again
Log.e("Snapped Item Position:",""+pos);
}
}
}
推荐答案
int count = 0; //field variable
int initial_position;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(count == 0) {
initial_position = mLayoutManager.findFirstVisibleItemPosition();
}
count ++;
// get newstate position
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
View centerView = snapHelper.findSnapView(mLayoutManager);
if(centerView != null){
int pos = mLayoutManager.getPosition(centerView);
count = 0; // in idle state clear the count again
Log.e("Snapped Item Position:",""+pos);
}
}
}
initial_position
= 起始位置
pos
= 结束位置
快乐编码:)
这篇关于如何知道以前和新的职位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!