簡體   English   中英

Android:recyclerview的水平和垂直滾動

[英]Android: Horizontal and vertical scroll for recyclerview

我試圖同時為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>

誰能告訴我一個主意。

到目前為止,我發現在一個視圖上進行水平和垂直滾動的最佳解決方案是將其放在一個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;
        }
    }

我知道這不能直接回答您的問題(我對回收者視圖知之甚少),但我希望它仍然可以為您提供幫助。 我將其用於切片引擎,並且效果很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM