作者:孤魂地狱 | 来源:互联网 | 2023-09-10 10:07
话不多说,先上效果图:
好了,来看xml代码:(为了保持简明,我删除了部分代码)
title层级是这样的:
HorizontalScrollView, RelativeLayout, LinearLayout, TextView
控制横向滚动 包裹标题与光标 包裹标题 六个标题
然后是Java代码(这里也删除并修改了部分代码)
public class XXXActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
private HorizontalScrollView scroll_view;
private LinearLayout llt_title;
private TextView tx_applying,
tx_holding,
tx_can_be_redeemed,
tx_can_be_transfer,
tx_has_finished,
tx_apply_fail;
private View cursor;
private ViewPager fragment_view_pager;
private MyFragmentPagerAdapter adapter;
private int currentIndex;//当前页卡编号
private int subTitleWidth;//子标题宽度,cursor平移距离
private int titleBarHideOffset;//整个标题未显示的长度
private int firstHideSubTitle;//第一个未显示全的标题(从0数起)
private int hideTitleNum;//未显示标题的数目(从1数起)
@Override
protected void onCreate() {
scroll_view = (HorizontalScrollView) findViewById(R.id.scroll_view);
llt_title = (LinearLayout) findViewById(R.id.llt_title);
tx_applying = (TextView) findViewById(R.id.tx_applying);
tx_holding = (TextView) findViewById(R.id.tx_holding);
tx_can_be_redeemed = (TextView) findViewById(R.id.tx_can_be_redeemed);
tx_can_be_transfer = (TextView) findViewById(R.id.tx_can_be_transfer);
tx_has_finished = (TextView) findViewById(R.id.tx_has_finished);
tx_apply_fail = (TextView) findViewById(R.id.tx_apply_fail);
cursor = findViewById(R.id.cursor);
fragment_view_pager = (ViewPager) findViewById(R.id.fragment_view_pager);
tx_applying.setOnClickListener(this);
tx_holding.setOnClickListener(this);
tx_can_be_redeemed.setOnClickListener(this);
tx_can_be_transfer.setOnClickListener(this);
tx_has_finished.setOnClickListener(this);
tx_apply_fail.setOnClickListener(this);
//获取屏幕宽度、标题宽度
WindowManager manager = this.getWindowManager();
DisplayMetrics outMetrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(outMetrics);
int windowWidth = outMetrics.widthPixels;
subTitleWidth = DisplayUtils.dip2px(this, 85);//一个title的宽度
int titleBarWidth = subTitleWidth * 6;//整个title的宽度
//标题栏最大移动距离与第一个需要移动的title
titleBarHideOffset = titleBarWidth - windowWidth;//未显示出的部分
if (titleBarHideOffset <0) {
titleBarHideOffset = 0;
hideTitleNum = 0;
} else {//这里逻辑开始略复杂,关键是获取第一个未显示全的title位置
for (int i = 0; i <6; i++) {
if (windowWidth getFragmentList() {
ArrayList fragmentList = new ArrayList<>();
//此处省略......
return fragmentList;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tx_applying:
fragment_view_pager.setCurrentItem(0);
break;
case R.id.tx_holding:
fragment_view_pager.setCurrentItem(1);
break;
case R.id.tx_can_be_redeemed:
fragment_view_pager.setCurrentItem(2);
break;
case R.id.tx_can_be_transfer:
fragment_view_pager.setCurrentItem(3);
break;
case R.id.tx_has_finished:
fragment_view_pager.setCurrentItem(4);
break;
case R.id.tx_apply_fail:
fragment_view_pager.setCurrentItem(5);
break;
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageSelected(int position) {
Animation animation = new TranslateAnimation(
currentIndex * subTitleWidth, //from X
position * subTitleWidth, //to X
0, 0
);
//cursor滑动
currentIndex = position;
animation.setFillAfter(true);//动画终止时停留在最后一帧,不然会回到没有执行前的状态
animation.setInterpolator(new DecelerateInterpolator());//减速动画
animation.setDuration(300);//动画持续时间0.3秒
cursor.startAnimation(animation);
llt_title.getChildAt(position).requestFocus();
//整体滑动
if (hideTitleNum > 0) {
if (position position * subTitleWidth) {
smoothScrollTo(position * subTitleWidth);
}
}
int num = position - firstHideSubTitle;
if (num >= 0) {//右侧
int scrollTo = titleBarHideOffset - (hideTitleNum - (num + 1)) * subTitleWidth;
if (scroll_view.getScrollX() list;
public MyFragmentPagerAdapter(FragmentManager fm, ArrayList list) {
super(fm);
this.list = list;
}
@Override
public FragProductList getItem(int arg0) {
return list.get(arg0);
}
@Override
public int getCount() {
return list.size();
}
}
}
如果有什么不大明白我们可以讨论,反正能直接用就是了