使用RecyclerView内滚动型与灵活的回收站项目高度回收站、灵活、高度、项目

2023-09-13 01:17:29 作者:Sunshine&明眸

我想知道有没有用任何可能的方式 RecyclerView

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

在这之前我用RecyclerView是修复高度在滚动型,但这个时候,我不知道该项目的高度。

before this I use RecyclerView with fix height inside a ScrollView but this time i don't know height of the item.

提示:我读了所有的问题和堆栈问题解决之前问这个问题,因为3天前

Hint: I read all question and solution of stack question before ask this question since 4 days ago.

更新: 一些解决方案,学习如何滚动 RecyclerView 在他自己的,但我想告诉这个扩展。

update: some solution learn how to scroll RecyclerView on his own but i want to show this expanded.

推荐答案

我寻找答案这个问题到处词,但我din't发现那位这个流行问题的任何正面回答。最后,我把一些伎俩在一起,解决这个问题!

I search for answer this question all over the word but I din't fount any direct answer for this popular question. At last I mix some trick together and solve this problem!

我的问题开始的时候,我的嘘声问我使用的 RecyclerView 在滚动型,然后因为你知道我们不能用丝束的滚动里面互相对象时,我们设置或不知道的修复项目高度作为我们的除了RecyclerView 。这就是答案:

My problem start when my boos ask me to use a RecyclerView inside a ScrollView, and as you know we can not use tow Scrollable object 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
        }
    });

这丝毫code,你发现你的项目的高度,他们建立和发送该项目的高度,以您的活动丝毫 inteface 。

对于那些朋友谁有问题的接口,我住的接口code以下,应该在适配器写。

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

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

第二步: 我们发现我们的项目的高度,到目前为止,现在我们要设置的高度为我们RecyclerView。首先要计算的项目高度的总和。在这一步中,我们做到这一点,的BitMap 第一次的工具最后一步接口并写这些code以下您的活动里面或片段定义你的< STRONG> RecyclerView

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

第三步: 现在我们有你RecyclerView高度的总和。对于最后一步,我们应该只是给我们写在最后一步适配器一些code这样的接口:

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

在执行的界面,你应该用你的背景下,我用发送到您的这

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

享受它

 
精彩推荐
图片推荐