Android的:如何获得当前X RecyclerView偏移? [英] Android: How to get the current X offset of RecyclerView?

查看:2111
本文介绍了Android的:如何获得当前X RecyclerView偏移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个滚动型对于无限时间选取旋转木马,并发现了,这是不是最好的方法(<一href="http://stackoverflow.com/questions/26341587/horizontalscrollview-customadapter-with-getview-doesnt-reuse-convertviews-li">last问题)

现在,我发现回收站视图我无法获得当前滚动的recyclerView的X方向的偏移? (比方说,每一项都是100px的宽度,而第二项是唯一可见的50%,所以滚动偏移量为150像素)

  • 在所有项目具有相同的宽度,但therer一定的差距之前,首先,在最后一个项目之后
  • recyclerView.getScrollX()返回 0 (文档说:初始滚动值)
  • 布局管理 findFirstVisibleItemPosition ,但我不能计算出的X轴偏移与

更新

我刚刚找到一种方法来保持跟踪X位置,同时更新与 onScrolled 回调的价值,但我想preFER得到实际的值,而不是跟踪这一切的时候!

 私人INT overallXScrol = 0;
// ...
mRecyclerView.setOnScrollListener(新RecyclerView.OnScrollListener(){
        @覆盖
        公共无效onScrolled(RecyclerView recyclerView,诠释DX,诠释DY){
            super.onScrolled(recyclerView,DX,DY);

            overallXScroll = overallXScroll + DX;

            Log.i(检查,overall-&GT;+ overallXScroll);

        }
    });
 

解决方案

解决方案1: setOnScrollListener

保存的X定位为类变量和更新中的 onScrollListener 每一个变化。确保你不重置 overallXScroll (FE onScreenRotationChange

 私人INT overallXScroll = 0;
 // ...
 mRecyclerView.setOnScrollListener(新RecyclerView.OnScrollListener(){
    @覆盖
    公共无效onScrolled(RecyclerView recyclerView,诠释DX,诠释DY){
        super.onScrolled(recyclerView,DX,DY);

        overallXScroll = overallXScroll + DX;
        Log.i(检查,整体X =+ overallXScroll);

    }
 });
 

解决方案2:计算当前位置

在我的情况,我有一个充满了时间值(0:00,0:30,1:00,1:30 ... 23:00,23:30)水平列表。我计算从时间项,这是在屏幕(计算点)的中间位置的时间。这就是为什么我需要的精确的X滚动位置我 RecycleView

  • 在每次项目有60dp相同的宽度(供参考:60dp = 30分钟,2DP = 1分)
  • 第一项(标题项)有一个微胖,设置0min到中心

     私人INT mScreenWidth = 0;
    私人诠释mHeaderItemWidth = 0;
    私人诠释mCellWidth = 0;
    
    @覆盖
    保护无效的onCreate(包savedInstanceState){
      super.onCreate(savedInstanceState);
    
      //初始化循环意见
      // ...
    
      DisplayMetrics displaymetrics =新DisplayMetrics();
      。this.getWindowManager()getDefaultDisplay()getMetrics(displaymetrics)。
      this.mScreenWidth = displaymetrics.widthPixels;
    
      //计算值在当前设备
      mCellWidth =(INT)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,60,getResources()
            .getDisplayMetrics());
    
      列表中的权利(差距缩小到屏幕的左边,从第一个项目的左侧)//得到补偿
      最终诠释mOffset =(this.mScreenWidth / 2) - (mCellWidth / 2);
    
      // HeaderItem宽度(图形蓝色矩形)
      mHeaderItemWidth = mOffset + mCellWidth;
    
      mRecyclerView.setOnScrollListener(新RecyclerView.OnScrollListener(){
    
        @覆盖
        公共无效onScrolled(RecyclerView recyclerView,诠释DX,诠释DY){
            super.onScrolled(recyclerView,DX,DY);
    
            //获取第一个可见项目
            视图firstVisibleItem = mLLM.findViewByPosition(mLLM.findFirstVisibleItemPosition());
    
            INT leftScrollXCalculated = 0;
            如果(firstItemPosition == 0){
                   //如果第一个项目,得到宽度headerview的(getLeft()℃的,这就是为什么我使用Math.abs())
                leftScrollXCalculated = Math.abs(firstVisibleItem.getLeft());
            }
            其他{
    
                   // X坐标=峡向右+细胞数*宽度 - 当前的第一个可见项目的偏移量电池
                   //(mHeaderItemWidth包括已经宽度一格的,这就是为什么我必须再减去它)
                leftScrollXCalculated =(mHeaderItemWidth  -  mCellWidth)+ firstItemPosition * mCellWidth + firstVisibleItem.getLeft();
            }
    
            Log.i(ASDF,计算出的X要离开=+ leftScrollXCalculated);
    
        }
    });
     

    }

