Android 上下滚动的新闻效果

类似于淘宝中的淘宝头条

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 实现上下循环滚动通知栏

你可能感兴趣的:(Android)