最近收到一個需求,如圖,大家一看,不就是一個簡單的表格嗎,RecyclerView就搞定了
我一開始也是這么想的,但是當我繼續聽下去
需求是左邊黨支部欄目只能上下滑動,之后聯動右邊下方表格一起上下滑動,右邊下方表格滑動,左邊下方表格依然如此
然后右邊上方只能左右滑動,之后聯動右邊下方表格一起左右滑動,右下方滑動,右上同樣一起滑動
然后此時我的內心是崩潰的
收集幾個關鍵點吧
1:右下方部分既能左右滑動,又可以上下滑動
2:左上角就是一個TextView,不動
3:需要對RecyclerView進行聯動
4:ListView應該也可以實現,但是我使用了擴展性更好的RecyclerView
5:RecyclerView不可以既左右滑動,又上下滑動,即他只能支持一個方向
所以基本有思路了
RecyclerView放在ScrollView容器內,上下滑動做RecyclerView聯動
左右滑動做ScrollView聯動
下面看xml layout代碼吧
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:gravity="center_vertical" android:layout_height="40dp"> <LinearLayout android:gravity="center" android:layout_width="120dp" android:background="@color/gray_trans" android:layout_height="match_parent"> <TextView android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:text="黨支部" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <demo.MyScrollView android:id="@+id/scrollView_right_up" android:layout_width="match_parent" android:scrollbars="none" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <demo.MyRecyclerView android:id="@+id/recyclerview_right_up" android:layout_width="match_parent" android:layout_height="40dp"> </demo.MyRecyclerView> </RelativeLayout> </demo.MyScrollView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview_left_bottom" android:layout_width="119dp" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> <View android:background="@color/gray_trans" android:layout_width="1dp" android:layout_height="match_parent"/> <demo.MyScrollView android:id="@+id/scrollView" android:layout_width="wrap_content" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <demo.MyRecyclerView android:id="@+id/recyclerview_right_bottom" android:layout_width="match_parent" android:layout_height="match_parent" > </demo.MyRecyclerView> </RelativeLayout> </demo.MyScrollView> </LinearLayout> </LinearLayout>
其中MyScrollView是重寫的暴露了onScrollChanged 方法,之后我們可以setOnScrollListener對其滑動進行監聽
MyRecyclerView重寫以及為什么要套一層RelativeLayout請看我上一篇文章解釋
然后放出關鍵的聯動滑動代碼吧
public void setOnScrollLowSdk(){ recyclerViewLeftBottom.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { if(recyclerView.getScrollState()!= RecyclerView.SCROLL_STATE_IDLE){ recyclerViewRightBottom.scrollBy(dx, dy); } } }); recyclerViewRightBottom.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { if(recyclerView.getScrollState()!= RecyclerView.SCROLL_STATE_IDLE){ recyclerViewLeftBottom.scrollBy(dx,dy); } } }); scrollViewRB.setOnScrollListener(new MyScrollView.OnScrollListener() { @Override public void onScroll(int scorllX, int scrollY, int oldX, int oldY) { scrollViewRU.scrollTo(scorllX,scrollY); } }); scrollViewRU.setOnScrollListener(new MyScrollView.OnScrollListener() { @Override public void onScroll(int scorllX, int scrollY, int oldX, int oldY) { scrollViewRB.scrollTo(scorllX,scrollY); } }); }
命名的話應該可以看懂RU代表RightUp右上方,RB代表RightBottom右下方
至此就可以了,省去了RecyclerView數據填充以及各種的LayoutManager初始化,這個也不難
就搞定了