在 ScrollView 内使用 RecyclerView 并具有灵活的 Recycler 项目高度 [英] Use RecyclerView inside ScrollView with flexible Recycler item height

查看:24
本文介绍了在 ScrollView 内使用 RecyclerView 并具有灵活的 Recycler 项目高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何可能的方式使用RecyclerView?

I want to know if there is any possible way to use RecyclerView?

在此之前,我在 ScrollView 中使用了具有 固定高度 的 RecyclerView,但这次我不知道项目的高度.

Before this, I used RecyclerView with fixed height inside a ScrollView but this time I don't know the height of the item.

提示:在提出这个问题之前,我阅读了有关堆栈问题的所有问题和解决方案.

Hint: I read all question and solution on stack question before asking this question.

更新:一些解决方案显示了如何自行滚动 RecyclerView,但我想显示它展开.

update: Some solution show how to scroll RecyclerView on its own but I want to show it expanded.

推荐答案

我在世界各地寻找这个问题的答案,但我没有找到这个热门问题的任何直接答案.最后我混合了一些技巧,解决了这个问题!

I search for answer this question all over the world but I didn't found any direct answer for this popular question. At last I mixed some trick together and solved this problem!

当我的老板让我在 ScrollView 中使用 RecyclerView 时,我的问题就开始了,正如您所知,我们不能使用两个 Scrollable 对象除非我们为 RecyclerView 设置或知道固定项目高度.这就是答案:

My problem start when my boss asked me to use a RecyclerView inside a ScrollView, and as you know we cannot use two Scrollable objects inside each other except when we set or know fix item height for our RecyclerView. and this is the answer:

第 1 步:首先你应该找到你的 RecyclerView 灵活的项目高度,这可以从你的 RecyclerView>onBindViewHolder

Step 1: at first you should find your RecyclerView flexible item height and this is possible from your RecyclerView>onBindViewHolder

holder.itemView.post(new Runnable() {
        @Override
        public void run() {

            int cellWidth = holder.itemView.getWidth();// this will give you cell width dynamically
            int cellHeight = holder.itemView.getHeight();// this will give you cell height dynamically

            dynamicHeight.HeightChange(position, cellHeight); //call your iterface hear
        }
    });

使用此代码,您可以在他们构建时找到您的项目高度,并使用界面将项目高度发送到您的活动.

with this code you find your item height as them build and send the item height to your activity with interface.

对于接口有问题的朋友,我在下面留下接口代码,应该写在Adapter中.

for those friend who have problem with interface i leave interface code below that should write in Adapter.

public interface DynamicHeight {
    void HeightChange (int position, int height);
}

第 2 步:到目前为止,我们找到了我们的项目高度,现在我们想为 RecyclerView 设置高度.首先我们应该计算项目高度的总和.在这一步中,我们通过BitMap首先实现最后一步接口,并在定义您的RecyclerView的ActivityFragment中编写下面这些代码:

Step 2: we found our item height so far and now we want to set height to our RecyclerView. first we should calculate the Summation of item height. in this step we do this by BitMap first implements last step interface and write these code below inside your Activity or Fragment that define your RecyclerView:

@Override
public void HeightChange(int position, int height) {
    itemHeight.put(position, height);
    sumHeight = SumHashItem (itemHeight);

    float density = activity.getResources().getDisplayMetrics().density;
    float viewHeight = sumHeight * density;
    review_recyclerView.getLayoutParams().height = (int) sumHeight;

    int i = review_recyclerView.getLayoutParams().height;
}

int SumHashItem (HashMap<Integer, Integer> hashMap) {
    int sum = 0;

    for(Map.Entry<Integer, Integer> myItem: hashMap.entrySet())  {
        sum += myItem.getValue();
    }

    return sum;
}

第 3 步:现在我们有了 RecyclerView 高度的总和.对于最后一步,我们应该将我们在最后一步中编写的接口发送到适配器,并使用如下代码:

Step 3: now we have the summation of your RecyclerView height. for last step we should just send the Interface that we write in last step to adapter with some code like this:

reviewRecyclerAdapter = new ReviewRecyclerAdapter(activity, reviewList, review_recyclerView, this);

当您实现接口时,您应该将它与我使用this的上下文一起发送给您.

when you implement interface, you should send it to your with your context that I use this.

享受吧

这篇关于在 ScrollView 内使用 RecyclerView 并具有灵活的 Recycler 项目高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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