Original link: https://mp.weixin.qq.com/s/L3o2i3WTmg1ScXEYDS8YCg
In the previous article Android implementation Anchor Point positioning, we introduced the tablayout + scrollView implementation of the anchor point positioning, today we use tablayout + recyclerView to achieve the same effect.
:
Implementation ideas
The idea of implementation is consistent with the previous article:
1, the Monitoring recyclerView slide to the position, tablayout switch to the corresponding label
2, tablayout each tab click, recyclerView can slide to the corresponding area
Data simulation
Data simulation, using the previous article AnchorView as a recyclerView per-word view, while here the recyclerView last sub-view of the height of the modification, so that it fills the screen.
Private Linearlayoutmanager manager;private string[] tabtxt = {"Living room", "bedroom", "Restaurant", "Den", "Balcony", "Children Room"};//interpretation is recyclerview active Induced slip, true-Yes, false-No, private Boolean isrecyclerscroll;//caused by tablayout record the last position, preventing sliding in the same content block repeatedly positioning to tablayoutprivate int lastpos;//used to slide Recyclerview to the specified location private boolean canscroll;private int scrolltoposition;//tablayout set label for (int i = 0; i < Tabtxt.length; i++) {Tablayout.addtab (Tablayout.newtab (). SetText (Tabtxt[i]));} Calculate the height of the content block, full screen height-the height of the status bar height-tablayout (here fixed height 50dp), used for Recyclerview last item view fill height int screenh = getscreenheight (); int Statusbarh = Getstatusbarheight (this), int tabh = * 3;int lasth = Screenh-statusbarh-tabh;manager = new Linearla Youtmanager (this); Recyclerview.setlayoutmanager (manager) Recyclerview.setadapter (The new Myadapter (this, tabtxt, LASTH)); @Overridepublic void Onbindviewholder (myviewholder holder, int position) {HOLDER.ANCHORVIEW.SETCONTENTTXT ( Tabtxt[position]); HOLDER.ANCHORVIEW.SETANCHORTXT (Tabtxt[position]); Determine the last view if (Position = = tabtxt.length-1) {if (Holder.anchorView.getHeight () < lasth) {linearlayout.layoutparams params = new linearlayout.la Youtparams (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); Params.height = Lasth; Holder.anchorView.setLayoutParams (params); } }}
Recyclerview Sliding Positioning
When the recyclerView slide is triggered, the addOnScrollListener listener's onScrolled first visible view position is tablayout positioned directly to the corresponding position.
recyclerView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //当滑动由recyclerView触发时,isRecyclerScroll 置true if (event.getAction() == MotionEvent.ACTION_DOWN) { isRecyclerScroll = true; } return false; }});recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (isRecyclerScroll) { //第一个可见的view的位置,即tablayou需定位的位置 int position = manager.findFirstVisibleItemPosition(); if (lastPos != position) { tabLayout.setScrollPosition(position, 0, true); } lastPos = position; } }});
Tablayout Switching positioning
Click to tablayout switch, recyclerView you need to slide to the appropriate location, note that this need to be based on the location of the jump, the corresponding sliding.
Tablayout.addontabselectedlistener (New Tablayout.ontabselectedlistener () {@Override public void ontabselected (TabLa Yout. Tab tab) {//Click the tab to make Recyclerview slide, isrecyclerscroll false int pos = Tab.getposition (); Isrecyclerscroll = false; Movetoposition (Manager, Recyclerview, POS); } @Override public void ontabunselected (Tablayout.tab Tab) {} @Override the public void ontabreselected (Tablay Out. Tab tab) {}});p ublic void Movetoposition (Linearlayoutmanager manager, Recyclerview mrecyclerview, int position) {/ /The position of the first visible view int firstitem = Manager.findfirstvisibleitemposition (); Position of the last visible view int lastitem = Manager.findlastvisibleitemposition (); if (position <= FirstItem) {//If the jump position FirstItem before (slide out of the screen case), Smoothscrolltoposition can jump directly, Mrecyclerview. Smoothscrolltoposition (position); } else if (position <= LastItem) {//Jump position after FirstItem, LastItem (shown on current screen), Smoothscrollby to slide to the specified position int t Op = Mrecyclerview.getchildat (Position-firstitem). GetTop (); Mrecyclerview.smoothscrollby (0, top); } else {//if the position to jump is after LastItem, first call smoothscrolltoposition the position to be jumped to the visible position//again by onscrollstatechanged control to call the current Movetoposition method, execute the method mrecyclerview.smoothscrolltoposition (position) in the Last Judgment; Scrolltoposition = position; CanScroll = true; }}recyclerview.addonscrolllistener (New Recyclerview.onscrolllistener () {@Override public void onscrollstatechanged ( Recyclerview recyclerview, int newstate) {super.onscrollstatechanged (Recyclerview, newstate); if (canscroll) {canscroll = false; Movetoposition (Manager, Recyclerview, scrolltoposition); } }});
At this point, two ways to implement anchor positioning are introduced here, hoping to help the reader in the actual project use.
The code is in the same git address as the previous article.
Detailed code See
GitHub Address: Https://github.com/taixiang/tabScroll
Welcome to follow my blog: https://blog.manjiexiang.cn/
More welcome attention number: Spring Breeze ten miles Better know you
There is a "Buddha system code Farming Circle", welcome everyone to join the chat, Happy good!
Expired, can add me tx467220125 pull you into the group.
Android Tablayout+recyclerview for Anchor point positioning