13

我想知道是否有任何可能的方式使用RecyclerView使用ScrollView裏面的RecyclerView靈活的Recycler項目高度

此之前,我使用RecyclerView與修復高度一個滾動型內,但這個時候,我不知道該項目的高度。

提示:自4天前提出這個問題之前,我已經閱讀了所有關於堆棧問題的問題和解決方案。

更新: 一些解決學習如何滾動RecyclerView他自己的,但我想將此擴大。

+0

您有什麼問題滾動? –

+2

@OmarHassan我應該在** ScrollView **中使用多個** RecyclerView **。只是它 – Ashkan

+0

非上述幫助我......只有這個答案後在這裏:[Recyclerview內滾動視圖](http://stackoverflow.com/a/36979664/4240256) –

回答

11

我搜索爲了回答全世界的這個問題,但我沒有找到任何直接的答案這個受歡迎的問題。最後我混合了一些技巧並解決了這個問題!

我的問題開始時,我的老闆問我使用RecyclerView一個滾動型裏面,你也知道我們不能用裏面兩個彼此滾動對象除了當我們設置或者知道修復項目高度對於我們的RecyclerView。這就是答案:

步驟1: 首先你應該找到你的RecyclerView靈活的項目高度,這是可能的,從你的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 
     } 
    }); 

與此代碼,你找到你物品高度,因爲他們建立併發送物品高度到您的活動界面

對於那些有接口問題的朋友,我留下的接口代碼應該寫在Adapter中。

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

第2步: 我們發現我們的產品高度,到目前爲止,現在我們要高度設定爲我們的RecyclerView。首先我們應該計算物品高度的總和。在這一步,我們通過的BitMap 第一工具最後一步接口做到這一點,下面寫這些代碼的活動片段了定義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高度的總和。對於最後一步,我們應該送我們在最後一步寫一些代碼來完成適配器接口:

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

當你實現接口,你應該把它發送給你與你的情況下,我使用

享受

+0

我不能找出它......得到一些錯誤..你可以提供適配器類和recyclerview實現的完整示例 –

+0

@HetalUpadhyay哪部分你有錯誤?這周我沒有足夠的時間來做這件事。但如果你不在hUrry我會爲你做 – Ashkan

+0

其實我沒有收到任何錯誤,但是當我用水平回收站查看它的同一代碼時,它什麼也沒有顯示......所以如果你能提供一個代碼,它會感激你在水平和垂直滾動兩種情況下工作正常 –

2

如果設置固定的高度爲RecyclerView沒有爲工作的人(比如我),這裏是我已經添加到固定高度的解決方案:

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { 
    @Override 
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 
     int action = e.getAction(); 
     switch (action) { 
     case MotionEvent.ACTION_MOVE: 
      rv.getParent().requestDisallowInterceptTouchEvent(true); 
      break; 
    } 
    return false; 
} 

@Override 
public void onTouchEvent(RecyclerView rv, MotionEvent e) { 

} 

@Override 
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 

    } 
}); 

+1

我還沒有測試過,但我認爲你的代碼想自己滾動** RecyclerView **。我今天會嘗試。 tnx親愛的朋友 – Ashkan

+1

我沒有幫助,正如我之前所說,它有助於滾動RecyclerView自己修復高度。這不是我的目標。 – Ashkan

+0

非常感謝你。只是你的答案幫助我 – neustart47

2

我Ashkan解決方案完全一致,非常感謝(投票+1!),但有一件事...

如果RecyclerView不滾動型/ NestedScrollView內正確地滾動,但它沒有按不要匆忙(攔截它),那麼解決方案就是擴展RecyclerView並覆蓋OnInterceptTouchEvent和OnTouchEvent。如果你不需要任何點擊動作,但只是想提出與工作一扔項目則乾脆在兩個像下面返回false:

public class InterceptingRecyclerView extends RecyclerView { 

    public InterceptingRecyclerView(Context context) { 
     super(context); 
    } 

    public InterceptingRecyclerView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public InterceptingRecyclerView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent e) { 
     return false; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent e) { 
     return false; 
    } 
} 

或實際應該叫NonIntercepting ...;)這麼簡單。

7

[已解決]我有同樣的問題與水平recycleview。 變化搖籃回購爲recycleview

compile 'com.android.support:recyclerview-v7:23.2.1' 

這樣寫:linearLayoutManager.setAutoMeasureEnabled(true);

在更新

檢查http://developer.android.com/intl/es/tools/support-library/features.html#v7-recyclerview

我發現問題23.2各種相關措施,規範方法,修正錯誤。1個庫: 當產品match_parent回收視圖補全項查看,請隨時與min height or "wrap_content".

感謝

6

去要解決甩在RecyclerView必須覆蓋canScrollVerticallyLinearLayoutManager

linearLayoutManager = new LinearLayoutManager(context) { 
@Override 
public boolean canScrollVertically() { 
    return false; 
} 
}; 
+0

要添加到此答案,您還需要使用NestedScrollView這個答案本身不適用於Android 7+:\ – OmarBizreh

1

如果你想只是滾動然後你可以使用NestedScrollView而不是ScrollView所以,你可以用下面的修改代碼:

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

      //design your content here with RecyclerView 

    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 
3

使用此行到您的recyclerview:

 android:nestedScrollingEnabled="false" 

試試吧,recyclerview會順利通過靈活高度

相關問題