Android: как одновременно перемещать ScrollView с помощью Recyclelerview - PullRequest
0 голосов
/ 02 марта 2019

В моем приложении для Android я создал структуру расписания, используя утилиту восстановления и ScrollView. Ниже мое расписание.enter image description here

Здесь в представлении реселлера есть как список периодов в качестве бокового заголовка, так и список дней в качестве верхнего заголовка. Ниже приведен мой код.

xml:

<?xml version="1.0" encoding="utf-8"?>
            <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent"
              xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:fab="http://schemas.android.com/apk/res-auto" xmlns:local="http://schemas.android.com/apk/res-auto">
                <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent">
                    <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior">
                        <LinearLayout android:orientation="vertical" android:paddingTop="10.0dip" android:layout_width="match_parent" android:layout_height="match_parent">
                            <android.support.v7.widget.CardView android:id="@+id/cardVwSection" android:background="@color/SkyBlue" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16.0dip" android:layout_marginRight="16.0dip" android:layout_marginBottom="5.0dip">
                                <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content">
                                    <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60.0dip">
                                        <TextView android:textSize="16.0sp" android:textStyle="bold" android:gravity="center" android:layout_gravity="center" android:id="@+id/txtOdd" android:background="@color/Category_iconEven" android:focusable="false" android:layout_width="90.0dip" android:layout_height="match_parent" android:text="Period" />
                                        <com.pro.sample.view.ObservableHorizontalScrollView android:id="@+id/htlScrVwB" android:layout_width="wrap_content" android:layout_height="match_parent">
                                            <LinearLayout android:orientation="horizontal" android:focusableInTouchMode="true" android:layout_width="wrap_content" android:layout_height="match_parent">
                                                <TextView android:textSize="16.0sp" android:textStyle="bold" android:gravity="center" android:layout_gravity="center" android:id="@+id/txtMonday" android:background="@color/Category_iconOdd" android:focusable="false" android:layout_width="90.0dip" android:layout_height="match_parent" android:text="Monday" />
                                                <TextView android:textSize="16.0sp" android:textStyle="bold" android:gravity="center" android:layout_gravity="center" android:id="@+id/txtTuesday" android:background="@color/Category_iconEven" android:focusable="false" android:layout_width="90.0dip" android:layout_height="match_parent" android:text="Tuesday" />
                                                <TextView android:textSize="16.0sp" android:textStyle="bold" android:gravity="center" android:layout_gravity="center" android:id="@+id/txtWednessDay" android:background="@color/Category_iconOdd" android:focusable="false" android:layout_width="90.0dip" android:layout_height="match_parent" android:text="WednessDay" />
                                                <TextView android:textSize="16.0sp" android:textStyle="bold" android:gravity="center" android:layout_gravity="center" android:id="@+id/txtThursday" android:background="@color/Category_iconEven" android:focusable="false" android:layout_width="90.0dip" android:layout_height="match_parent" android:text="Thursday" />
                                                <TextView android:textSize="16.0sp" android:textStyle="bold" android:gravity="center" android:layout_gravity="center" android:id="@+id/txtFriday" android:background="@color/Category_iconOdd" android:focusable="false" android:layout_width="90.0dip" android:layout_height="match_parent" android:text="Friday" />
                                                <TextView android:textSize="16.0sp" android:textStyle="bold" android:gravity="center" android:layout_gravity="center" android:id="@+id/txtSaturday" android:background="@color/Category_iconEven" android:focusable="false" android:layout_width="90.0dip" android:layout_height="match_parent" android:text="Saturday" />
                                                <TextView android:textSize="16.0sp" android:textStyle="bold" android:gravity="center" android:layout_gravity="center" android:id="@+id/txtSunday" android:background="@color/Category_iconOdd" android:focusable="false" android:layout_width="90.0dip" android:layout_height="match_parent" android:text="Sunday" />
                                            </LinearLayout>
                                        </com.pro.sample.view.ObservableHorizontalScrollView>
                                    </LinearLayout>
                                    <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="800.0dip">
                                        <com.pro.sample.view.ObservableScrollView android:id="@+id/scrVwC" android:layout_width="90.0dip" android:layout_height="wrap_content">
                                            <android.support.v7.widget.RecyclerView android:gravity="center_horizontal" android:id="@+id/rcyPeriod" android:background="@color/white" android:padding="1.0dip" android:scrollbars="none" android:layout_width="wrap_content" android:layout_height="800.0dip" android:horizontalSpacing="1.0dip" android:verticalSpacing="1.0dip" android:stretchMode="columnWidth" android:numColumns="1" android:adjustViewBounds="true" />
                                        </com.pro.sample.view.ObservableScrollView>
                                        <com.pro.sample.view.ObservableScrollView android:id="@+id/scrVwD" android:layout_width="wrap_content" android:layout_height="wrap_content">
                                            <com.pro.sample.view.ObservableHorizontalScrollView android:id="@+id/htlScrVwD" android:layout_width="wrap_content" android:layout_height="match_parent">
                                                <android.support.v7.widget.RecyclerView android:gravity="center_horizontal" android:id="@+id/rcySubject" android:background="@color/white" android:padding="1.0dip" android:scrollbars="none" android:layout_width="wrap_content" android:layout_height="800.0dip" android:horizontalSpacing="1.0dip" android:verticalSpacing="1.0dip" android:stretchMode="columnWidth" android:numColumns="7" android:adjustViewBounds="true" />
                                            </com.pro.sample.view.ObservableHorizontalScrollView>
                                        </com.pro.sample.view.ObservableScrollView>
                                    </LinearLayout>
                                </LinearLayout>
                            </android.support.v7.widget.CardView>
                        </LinearLayout>
                    </android.support.v4.widget.NestedScrollView>
                </android.support.design.widget.CoordinatorLayout>
            </android.support.v4.widget.DrawerLayout>

