在我的android应用程序中,我使用recyclerview和ScrollView创建了时间表结构。下面是我的时间表。
在recyclerview中,周期表既是侧面标题,又是日期列表是最高标题。下面是我的代码。
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);
}
}
}
在这里,当我水平移动日列表标题时,时间表也会随之滚动,反之亦然,并且它不会隐藏以下时间段列表。
但是当我垂直滚动时段列表或时间表时,它将隐藏或滚动日列表标题。是否有任何方法可以使时间表或时段列表垂直时固定或可见日列表。
更新:
最后,我注意到的一件事是onScrollChanged()函数仅适用于水平滚动视图(即.ObservableHorizontalScrollView),而不适用于垂直滚动视图(即.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);
}
}