Android:用于recyclerview的水平和垂直滚动

时间:2016-06-24 09:20:51

标签: android android-recyclerview horizontalscrollview vertical-scroll

我正在尝试同时为Recycler视图实现水平和垂直滚动。我必须显示一个包含8列的表,所以我打算同时实现水平和垂直滚动。

我在列表行中尝试了 Horizo​​ntalScrollView ,但它在一行中水平滚动。 我还尝试 Horizo​​ntalScrollView 作为 recyclerview 的父级,但它无法正常工作

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>

任何人都可以告诉我一个想法。

1 个答案:

答案 0 :(得分:0)

到目前为止,我发现在一个视图上进行水平和垂直滚动的最佳解决方案是将其放在一个FrameLayout中。然后你的视图必须实现OnTouch(你可以在你的活动中执行此操作),你只需在触摸指针移动时滚动它。

这是一个例子: 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#(我使用xamarin,但我认为Java不会有这么多差异) :

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

我知道这并没有直接回答你的问题(我不太了解回收商的观点),但我希望它仍然可以帮助你。我用它作为一个瓦片引擎,它做得非常好。

相关问题