Main.java:

public class ManageTimeTable extends AppCompatActivity implements ScrollViewListener, HorizontalScrollViewListener {

ObservableHorizontalScrollView htlScrVwB,htlScrVwD;
ObservableScrollView scrVwC,scrVwD;

RecyclerView rcySubject,rcyPeriod;


private LinearLayoutManager mLayoutManager1,mLayoutManager2;

    private float mx, my;
    private float curX, curY;
    int dayPos = 0, pos, classPos = 0, subPos = 0, examPos = 0, perPos = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manage_time_table);


        htlScrVwB = findViewById(R.id.htlScrVwB);
        htlScrVwD = findViewById(R.id.htlScrVwD);
        scrVwC = findViewById(R.id.scrVwC);
        scrVwD = findViewById(R.id.scrVwD);
        rcySubject = findViewById(R.id.rcySubject);
        rcyPeriod = findViewById(R.id.rcyPeriod);

        htlScrVwB.setTag("hb");
        htlScrVwD.setTag("hd");

        scrVwC.setTag("sc");
        scrVwD.setTag("sd");

        scrVwC.setScrollViewListener(this);
        scrVwD.setScrollViewListener(this);
        htlScrVwB.setScrollViewListener(this);
        htlScrVwD.setScrollViewListener(this);

        mLayoutManager1 = new LinearLayoutManager(this);
        rcySubject.setLayoutManager(mLayoutManager1);
        mLayoutManager2 = new LinearLayoutManager(this);
        rcyPeriod.setLayoutManager(mLayoutManager2);

        if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            rcySubject.setLayoutManager(new GridLayoutManager(this, 7));
            rcyPeriod.setLayoutManager(new GridLayoutManager(this, 1));
        } else {
            rcySubject.setLayoutManager(new GridLayoutManager(this, 7));
            rcyPeriod.setLayoutManager(new GridLayoutManager(this, 1));
        }

        dayList.add("Monday");
        dayList.add("Tuesday");
        dayList.add("Wednesday");
        dayList.add("Thursday");
        dayList.add("Friday");
        dayList.add("Saturday");
        dayList.add("Sunday");

        }

        public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
            if (scrollView == this.scrVwC) {
                this.scrVwD.scrollTo(x, y);
            } else if (scrollView == this.scrVwD) {
                this.scrVwC.scrollTo(x, y);
            }
        }

        public void onScrollChanged(ObservableHorizontalScrollView scrollView, int x, int y, int oldx, int oldy) {
            if (scrollView == this.htlScrVwB) {
                this.htlScrVwD.scrollTo(x, y);
            } else if (scrollView == this.htlScrVwD) {
                this.htlScrVwB.scrollTo(x, y);
            }
        }
    }

    public class ObservableScrollView extends ScrollView {
    private ScrollViewListener scrollViewListener = null;

    public ObservableScrollView(Context context) {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (this.scrollViewListener != null) {
            this.scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }
    }




    public class ObservableHorizontalScrollView extends HorizontalScrollView {
    private HorizontalScrollViewListener scrollViewListener = null;

    public ObservableHorizontalScrollView(Context context) {
        super(context);
    }

    public ObservableHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ObservableHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ObservableHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setScrollViewListener(HorizontalScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (this.scrollViewListener != null) {
            this.scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }
}

Здесь, когда я перемещаю заголовок списка по горизонтали, расписание также прокручивается вместе с ним, и наоборот, и не будет скрывать список периодов, как показано ниже.enter image description here

Но когда я прокручиваю список периодов или расписание по вертикали, он скрывает или прокручивает заголовок списка вместе с ним. Есть ли способ сделать список дня фиксированным или видимымкогда расписание или периодический список расположены вертикально.

Обновление:

Наконец, одна вещь, которую я заметил, заключается в том, что функция onScrollChanged () работает только для горизонтального просмотра прокрутки (т. е. .ObservableHorizontScrollView) и не работаетдля вертикальной прокрутки (т.е. .ObservableScrollView). Ниже указана функция, которая не работает. Я думаю, что она не работает из-за родительского NesstedScrollview.

public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (scrollView == this.scrVwC) {
            this.scrVwD.scrollTo(x, y);
        } else if (scrollView == this.scrVwD) {
            this.scrVwC.scrollTo(x, y);
        }
    }
...