Android 如何知道以前和新的职位?

Android 如何知道以前和新的职位?,android,android-recyclerview,onscrolllistener,Android,Android Recyclerview,Onscrolllistener,我有一个水平布局的recyclerview,一次只能看到一个视图: mRecyclerView = findViewById(R.id.rvmain); mRecyclerView.setOnFlingListener(null); final SnapHelper snapHelper = new LinearSnapHelper(); snapHelper.attachToRecyclerView(mRecyclerView); mLayoutManager = new LinearLayo

我有一个水平布局的recyclerview,一次只能看到一个视图:

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,每次滚动时,我都想知道起始位置和结束位置。你怎么知道

我知道了解这个新职位是件好事:

    @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);
            }
        }
    }
如何在滚动开始前知道位置

然后我想比较两者并做一些事情,我应该在哪里使用这些代码

我按照上面提到的解决方案进行了尝试:

@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
初始位置的最终值与滚动开始处的值不同

它返回多次位置。我不想检查所有多个位置的最终位置和初始位置之间的差异。我只想在最后检查一下

我的问题的解决方案

正如我看到的,即使滚动一个位置,onScrollListener也会运行4次。我发现无法控制它运行了多少次,但我希望只在它第一次运行时做一些事情。如果pos!=初始位置。我只是第一次看到它是真的,后来它是假的

我的问题是,每当我在任何与前一个视图不同的视图着陆时,我都想重置字体

最终代码:

    @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);
            }
        }


    }
初始位置
=起始位置

pos
=结束位置


快乐编码:)

我试过了。初始位置=新闻状态。初始位置始终为零。“最终位置正确。@Santhoshyedid更新了答案,请检查.”。。找到最终位置的方法也一样!!当
count==0
时,实际上onScrolllistner会不断显示许多newstate值。最后我只想比较一下。如何做请检查更新后的问题,并尝试解决您所给出的问题。正如您所看到的,即使我滚动一个视图,onScrolllisternet也会运行多次。我发现“E/Initial Item Position::0”后来变成了“E/Initial Item Position::1”。我无法控制停止运行多次。所以我在if(pos!=初始位置)中加入了一个条件,并完成了我的工作。其他时候,它不会在if中运行代码。我已经更新了我的最终代码。
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);
        }
    }
}