1.mainactivity布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:id="@+id/rl_title" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/colorPrimary"> <ImageView android:id="@+id/iv_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:src="@drawable/new_img_back" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/app_name" android:textColor="@android:color/white" android:textSize="18sp" /> </RelativeLayout> <com.yuqirong.tabsuspenddemo.view.MyScrollView android:id="@+id/mScrollView" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#cccccc" android:orientation="vertical"> <ImageView android:id="@+id/iv_pic" android:layout_width="match_parent" android:layout_height="180dp" android:scaleType="centerCrop" android:src="@drawable/ic_bg_personal_page" /> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:layout_marginBottom="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="55dp" android:background="@android:color/white" android:orientation="horizontal"></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:layout_marginBottom="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:layout_marginBottom="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:layout_marginBottom="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:layout_marginBottom="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:layout_marginBottom="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:layout_marginBottom="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"></LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:layout_marginBottom="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="15dp" android:background="@android:color/white" android:orientation="horizontal"></LinearLayout> <include layout="@layout/tab_layout" /> </LinearLayout> </com.yuqirong.tabsuspenddemo.view.MyScrollView> </LinearLayout> 2.tab_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_tab"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="分类"
android:textColor="@android:color/white"
android:textSize="18sp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="排序"
android:textColor="@android:color/white"
android:textSize="18sp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="筛选"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
3.mainactivity
public class MainActivity extends AppCompatActivity implements MyScrollView.OnScrollListener { private static final String TAG = "MainActivity"; private MyScrollView mScrollView; private LinearLayout ll_tab; private ImageView iv_pic; private int picBottom; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().hide(); mScrollView = (MyScrollView) findViewById(R.id.mScrollView); mScrollView.setOnScrollListener(this); ll_tab = (LinearLayout) findViewById(R.id.ll_tab); iv_pic = (ImageView) findViewById(R.id.iv_pic); findViewById(R.id.ll_main).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { onScroll(mScrollView.getScrollY()); } }); } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { picBottom = iv_pic.getBottom(); } } @Override public void onScroll(int scrollY) { int top = Math.max(scrollY, picBottom); ll_tab.layout(0, top, ll_tab.getWidth(), top + ll_tab.getHeight()); } }