package com.myandroid.scrollandtabdemo;
import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
/**
* Created by John on 2017/10/31.
* <p>
* 上下滑动标题联动、标题栏固定到顶部 效果
*/
public class MainActivity extends Activity implements View.OnClickListener {
// 滑动 scrollview
private MyScrollView my_scrollview;
// 固定标题和滑动标题 LinearLayout
private LinearLayout ll_title_in_scrollview, ll_title_top;
// 固定标题
private TextView tv_title1, tv_title2, tv_title3;
// ScrollView 中的标题
private TextView tv_title_in_scrollview1, tv_title_in_scrollview2, tv_title_in_scrollview3;
// top界面、3个body界面
private LinearLayout ll_top_view, ll_body1, ll_body2, ll_body3;
// 动画图片
private ImageView imgv_cursor;
// 动画图片偏移量
private int offset = 0;
// 上一个界面 id
private int lastTabIndex = 0;
/**
* 用于在同一个内容模块内滑动,锁定导航标签,防止重复刷新标签
* true: 锁定
* false ; 没有锁定
*/
private boolean content2NavigateFlagInnerLock = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initImageView();
addClickListener();
addScrollListener();
}
/**
* 初始化界面控件
*/
private void initView() {
ll_title_in_scrollview = findViewById(R.id.ll_title_in_scrollview);
ll_title_top = findViewById(R.id.ll_title_top);
tv_title1 = findViewById(R.id.tv_title1);
tv_title2 = findViewById(R.id.tv_title2);
tv_title3 = findViewById(R.id.tv_title3);
tv_title_in_scrollview1 = findViewById(R.id.tv_title_in_scrollview1);
tv_title_in_scrollview2 = findViewById(R.id.tv_title_in_scrollview2);
tv_title_in_scrollview3 = findViewById(R.id.tv_title_in_scrollview3);
imgv_cursor = findViewById(R.id.imgv_cursor);
my_scrollview = findViewById(R.id.my_scrollview);
ll_top_view = findViewById(R.id.ll_top_view);
ll_body1 = findViewById(R.id.ll_body1);
ll_body2 = findViewById(R.id.ll_body2);
ll_body3 = findViewById(R.id.ll_body3);
}
/**
* 初始化滑动条
*/
private void initImageView() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;// 获取分辨率宽度
offset = screenW / 3;// 计算偏移量
LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(offset, 4);
imgv_cursor.setLayoutParams(ll);
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
imgv_cursor.setImageMatrix(matrix);// 设置动画初始位置
}
/**
* 添加标题点击监听
*/
private void addClickListener() {
tv_title1.setOnClickListener(this);
tv_title2.setOnClickListener(this);
tv_title3.setOnClickListener(this);
tv_title_in_scrollview1.setOnClickListener(this);
tv_title_in_scrollview2.setOnClickListener(this);
tv_title_in_scrollview3.setOnClickListener(this);
}
/**
* 添加滑动监听
*/
private void addScrollListener() {
my_scrollview.setScrollViewListener(new MyScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(ScrollView scrollView, int x, int y, int oldx, int oldy) {
scrollRefreshNavigationTag(scrollView);
}
@Override
public void onScrollStop(boolean isScrollStop) {
}
});
}
/**
* 内容区域滑动刷新导航标签
*
* @param scrollView 内容模块容器
*/
private void scrollRefreshNavigationTag(ScrollView scrollView) {
if (scrollView == null) {
return;
}
// 获得ScrollView滑动距离
int scrollY = scrollView.getScrollY();
// 确定头部标题显示或隐藏
if (scrollY >= ll_title_in_scrollview.getTop()) {
ll_title_top.setVisibility(View.VISIBLE);
} else {
ll_title_top.setVisibility(View.GONE);
}
// 确定ScrollView当前展示的顶部内容属于哪个内容模块
if (scrollY >= ll_body3.getTop() - ll_title_in_scrollview.getHeight()) {
refreshContent2NavigationFlag(2);
} else if (scrollY >= ll_body2.getTop() - ll_title_in_scrollview.getHeight()) {
refreshContent2NavigationFlag(1);
} else if (scrollY >= ll_body1.getTop() - ll_title_in_scrollview.getHeight()) {
refreshContent2NavigationFlag(0);
}
}
/**
* 刷新标签
*
* @param currentTagIndex 当前模块位置
*/
private void refreshContent2NavigationFlag(int currentTagIndex) {
// 上一个位置与当前位置不一致是,解锁内部锁,是导航可以发生变化
if (lastTabIndex != currentTagIndex) {
content2NavigateFlagInnerLock = false;
}
if (!content2NavigateFlagInnerLock) {
// 锁定内部锁
content2NavigateFlagInnerLock = true;
// 滑动到指定位置
splippingToTab(currentTagIndex);
}
lastTabIndex = currentTagIndex;
}
/**
* 滑动到指定的位置
*
* @param tabIndex
*/
private void splippingToTab(int tabIndex) {
Animation animation = new TranslateAnimation(offset * lastTabIndex, offset * tabIndex, 0, 0);
animation.setFillAfter(true);// True:图片停在动画结束位置
animation.setDuration(200);
imgv_cursor.startAnimation(animation);
restoreStyle();
if (tabIndex == 0) {
setStyle(tv_title1);
} else if (tabIndex == 1) {
setStyle(tv_title2);
} else if (tabIndex == 2) {
setStyle(tv_title3);
}
}
/**
* 还原字体默认颜色
*/
private void restoreStyle() {
tv_title1.setTextColor(getResources().getColor(R.color.new_grey1));
tv_title2.setTextColor(getResources().getColor(R.color.new_grey1));
tv_title3.setTextColor(getResources().getColor(R.color.new_grey1));
}
/**
* 设置选中字体颜色
*/
private void setStyle(TextView txt) {
txt.setTextColor(getResources().getColor(R.color.new_purple));
}
/**
* 点击事件
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_title1: // 顶部标题
my_scrollview.smoothScrollTo(0, ll_body1.getTop() - ll_title_in_scrollview.getHeight());
break;
case R.id.tv_title2:
my_scrollview.smoothScrollTo(0, ll_body2.getTop() - ll_title_in_scrollview.getHeight());
break;
case R.id.tv_title3:
my_scrollview.smoothScrollTo(0, ll_body3.getTop() - ll_title_in_scrollview.getHeight());
break;
case R.id.tv_title_in_scrollview1: // 滑动标题
my_scrollview.smoothScrollTo(0, ll_body1.getTop() - ll_title_in_scrollview.getHeight());
break;
case R.id.tv_title_in_scrollview2:
上下滚动tab标题跟随联动效果,点击tab滚动切换模块
5星 · 超过95%的资源 需积分: 49 117 浏览量
2017-12-25
10:49:53
上传
评论 1
收藏 172KB ZIP 举报
秋水渡人
- 粉丝: 1
- 资源: 11
最新资源
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