I'm using a Scrollview for an infinite "Time Picker Carousel" and found out, that it is not the best approach (last question)

Now, I found the Recycler View but I am unable to get the current scroll offset in X direction of the recyclerView? (Let's say each item is 100px width, and the second item is only visible to 50%, so the scroll-offset is 150px)

  • all items have the same width, but therer is some gap before the first, after the last item
  • recyclerView.getScrollX() returns 0 (docs say: initial scroll value)
  • the LayoutManager has findFirstVisibleItemPosition, but I cannot calculate the X offset with that

UPDATE

I just found a way to keep tracking the X-Position, while updating the value with the onScrolled callback, but I would prefer getting the actual value instead of tracking it all the time!

private int overallXScrol = 0;
//...
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            overallXScroll = overallXScroll + dx;

            Log.i("check","overall->" + overallXScroll);

        }
    });

解决方案

Solution 1: setOnScrollListener

Save your X-Position as class variable and update each change within the onScrollListener. Ensure you don't reset overallXScroll (f.e. onScreenRotationChange)

 private int overallXScroll = 0;
 //...
 mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        overallXScroll = overallXScroll + dx;
        Log.i("check","overall X  = " + overallXScroll);

    }
 });

Solution 2: Calculate current position.

In my case, I have a horizontal List which is filled with time values (0:00, 0:30, 1:00, 1:30 ... 23:00, 23:30). I'm calculating the time from the time-item, which is in the middle of the screen (calculation point). That's why I need the exact X-Scroll Position of my RecycleView

  • Each time item has the same width of 60dp (fyi: 60dp = 30min, 2dp = 1min)
  • First item (Header item) has an extra padding, to set 0min to the center

    private int mScreenWidth = 0;
    private int mHeaderItemWidth = 0;
    private int mCellWidth = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    
      //init recycle views
      //...
    
      DisplayMetrics displaymetrics = new DisplayMetrics();
      this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
      this.mScreenWidth = displaymetrics.widthPixels;
    
      //calculate value on current device
      mCellWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, getResources()
            .getDisplayMetrics());
    
      //get offset of list to the right (gap to the left of the screen from the left side of first item)
      final int mOffset = (this.mScreenWidth / 2) - (mCellWidth / 2);
    
      //HeaderItem width (blue rectangle in graphic)
      mHeaderItemWidth = mOffset + mCellWidth;
    
      mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
    
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
    
            //get first visible item
            View firstVisibleItem = mLLM.findViewByPosition(mLLM.findFirstVisibleItemPosition());
    
            int leftScrollXCalculated = 0;
            if (firstItemPosition == 0){
                   //if first item, get width of headerview (getLeft() < 0, that's why I Use Math.abs())
                leftScrollXCalculated = Math.abs(firstVisibleItem.getLeft());
            }
            else{
    
                   //X-Position = Gap to the right + Number of cells * width - cell offset of current first visible item
                   //(mHeaderItemWidth includes already width of one cell, that's why I have to subtract it again)
                leftScrollXCalculated = (mHeaderItemWidth - mCellWidth) + firstItemPosition  * mCellWidth + firstVisibleItem.getLeft();
            }
    
            Log.i("asdf","calculated X to left = " + leftScrollXCalculated);
    
        }
    });
    

    }

这篇关于Android的:如何获得当前X RecyclerView偏移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