闲鱼怎么做钓鱼网站,网站开发商品排序逻辑,wordpress内页php页面,网站流量很少在前两篇文章中#xff0c;分别介绍了tablayoutscrollview 和 tablayoutrecyclerview 实现的滑动定位的功能#xff0c;文章链接#xff1a;Android 实现锚点定位Android tabLayoutrecyclerView实现锚点定位仔细看的话#xff0c;这种滑动定位的功能#xff0c;还可以整体…在前两篇文章中分别介绍了tablayoutscrollview 和 tablayoutrecyclerview 实现的滑动定位的功能文章链接Android 实现锚点定位Android tabLayoutrecyclerView实现锚点定位仔细看的话这种滑动定位的功能还可以整体滑动再加上顶部tablayout 吸附悬停的效果。实现效果布局这里采用的是两个 tablayout。一个用于占位位于原始位置scrollview内部随scrollview滚动另一个则是在滑动过程中不断滑动滑动到顶部时吸附在屏幕顶部用户实际操作的也是这个tablayout。xmlns:apphttp://schemas.android.com/apk/res-autoandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalandroid:idid/scrollViewandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationverticalandroid:layout_widthmatch_parentandroid:layout_height200dpandroid:background#cccandroid:gravitycenterandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text这里是顶部内容区域android:textSize16sp /android:idid/tablayout_holderandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:background#ffffffapp:tabIndicatorColorcolor/colorPrimaryapp:tabModescrollableapp:tabSelectedTextColorcolor/colorPrimary /android:idid/containerandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationverticalandroid:padding16dp /android:idid/tablayout_realandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:background#ffffffandroid:visibilityinvisibleapp:tabIndicatorColorcolor/colorPrimaryapp:tabModescrollableapp:tabSelectedTextColorcolor/colorPrimary /实现滑动定位的功能可以参考之前的文章这里主要是进行吸附悬停的效果。数据初始化/*** 占位tablayout用于滑动过程中去确定实际的tablayout的位置*/private TabLayout holderTabLayout;/*** 实际操作的tablayout*/private TabLayout realTabLayout;private CustomScrollView scrollView;private LinearLayout container;private String[] tabTxt {客厅, 卧室, 餐厅, 书房, 阳台, 儿童房};private List anchorList new ArrayList();//判读是否是scrollview主动引起的滑动true-是false-否由tablayout引起的private boolean isScroll;//记录上一次位置防止在同一内容块里滑动 重复定位到tablayoutprivate int lastPos 0;//监听判断最后一个模块的高度不满一屏时让最后一个模块撑满屏幕private ViewTreeObserver.OnGlobalLayoutListener listener;for (int i 0; i tabTxt.length; i) {AnchorView anchorView new AnchorView(this);anchorView.setAnchorTxt(tabTxt[i]);anchorView.setContentTxt(tabTxt[i]);anchorList.add(anchorView);container.addView(anchorView);}for (int i 0; i tabTxt.length; i) {holderTabLayout.addTab(holderTabLayout.newTab().setText(tabTxt[i]));realTabLayout.addTab(realTabLayout.newTab().setText(tabTxt[i]));}一开始让实际的tablayout 移动到占位的tablayout 处覆盖占位的tablayout。listener new ViewTreeObserver.OnGlobalLayoutListener() {Overridepublic void onGlobalLayout() {//计算让最后一个view高度撑满屏幕int screenH getScreenHeight();int statusBarH getStatusBarHeight(AliHomeMoreActivity.this);int tabH holderTabLayout.getHeight();int lastH screenH - statusBarH - tabH - 16 * 3;AnchorView anchorView anchorList.get(anchorList.size() - 1);if (anchorView.getHeight() lastH) {LinearLayout.LayoutParams params new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);params.height lastH;anchorView.setLayoutParams(params);}//一开始让实际的tablayout 移动到 占位的tablayout处覆盖占位的tablayoutrealTabLayout.setTranslationY(holderTabLayout.getTop());realTabLayout.setVisibility(View.VISIBLE);container.getViewTreeObserver().removeOnGlobalLayoutListener(listener);}};container.getViewTreeObserver().addOnGlobalLayoutListener(listener);private int getScreenHeight() {return getResources().getDisplayMetrics().heightPixels;}public int getStatusBarHeight(Context context) {int result 0;int resourceId context.getResources().getIdentifier(status_bar_height, dimen, android);if (resourceId 0) {result context.getResources().getDimensionPixelSize(resourceId);}return result;}scrollview滑动主要在滑动过程这不断监听滑动的距离再移动实际的tablayout 当在屏幕内时让其一直覆盖在占位的tablayout 上看上去是跟着scrollview 一起滑动的当滑出屏幕时实际的tablayout 不断移动 使其相对屏幕静止看上去是吸附在屏幕顶部。scrollView.setOnTouchListener(new View.OnTouchListener() {Overridepublic boolean onTouch(View v, MotionEvent event) {if (event.getAction() MotionEvent.ACTION_DOWN) {isScroll true;}return false;}});//监听scrollview滑动scrollView.setCallbacks(new CustomScrollView.Callbacks() {Overridepublic void onScrollChanged(int x, int y, int oldx, int oldy) {//根据滑动的距离y(不断变化的) 和 holderTabLayout距离父布局顶部的距离(这个距离是固定的)对比//当y holderTabLayout.getTop()时holderTabLayout 仍在屏幕内realTabLayout不断移动holderTabLayout.getTop()距离覆盖holderTabLayout//当y holderTabLayout.getTop()时holderTabLayout 移出realTabLayout不断移动y相对的停留在顶部看上去是静止的int translation Math.max(y, holderTabLayout.getTop());realTabLayout.setTranslationY(translation);if (isScroll) {for (int i tabTxt.length - 1; i 0; i--) {//需要y减去顶部内容区域的高度(具体看项目的高度这里demo写死的200dp)if (y - 200 * 3 anchorList.get(i).getTop() - 10) {setScrollPos(i);break;}}}}});private void setScrollPos(int newPos) {if (lastPos ! newPos) {realTabLayout.setScrollPosition(newPos, 0, true);}lastPos newPos;}tablayout点击切换由于实际操作的是realtablayout 所以这里只需要一直监听该tablayout。//实际的tablayout的点击切换realTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {Overridepublic void onTabSelected(TabLayout.Tab tab) {isScroll false;int pos tab.getPosition();int top anchorList.get(pos).getTop();//同样这里滑动要加上顶部内容区域的高度(这里写死的高度)scrollView.smoothScrollTo(0, top 200 * 3);}Overridepublic void onTabUnselected(TabLayout.Tab tab) {}Overridepublic void onTabReselected(TabLayout.Tab tab) {}});至此滑动定位顶部吸附悬停 的效果结束了。做完之后再看这个效果其实和 支付宝-首页 更多 那个页面里的滑动效果一样。代码与之前文章的在同一个git地址里。