import android.content.Context; import android.support.design.widget.TabLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.MotionEvent; import android.view.View; public class DesignStickyActivity extends AppCompatActivity { private LinearLayoutManager manager; private RecyclerView recyclerview; private String[] tabTxt = {"android", "java", "视频", "UI", "ios", "产品","老板","下午茶"}; private TabLayout tabLayout; //判读是否是recyclerView主动引起的滑动,true- 是,false- 否,由tablayout引起的 private boolean isRecyclerScroll; //记录上一次位置,防止在同一内容块里滑动 重复定位到tablayout private int lastPos; //用于recyclerView滑动到指定的位置 private boolean canScroll; private int scrollToPosition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_design_scroll); recyclerview = findViewById(R.id.recyclerview); tabLayout = findViewById(R.id.tablayout); //tablayout设置标签 for (int i = 0; i < tabTxt.length; i++) { tabLayout.addTab(tabLayout.newTab().setText(tabTxt[i])); } //计算内容块所在的高度,全屏高度-状态栏高度-tablayout的高度(这里固定高度50dp),用于recyclerView的最后一个item view填充高度 int screenH = getScreenHeight(); int statusBarH = getStatusBarHeight(this); int tabH = 50 * 3; int lastH = screenH - statusBarH - tabH; manager = new LinearLayoutManager(this); recyclerview.setLayoutManager(manager); recyclerview.setAdapter(new MyAdapter(this, tabTxt, lastH)); tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { //点击标签,使recyclerView滑动,isRecyclerScroll置false int pos = tab.getPosition(); isRecyclerScroll = false; moveToPosition(manager, recyclerview, pos); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); 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 onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (canScroll) { canScroll = false; moveToPosition(manager, recyclerView, scrollToPosition); } } @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; } } }); } public void moveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, int position) { // 第一个可见的view的位置 int firstItem = manager.findFirstVisibleItemPosition(); // 最后一个可见的view的位置 int lastItem = manager.findLastVisibleItemPosition(); if (position <= firstItem) { // 如果跳转位置firstItem 之前(滑出屏幕的情况),就smoothScrollToPosition可以直接跳转, mRecyclerView.smoothScrollToPosition(position); } else if (position <= lastItem) { // 跳转位置在firstItem 之后,lastItem 之间(显示在当前屏幕),smoothScrollBy来滑动到指定位置 int top = mRecyclerView.getChildAt(position - firstItem).getTop(); mRecyclerView.smoothScrollBy(0, top); } else { // 如果要跳转的位置在lastItem 之后,则先调用smoothScrollToPosition将要跳转的位置滚动到可见位置 // 再通过onScrollStateChanged控制再次调用当前moveToPosition方法,执行上一个判断中的方法 mRecyclerView.smoothScrollToPosition(position); scrollToPosition = position; canScroll = true; } } 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; } }
布局:
xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/gray" app:elevation="0dp"> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="@android:color/holo_orange_light" android:gravity="center" android:text="这是头部滚动部分" app:layout_scrollFlags="scroll"/> <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorAccent" /> android.support.design.widget.AppBarLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> android.support.design.widget.CoordinatorLayout>
RecyclerView中的adapter
package com.tabscroll; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; public class MyAdapter extends RecyclerView.Adapter{ private Context context; private String[] tabTxt; private int lastH; public MyAdapter(Context context, String[] tabTxt, int lastH) { this.context = context; this.tabTxt = tabTxt; this.lastH = lastH; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.adapter_view, parent, false); MyViewHolder viewHolder = new MyViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.anchorView.setContentTxt(tabTxt[position]); holder.anchorView.setAnchorTxt(tabTxt[position]); //判断最后一个view if (position == tabTxt.length - 1) { System.out.println("positon 》"+position); if (holder.anchorView.getHeight() < lastH) { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.height = lastH; holder.anchorView.setLayoutParams(params); } }else { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); holder.anchorView.setLayoutParams(params); } } @Override public int getItemCount() { return tabTxt.length; } public static class MyViewHolder extends RecyclerView.ViewHolder { private AnchorView anchorView; public MyViewHolder(View itemView) { super(itemView); anchorView = itemView.findViewById(R.id.anchorView); } } }
效果如下: