简体   繁体   English

RecyclerView 获取第一个可见项的高度

[英]RecyclerView get height of first visible item

How can i calculate the height of the first visible item in my recyclerview ?如何计算recyclerview第一个可见项目的高度?

  public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        switch (newState) {
            case RecyclerView.SCROLL_STATE_IDLE:
                Log.d("scroll_stop","The RecyclerView is not scrolling : ");
                View firstItemView = mGLM.findViewByPosition(mGLM.findFirstVisibleItemPosition());

                View mVizibleView= mPlm.findViewByPosition(mGLM.findFirstVisibleItemPosition());
}
  });

But this give me the full height of the view但这给了我视图的完整高度

Taking the Size of any RecyclerView Item is pointless, If you getHeight() or getWidth() of any item it's just gonna give a Null Error and your app is gonna crash.获取任何 RecyclerView 项目的大小是没有意义的,如果您获取任何项目的getHeight()getWidth() ,它只会给出 Null 错误并且您的应用程序会崩溃。

What you can do, Is to Use your ViewHolder from recyclerView Adapter and First set a Specific Height to any Item you want and Then use that Height somewhere Else.您可以做的是使用您的 ViewHolder 从 recyclerView Adapter 并首先将特定高度设置为您想要的任何项目,然后在其他地方使用该高度。

Look at the Code Below, Maybe helps:看看下面的代码,也许有帮助:

 class myViewHolder extends RecyclerView.ViewHolder { RemaltiveLayout itemLayout; public myViewHolder(View itemView) { super(itemView); itemLayout = (RemaltiveLayout) itemView.findViewById(R.id.myItemLayout); itemLayout.getLayoutParams().height = res.getDimensionPixelSize(R.dimen.height_value); }

You should be able to use item at a specific location position as well您也应该能够在特定位置使用 item

This is not gonna work with Width though, cause items are always being stretched to fill columns of layoutManager不过,这不适用于 Width,因为项目总是被拉伸以填充 layoutManager 的列

Steps to get first item height:获取第一个项目高度的步骤:

  1. Get the layout manager from the recyclerview reference in onScrolled()onScrolled()的 recyclerview 引用中获取布局管理器
  2. Get the first the visible item position using layoutManager.findFirstVisibleItemPosition()使用layoutManager.findFirstVisibleItemPosition()获取第一个可见项位置
  3. If fist visible item position is 0 then get the 0th child height layoutManager.getChildAt(0)如果第一个可见项位置为 0,则获取第 0 个子高度layoutManager.getChildAt(0)

Sample code:示例代码:

RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

        if (layoutManager != null) {
            int child0Height = 0;
            int currentFirstVisibleItem = layoutManager.findFirstVisibleItemPosition();
            if (currentFirstVisibleItem == 0) {
                final View childAt0 = layoutManager.getChildAt(0);
                if (childAt0 != null) {
                    child0Height = childAt0.getHeight();
                }
            }
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM