0

I am trying to implement horizontal and vertical scrolling for a Recycler view at the same time.I have to show a table of 8 columns, so I plan to implement horizontal and vertical scrolling at the same time.

I tried HorizontalScrollView in list row but it is scrolling horizontally in one row. I also tried the HorizontalScrollView as the parent of recyclerview but its not working

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">


<TextView
    android:id="@+id/title"
    android:textColor="@color/title"
    android:textStyle="bold"
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="aaa"/>

<TextView
    android:layout_toRightOf="@+id/title"
    android:id="@+id/genre"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:text="bbb"/>

<TextView
    android:layout_toRightOf="@+id/genre"
    android:id="@+id/year"
    android:textColor="@color/year"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="20dp"
    android:layout_height="wrap_content"
    android:text="ccc"/>

 </RelativeLayout>

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
 </LinearLayout>
 </HorizontalScrollView>
 </LinearLayout>

can any one please tell me an idea.

Binil Surendran
  • 2,524
  • 6
  • 35
  • 58

1 Answers1

0

The best solution i found so far to have horizontal and vertical scrolling on one view is to put it inside one FrameLayout. Then your view must implement OnTouch (you can do this in your activity to) and you just have to scroll it as the touch pointer moves.

Here is an example : xml :

<FrameLayout
 android:id="@+id/TwoWaysScrollableContainer"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:clipToPadding="false">
    <YourView
        android:id="@+id/GridContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false" />
 </FrameLayout>

C# (i use xamarin, but Java wouldn't have so many differencies i think) :

public class MyActivity : Activity
    {
        private MyView _myView; //Can be GridView, relative layout or wathever
        private float _startX, _startY,_prevDx, _prevDy, _dx,_dy;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
        _myView = FindViewById<MyView>(Resource.Id.GridContainer);
        }

        public bool OnTouch(View v, MotionEvent e)
        {
            switch (e.Action)
            {
                case MotionEventActions.Down:
                    {

                        mode = Mode.DRAG;
                        startX = e.GetX() - prevDx;
                        startY = e.GetY() - prevDy;

                        break;
                    }
                 case MotionEventActions.Move:
                    {
                        /* you also could add logic to prevent your view from scrolling out of you grid bounds*/
                        if (mode == Mode.DRAG)
                        {
                            dx = startX - e.GetX();
                            dy = startY - e.GetY();
                            _myView.ScrollBy((int)(dx), (int)(dy));

                            startX = e.GetX();
                            startY = e.GetY();
                        }
                        break;
                    }
               case MotionEventActions.Up:
                    {
                        mode = Mode.NONE;
                        prevDx = dx;
                        prevDy = dy;
                        break;
                    }
              }
              return true;
        }
    }

I know this doesn't answer your question directly ( i don't know much about recycler view), but i hope it can still help you. I used this for a tile engine and it did the trick very well.

yan yankelevich
  • 885
  • 11
  • 25