項(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>
- 首頁布局(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>