ScrollView 与TabLayout 结合使用
废话不多说,上效果先
看效果你会发现 中间的锚点导航,到最上面一直存在,这里其实是有2个TabLayout 一个位于布局顶部
一个在ScrollView 内部
因此布局是这样的
<code>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
>
<com.aiming_point.scrollviewaimingpoint.ObservableScrollView
android:id="@+id/sv_aiming_point"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@mipmap/a"/>
<android.support.design.widget.TabLayout
android:id="@+id/inside_tab"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFB8D308"
app:tabGravity="fill"
app:tabMode="fixed">
</android.support.design.widget.TabLayout>
<TextView
android:id="@+id/aiming_point1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:text="锚点1"
android:textSize="30sp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@mipmap/b"/>
<TextView
android:id="@+id/aiming_point2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:text="锚点2"
android:textSize="30sp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="160dp"
android:scaleType="centerCrop"
android:src="@mipmap/c"/>
<TextView
android:id="@+id/aiming_point3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:text="锚点3"
android:textSize="30sp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="230dp"
android:scaleType="centerCrop"
android:src="@mipmap/d"/>
<TextView
android:id="@+id/aiming_point4"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:text="锚点4"
android:textSize="30sp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@mipmap/e"/>
<TextView
android:id="@+id/aiming_point5"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:text="锚点5"
android:textSize="30sp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
android:src="@mipmap/f"/>
<TextView
android:id="@+id/aiming_point6"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:text="锚点6"
android:textSize="30sp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@mipmap/g"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="600dp"
android:scaleType="centerCrop"
android:src="@mipmap/a"/>
</LinearLayout>
</RelativeLayout>
</com.aiming_point.scrollviewaimingpoint.ObservableScrollView>
<android.support.design.widget.TabLayout
android:id="@+id/outside_tab"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFB8D308"
android:visibility="gone"
app:tabGravity="fill"
app:tabMode="fixed">
</android.support.design.widget.TabLayout>
</RelativeLayout>
</code>
定义锚点首先我们需要ScrollView滑动监听,滑动状态监听, TabLayout选中监听
因此我们需要实现其相应的接口TabLayout.OnTabSelectedListener, View.OnScrollChangeListener, View.OnTouchListener
ScrollView滑动监听处理,这里定义了 当滑动的具体达到内部的TabLayout的高度的时候在外部固定位置的TabLayout则显示, 锚点位置判断, 遍历所有锚点的位置,从最后一个锚点开始计算,当滑动的位置+TabLayout的高度大于锚点的top位置的时候则将TabLayout设置成选中该位置
<code>
@Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
/**已经滑动的距离*/
int scorlly = mScrollView.getScrollY();
/**导航控制*/
if (scorlly >= mInside_tab.getTop()) {
mOutside_tab.setVisibility(View.VISIBLE);
} else {
mOutside_tab.setVisibility(View.GONE);
}
/**遍历瞄点 从最下面瞄点开始遍历, 如果 已经滑动的Y距离+tablayout高度 大于 瞄点的top 选中,选中该tab */
for (int j = 0; j < mAimingPointList.size(); j++) {
int index = mAimingPointList.size() - j - 1;
int top = mAimingPointList.get(index).getTop();
if (scorlly + mCoverHeight >= top) {
mInside_tab.getTabAt(index).select();
mOutside_tab.getTabAt(index).select();
return;
}
}
}
</code>
ScrollView的滑动状态监听,保存当前的滑动状态
<code>
/**
* 当前的触摸状态
*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
state = motionEvent.getAction();
return false;
}
</code>
TabLayout选中监听 ,选中时2个TabLayout 同步位置,并且显示外部的TabLayout,根据滑动状态,来判断是否移动ScrollView, 状态是ACTION_UP的时候,也就是滑动停止的时候 选中TabLayout条目,才会滑动ScrollView
<code>
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
mOutside_tab.getTabAt(position).select();
mInside_tab.getTabAt(position).select();
mOutside_tab.setVisibility(View.VISIBLE);
if (state == MotionEvent.ACTION_UP) {
mScrollView.smoothScrollTo(0, mAimingPointList.get(position).getTop() - dip2px(mContext, 50));
}
}
</code>
完整代码
Activity代码
<code>
package com.aiming_point.scrollviewaimingpoint;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
/**
* 内部的tab
*/
private TabLayout mInside_tab;
/**
* 外部的tab
*/
private TabLayout mOutside_tab;
private TextView mAiming_point1;
private TextView mAiming_point2;
private TextView mAiming_point3;
private TextView mAiming_point4;
private TextView mAiming_point5;
private TextView mAiming_point6;
private String[] pointText = new String[]{"锚点1", "锚点2", "锚点3", "锚点4", "锚点5", "锚点6"};
private ObservableScrollView mScrollView;
private ArrayList<TextView> mAimingPointList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
/**创建锚点容器*/
mAimingPointList = new ArrayList<>();
mOutside_tab = (TabLayout) findViewById(R.id.outside_tab);
mInside_tab = (TabLayout) findViewById(R.id.inside_tab);
mAiming_point1 = (TextView) findViewById(R.id.aiming_point1);
mAiming_point2 = (TextView) findViewById(R.id.aiming_point2);
mAiming_point3 = (TextView) findViewById(R.id.aiming_point3);
mAiming_point4 = (TextView) findViewById(R.id.aiming_point4);
mAiming_point5 = (TextView) findViewById(R.id.aiming_point5);
mAiming_point6 = (TextView) findViewById(R.id.aiming_point6);
mScrollView = (ObservableScrollView) findViewById(R.id.sv_aiming_point);
/**添加锚点*/
mAimingPointList.add(mAiming_point1);
mAimingPointList.add(mAiming_point2);
mAimingPointList.add(mAiming_point3);
mAimingPointList.add(mAiming_point4);
mAimingPointList.add(mAiming_point5);
mAimingPointList.add(mAiming_point6);
/**初始化tabLayout*/
for (int i = 0; i < pointText.length; i++) {
addAimingPoint(mInside_tab, pointText[i]);
addAimingPoint(mOutside_tab, pointText[i]);
}
new ScrollViewTheAnchor(mInside_tab, mOutside_tab, mScrollView, mAimingPointList, this, 50);
}
/**
* 给TabLayout添加条目
*/
public void addAimingPoint(TabLayout tabLayout, String text) {
tabLayout.addTab(tabLayout.newTab().setText(text));
}
}
</code>
ScrollViewTheAnchor代码
<code>
import android.content.Context;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
public class ScrollViewTheAnchor implements TabLayout.OnTabSelectedListener, ScrollViewListener, View.OnTouchListener {
/**
* 内部的tab
*/
private TabLayout mInside_tab;
/**
* 外部的tab
*/
private TabLayout mOutside_tab;
private ObservableScrollView mScrollView;
/**
* 锚点集合
*/
private ArrayList<TextView> mAimingPointList;
private Context mContext;
/**
* ScrollView 的滑动状态
*/
private int state = MotionEvent.ACTION_UP;
/**
* 覆盖的高度
*/
private float mCoverHeight;
/**
* @param inside_tab 内部Tab
* @param outside_tab 外部Tab
* @param scrollView scrollView
* @param aimingPointList 定位的锚点集合
* @param coverHeight 覆盖在scrollView上面的 布局总高度dp值
*/
public ScrollViewTheAnchor(TabLayout inside_tab, TabLayout outside_tab, ObservableScrollView scrollView, ArrayList<TextView> aimingPointList, Context context, float coverHeight) {
this.mInside_tab = inside_tab;
this.mOutside_tab = outside_tab;
this.mScrollView = scrollView;
this.mAimingPointList = aimingPointList;
this.mContext = context;
this.mCoverHeight = dip2px(mContext, coverHeight);
mOutside_tab.setOnTabSelectedListener(this);
mInside_tab.setOnTabSelectedListener(this);
mScrollView.setScrollViewListener(this);
mScrollView.setOnTouchListener(this);
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.i("xionghui","onTabSelected"+tab.getPosition());
int position = tab.getPosition();
mOutside_tab.getTabAt(position).select();
mInside_tab.getTabAt(position).select();
mOutside_tab.setVisibility(View.VISIBLE);
if (state == MotionEvent.ACTION_UP) {
mScrollView.smoothScrollTo(0, mAimingPointList.get(position).getTop() - dip2px(mContext, 50));
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
Log.i("xionghui","onTabUnselected"+tab.getPosition());
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Log.i("xionghui","onTabReselected"+tab.getPosition());
}
/**
* 将dip或dp值转换为px值,保证尺寸大小不变
*
* @param context
* @param dipValue
* @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* 当前的触摸状态
*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
state = motionEvent.getAction();
return false;
}
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
/**已经滑动的距离*/
int scorlly = mScrollView.getScrollY();
/**导航控制*/
if (scorlly >= mInside_tab.getTop()) {
mOutside_tab.setVisibility(View.VISIBLE);
} else {
mOutside_tab.setVisibility(View.GONE);
}
/**遍历锚点 从最下面瞄点开始遍历, 如果 已经滑动的Y距离+tablayout高度 大于 锚点的top 选中,选中该tab */
for (int j = 0; j < mAimingPointList.size(); j++) {
int index = mAimingPointList.size() - j - 1;
int top = mAimingPointList.get(index).getTop();
if (scorlly + mCoverHeight >= top) {
mInside_tab.getTabAt(index).select();
mOutside_tab.getTabAt(index).select();
return;
}
}
}
}
</code>
ObservableScrollView
<code>
package com.aiming_point.scrollviewaimingpoint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
public class ObservableScrollView extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
}
ScrollViewListener
</code>
package com.aiming_point.scrollviewaimingpoint;
public interface ScrollViewListener {
void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
<code>
</code>