1. 程式人生 > >Android 實現類似於淘寶頭條的熱點滾動推薦

Android 實現類似於淘寶頭條的熱點滾動推薦

  其實原理很簡單。使用ViewFlipper,然後把要滾動的控制元件新增進去就可以了,當然首先得感謝各位前輩先寫好底層的東西~~~~

步驟:
第一現在佈局檔案裡面定義佈局,如下所示,可根據需要自己定義ViewFlipper
<ViewFlipper
    android:id="@+id/zy_fragment_vf"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/zy_fragment_information_lable"
    android:autoStart="true"  //是否自動開始
    android:background="#fff"
    android:flipInterval="5000"    //動畫的間隔
    android:inAnimation="@anim/anim_marquee_in"     //進來的動畫
    android:outAnimation="@anim/anim_marquee_out" //出去的動畫 />
anim_marquee_in.xml內容如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="1500"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

</set>

anim_marquee_out.xml內容如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="1500"
        android:fromYDelta="0"
        android:toYDelta="-100%p" />

</set>
第二在activity裡面添加布局  
for (int i = 0; i < list.size() / 2; i++) {
    View v1 = View.inflate(getContext(), R.layout.zy_fragment_information_item, null);
    TextView lable_1 = (TextView) v1.findViewById(R.id.zy_fragment_information_item_1_lable);
    TextView content_1 = (TextView) v1.findViewById(R.id.zy_fragment_information_item_1_content);
    TextView lable_2 = (TextView) v1.findViewById(R.id.zy_fragment_information_item_2_lable);
    TextView content_2 = (TextView) v1.findViewById(R.id.zy_fragment_information_item_2_content);
    showInformationHits(lable_1, list.get(i * 2).getHits());
    showInformationHits(lable_2, list.get(i * 2 + 1).getHits());
    content_1.setText(list.get(i * 2).getTitle());
    content_2.setText(list.get(i * 2 + 1).getTitle());
    final int finalI = i;
    content_1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            WebViewAct.startActivity(getContext(), list.get(finalI * 2).getUrl());
        }
    });
    content_2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            WebViewAct.startActivity(getContext(), list.get(finalI * 2 + 1).getUrl());
        }
    });
    mViewFlipper.addView(v1);

R.layout.zy_fragment_information_item裡面的佈局效果如下