I want to know if there is any possible way to use RecyclerView?
Before this, I used RecyclerView with fixed height inside a
To fix fling in RecyclerView
you must override canScrollVertically
in LinearLayoutManager
:
linearLayoutManager = new LinearLayoutManager(context) {
@Override
public boolean canScrollVertically() {
return false;
}
};
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
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);
Use This line to your recyclerview :
android:nestedScrollingEnabled="false"
try it ,recyclerview will be smoothly scrolled with flexible height
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>
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.