android 條目向上滾動(dòng)效果實(shí)現(xiàn)

項(xiàng)目當(dāng)中用到了類似淘寶首頁淘寶頭條的一個(gè)效果。這里是通過viewswitcher這個(gè)類來實(shí)現(xiàn)的.內(nèi)容部分根基自己的接口提供的數(shù)據(jù)。
效果圖如下:


viewswitcher.gif

1.主要代碼如下:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import java.util.Timer;
import java.util.TimerTask;

import cn.com.ethank.mobilehotel.R;

public class MainActivity extends Activity {
    private ViewSwitcher mViewSwitcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        mViewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);
        mViewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return getLayoutInflater().inflate(R.layout.item_viewswitch, null);
            }
        });
        //設(shè)置切入動(dòng)畫
        TranslateAnimation animationTop = new TranslateAnimation(0, 0, 200, 0);
        animationTop.setFillAfter(true);
        animationTop.setDuration(200);
        //設(shè)置切出動(dòng)畫
        TranslateAnimation animationBottom = new
                TranslateAnimation(0, 0, 0, -200);
        animationBottom.setFillAfter(true);
        animationBottom.setDuration(200);
        mViewSwitcher.setInAnimation(animationTop);
        mViewSwitcher.setOutAnimation(animationBottom);
        mTimer.schedule(mTimerTask, 1000, 4000);
    }

    Timer mTimer = new Timer();
    TimerTask mTimerTask = new TimerTask() {
        @Override
        public void run() {
            mHandler.sendEmptyMessage(0);
        }
    };
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            ((TextView) mViewSwitcher.getNextView().findViewById(R.id.viewswitcher_tv_one)).setText("中包贈(zèng)送價(jià)值200元的套餐");
            ((TextView) mViewSwitcher.getNextView().findViewById(R.id.viewswitcher_tv_two)).setText("大包贈(zèng)送價(jià)值500元的套餐");
            mViewSwitcher.showNext();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }

}


2.布局代碼如下(item_viewswitch.xml)

<?xml version="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:id="@+id/viewswitcher_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<TextView
android:id="@+id/viewswitcher_tv_one"
        android:layout_marginTop="9dp"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:textSize="13dp"
        android:textColor="#474747"
        android:ellipsize="end"
        android:layout_height="wrap_content"/>

<TextView
android:layout_marginTop="10dp"
        android:id="@+id/viewswitcher_tv_two"
        android:layout_marginBottom="10dp"
        android:singleLine="true"
        android:gravity="bottom"
        android:ellipsize="end"
        android:textSize="13dp"
        android:textColor="#474747"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>
  1. 首頁布局(activity_test.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"              
android:orientation="vertical"              
android:layout_width="match_parent"              
android:layout_height="match_parent">    
<ViewSwitcher        
android:id="@+id/viewswitcher"        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:scrollbars="none"/>
</LinearLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容