ViewFlipper实现轮播公告

首先来看实现效果:


自定义view:

public class ShufflingView extends ViewFlipper implements View.OnClickListener {
    private Context                       mContext;
    private List<String>                  mShufflingList;
    private onShufflingChildClickListener mShufflingChildClickListener;

    public ShufflingView(Context context) {
        super(context);
        init(context);
    }


    public ShufflingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        //set padding
        setPadding(dp2px(10), dp2px(5), dp2px(10), dp2px(5));
        //set in out animation
        setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.shuffling_in));
        setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.shuffling_out));

    }

    /**
     * 添加轮播的view
     *
     * @param shufflingList
     */
    public void addShufflingView(List<String> shufflingList) {
        if (shufflingList != null && shufflingList.size() > 0) {
            this.mShufflingList = shufflingList;
            for (int i = 0; i < shufflingList.size(); i++) {
                TextView textView = new TextView(mContext);
                textView.setText(shufflingList.get(i));
                textView.setSingleLine();
                textView.setTextSize(15f);
                textView.setGravity(Gravity.CENTER_VERTICAL);
                textView.setEllipsize(TextUtils.TruncateAt.END);
                textView.setTextColor(Color.parseColor("#ff00ff"));
                //设置tag,用于获取position
                textView.setTag(i);
                textView.setOnClickListener(this);
                //add textView to ViewFlipper
                ShufflingView.this.addView(textView, 
				new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            }
        }
    }


    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        String content = mShufflingList.get(position);
        if (mShufflingChildClickListener != null) {
            mShufflingChildClickListener.onShufflingChildClick(position, content);
        }
    }


    /**
     * 设置轮播时间
     *
     * @param time
     */
    public void setInterval(int time) {
        //set shuffling interval
        setFlipInterval(time);
    }

    /**
     * dp --> px
     *
     * @param dpValue
     * @return
     */
    private int dp2px(float dpValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpValue,
                mContext.getResources().getDisplayMetrics());
    }


    public void setOnShufflingChildClickListener(onShufflingChildClickListener clickListener) {
        this.mShufflingChildClickListener = clickListener;
    }

    interface onShufflingChildClickListener {
        void onShufflingChildClick(int position, String content);
    }
}

在xml中使用:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.getphoto.MainActivity">

    <TextView android:id="@+id/main_tv"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_centerInParent="true"
              android:layout_marginLeft="30dp"
              android:text="小二快报:"
              android:textSize="15sp"/>

    <com.example.getphoto.ShufflingView
        android:id="@+id/main_shuffling"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@id/main_tv"/>
</RelativeLayout>

在activity中使用:

ShufflingView mainShuffling = (ShufflingView) findViewById(R.id.main_shuffling);
        mainShuffling.setInterval(2000);
        List<String> list = Arrays.asList(
                "震惊!老汉半夜不睡觉是为啥",
                "突发!日本居然6月下大雪",
                "不敢相信!她们竟敢这么做!",
                "震惊!隔壁居然住的是老王",
                "欢迎走进科学栏目...");
        mainShuffling.addShufflingView(list);
        mainShuffling.startFlipping();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值