项目当中用到了类似淘宝首页淘宝头条的一个效果。这里是通过viewswitcher这个类来实现的.内容部分根基自己的接口提供的数据。
效果图如下:
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);
}
});
//设置切入动画
TranslateAnimation animationTop = new TranslateAnimation(0, 0, 200, 0);
animationTop.setFillAfter(true);
animationTop.setDuration(200);
//设置切出动画
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("中包赠送价值200元的套餐");
((TextView) mViewSwitcher.getNextView().findViewById(R.id.viewswitcher_tv_two)).setText("大包赠送价值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>
网友评论