类似于淘宝中的淘宝头条

ViewFlipper
布局文件:

<LinearLayout                android:id="@+id/ll_vf"                android:layout_width="match_parent"                android:layout_height="60dp"                android:layout_marginBottom="25dp"                android:background="@color/white"                android:gravity="center"                android:paddingLeft="@dimen/content_space_h"                android:paddingRight="@dimen/content_space_h">                <ViewFlipper                    android:id="@+id/vf_notice"                    android:layout_width="match_parent"                    android:layout_height="wrap_content"/>            LinearLayout>

java代码:

    @ViewInject(R.id.ll_vf)    private LinearLayout ll_vf;    @ViewInject(R.id.vf_notice)    private ViewFlipper vf_notice;    private int mCurrPos;    private int mCurrPos;    private Timer timer;    private TimerTask task;    // 其中data为传入的数据    private void initRollNotice(final List.PrizeList.Rows> data) {        task = new TimerTask() {            @Override            public void run() {                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        moveNext(data);                    }                });            }        };        timer = new Timer();        timer.schedule(task, 0, 5000);    }    private void moveNext(List.PrizeList.Rows> data) {        setView(data, this.mCurrPos, this.mCurrPos + 1);        this.vf_notice.setInAnimation(this, R.anim.in_bottom);        this.vf_notice.setOutAnimation(this, R.anim.out_top);        this.vf_notice.showNext();    }    private void setView(List.PrizeList.Rows> data, int curr, int next) {        View noticeView = getLayoutInflater().inflate(R.layout.item_draw_notice, null);        ImageView iv_head_pic = (ImageView) noticeView.findViewById(R.id.iv_head_pic);        TextView tv_name = (TextView) noticeView.findViewById(R.id.tv_name);        TextView tv_content = (TextView) noticeView.findViewById(R.id.tv_content);        TextView tv_time = (TextView) noticeView.findViewById(R.id.tv_time);        if ((curr < next) && (next > (data.size() - 1))) {            next = 0;        } else if ((curr > next) && (next < 0)) {            next = data.size() - 1;        }        Picasso.with(this).load(data.get(next).picurl)                .placeholder(R.drawable.img_res_logo)                .error(R.drawable.img_res_logo)                .into(iv_head_pic);        tv_name.setText(data.get(next).nickname + " 获得");        tv_content.setText(data.get(next).prize_name);        tv_time.setText(Tools.formatTime(data.get(next).time));        if (vf_notice.getChildCount() > 1) {            vf_notice.removeViewAt(0);        }        vf_notice.addView(noticeView, vf_notice.getChildCount());        mCurrPos = next;    }

两个动画:

in_bottom

<set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:duration="500"        android:fromYDelta="100.0%p"        android:toYDelta="0.0" />    <alpha        android:duration="500"        android:fromAlpha="0.0"        android:toAlpha="1.0" />set>

out_top

<set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:duration="500"        android:fromYDelta="0.0"        android:toYDelta="-100.0%p"/>    <alpha        android:duration="500"        android:fromAlpha="1.0"        android:toAlpha="0.0"/>set>

Item的点击事件

    if(vf_notice.getDisplayedChild()==0){                  vf_notice.getCurrentView().setOnClickListener(new OnClickListener() {                      @Override                      public void onClick(View arg0) {                          // TODO Auto-generated method stub                      }                  });              }  
//取消task 可要可不要   @Override    protected void onDestroy() {        super.onDestroy();        if (task != null) {            task.cancel();            timer.cancel();            timer = null;        }    }

这里再保存一个别人写的方法:

TextView滚动效果

上下滚动

参考文章:
[Android] 使用 ViewFlipper 实现上下循环滚动通知栏

更多相关文章

  1. Android在程序中动态生成控件,动态布局
  2. android 相对布局例子代码
  3. Android Studio 添加百分比布局库的依赖报错 第一行代码
  4. 简单布局收缩动画
  5. Android 基础布局控件自定义view使用练习
  6. 安卓自定义流式布局

随机推荐

  1. APPS大乱斗:4大Android文件浏览器横评(一)
  2. 在Ubuntu上下载、编译和安装Android最新
  3. android常用调试工具fiddle、wireshark和
  4. Android(安卓)PK ios,是谁胜谁负
  5. 【Android(安卓)多语言切换简单实例分享
  6. Android周学习Step By Step(4)--界面布局
  7. Android(安卓)cardview覆盖问题
  8. android apk安装原理分析
  9. Android系统:见证“开源”的力量
  10. Android(安卓)的提权 (root) 原理是什么?