用HorizontalScrollView仿电台频道切换

这里写图片描述

搞这个Demo进了好多坑,不想扔掉,你懂?^_^^,最后发现这个滑动的可以,但左右两边的阴影不是它实现的,蒙了张图片了事

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:centerColor="#33FFFFFF"
        android:endColor="#330f1511"
        android:startColor="#330f1511" />
</shape>

实现大概步骤:

  • 写一个子ItemLayout,有ImageView和TextView,略
  • 主界面XML布局

        <com.xuie.horizontalscrollviewdemo.ChannelView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:layout_alignParentBottom="true"
            android:scrollbars="none">
    
            <LinearLayout
                android:id="@+id/ll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal" />
        </com.xuie.horizontalscrollviewdemo.ChannelView>
  • 添加View

    LayoutInflater inflater = LayoutInflater.from(this);
    for (float n : nums) {
        LinearLayout item = (LinearLayout) inflater.inflate(R.layout.context_item, ll, false);
        item.getChildAt(0).setBackgroundResource(R.mipmap.bg);
        ((TextView) item.getChildAt(1)).setText(n + "");
        ll.addView(item);
    }
  • 滑动选择,是通过mHandler延时发送实现的
    这里有几个矛盾,TOUCH_UP的后还会滑动一段时间,是不是指OverScroller里面的缺省Duration呢 - DEFAULT_DURATION = 250ms我自己试了有时滑完后都快1秒钟了,

    mHandler.removeMessages(0);
    mHandler.sendEmptyMessageDelayed(0, 1000);

    这样就有了动画中最后,会定位到数字中间的动作,目前看起来没有太大的维和感。

参考这里及若干

源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值