19

I want to know if there is any possible way to use 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.

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

David Kariuki
  • 508
  • 6
  • 22
Ashkan
  • 1,122
  • 3
  • 12
  • 36

8 Answers8

22

If you want to just scrolling then you can use to NestedScrollView instead of ScrollView So you can modify your code with following :

<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>
Bhunnu Baba
  • 1,554
  • 11
  • 19
13

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!

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:

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.

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

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

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.

Enjoy it

matdev
  • 3,317
  • 4
  • 26
  • 42
Ashkan
  • 1,122
  • 3
  • 12
  • 36
  • i cant figur it out...getting some errors..can you please provide full example with adapter class and recyclerview implementation – H Raval Oct 26 '15 at 07:49
  • @HetalUpadhyay which part you have Error? this week i have not enough time to do this. but if you are not in hUrry I will do it for you – Ashkan Oct 26 '15 at 09:40
  • actually i am not getting any error but when i tried same code with horizontal recycler view it display nothing...so it will be grateful to you if you can provide a code that work fine in both case horizontal and vertical scrolling – H Raval Oct 26 '15 at 09:43
  • @HetalUpadhyay may you explain what do you want to do? because I have some code to calculate how many item should be in a horizontal form with fix width – Ashkan Oct 26 '15 at 09:54
  • @HetalUpadhyay ok, got it. I try to do it on friday. send me an email: ashkan.hematian@gmail.com – Ashkan Oct 26 '15 at 09:58
  • sorry i forgot to mention that i am using GridLayoutManager not Linear – H Raval Oct 26 '15 at 12:17
  • thanx...worked perfectly for me just added android:fillViewport="true" to scrollview and it start working – H Raval Oct 26 '15 at 12:32
  • seems like variable `viewHeight` is never used – hornet2319 May 13 '16 at 13:14
  • This is far more complicated than is necessary.Look at @Bhunnu Baba's answer. – Adam Jun 22 '18 at 21:06
11

Use This line to your recyclerview :

 android:nestedScrollingEnabled="false"

try it ,recyclerview will be smoothly scrolled with flexible height

iDeveloper
  • 1,420
  • 16
  • 43
8

[RESOLVED] I have same issue with Horizontal recycleview. Change Gradle repo for recycleview

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

Write this: linearLayoutManager.setAutoMeasureEnabled(true);

Fixed bugs related to various measure-spec methods in update

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

I have found issue with 23.2.1 library: When item is match_parent recycle view fill full item to view, please always go with min height or "wrap_content".

Thanks

Chintan Khetiya
  • 15,208
  • 9
  • 41
  • 81
Amit
  • 399
  • 1
  • 4
  • 11
7

To fix fling in RecyclerView you must override canScrollVertically in LinearLayoutManager:

linearLayoutManager = new LinearLayoutManager(context) {
 @Override
 public boolean canScrollVertically() {
  return false;
 }
};
Michał Ciołek
  • 309
  • 2
  • 9
  • To add to this answer, you also need to use NestedScrollView this answer alone didn't work on Android 7+ :\ – JustADev Dec 15 '17 at 14:02
2

In case setting fixed height for the RecyclerView didn't work for someone (like me), here is what I've added to the fixed height solution:

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

    }
});

I

John smith
  • 1,671
  • 14
  • 27
  • 1
    I didn't test it yet, but I think your code want to scroll **RecyclerView** on it own. I will try it today. tnx dear friend – Ashkan Sep 07 '15 at 06:43
  • 1
    i didn't help, as I said before it help to scroll RecyclerView on it own with fix height. it's not my goal. – Ashkan Sep 07 '15 at 09:47
2

I fully agree with Ashkan solution, big thanks (voted +1!), but there is one thing...

If RecyclerView does scroll correctly inside ScrollView/NestedScrollView BUT it doesn't fling (intercept it) then the solution is to extend RecyclerView and override OnInterceptTouchEvent and OnTouchEvent. If you don't need any click actions but just want to present items with working fling then simply return false in both like below:

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

Or actually it should be called NonIntercepting... ;) Simple as that.

Darek Deoniziak
  • 682
  • 7
  • 16
1

I solved this problem by using NestedScrollView instead of ScrollView. And for RecyclerView

    <android.support.v7.widget.RecyclerView 
android:layout_width="match_parent"
  android:layout_height="match_parent" />

And in Code

recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
Gsk
  • 2,617
  • 5
  • 21
  • 26
Arun PK
  • 109
  • 8