用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);

    這樣就有了動畫中最後,會定位到數字中間的動作,目前看起來沒有太大的維和感。

參考這裏及若干

源碼下載

發佈了28 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章