Android类似于滚动的通知栏实现


控件类似于网页上的滚动播报栏

图片1:


图片2:


如上图,实现滚动栏里多条消息的自切换;

点击后获取具体内容。

简单是实现代码:

[html]
  1. public class PublicNoticeView extends LinearLayout {  
  2.   
  3.     private static final String TAG = "LILITH";   
  4.     private Context mContext;  
  5.     private ViewFlipper viewFlipper;  
  6.     private View scrollTitleView;  
  7.     private Intent intent;  
  8.       
  9.     Handler mHandler = new Handler(){  
  10.         @Override  
  11.         public void handleMessage(Message msg) {  
  12.             // TODO Auto-generated method stub  
  13.             switch (msg.what) {  
  14.             case 1:  
  15.                   
  16.                 //bindNotices();  
  17.                 break;  
  18.   
  19.             case -1:  
  20.                 break;  
  21.             }  
  22.         }  
  23.     };  
  24.   
  25.     /**  
  26.      * 构造  
  27.      * @param context  
  28.      */  
  29.     public PublicNoticeView(Context context) {  
  30.         super(context);  
  31.         mContext = context;  
  32.         init();   
  33.     }  
  34.       
  35.   
  36.     public PublicNoticeView(Context context,AttributeSet attrs) {  
  37.         super(context, attrs);  
  38.         mContext = context;  
  39.         init();  
  40.           
  41.     }  
  42.       
  43.     /**  
  44.      * 网络请求后返回公告内容进行适配  
  45.      */  
  46.     protected void bindNotices() {  
  47.         // TODO Auto-generated method stub  
  48.         viewFlipper.removeAllViews();  
  49.         int i = 0;  
  50.         while(i<5){  
  51.             String text = "公告:中奖了 5000w-------";  
  52.             TextView textView = new TextView(mContext);  
  53.             textView.setText(text);  
  54.             textView.setOnClickListener(new NoticeTitleOnClickListener(mContext,i+text));  
  55.             LayoutParams lp = new LinearLayout.LayoutParams(  
  56.                     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  57.             viewFlipper.addView(textView,lp);  
  58.             i++;  
  59.         }  
  60.     }  
  61.   
  62.   
  63.     private void init(){  
  64.         bindLinearLayout();  
  65.         Message msg = new Message();  
  66.         msg.what = 1;  
  67.         mHandler.sendMessageDelayed(msg, 3000);  
  68.           
  69.     }  
  70.   
  71.     /**  
  72.      * 初始化自定义的布局  
  73.      */  
  74.     public void bindLinearLayout() {  
  75.         scrollTitleView = LayoutInflater.from(mContext).inflate(  
  76.                 R.layout.main_public_notice_title, null);  
  77.         LayoutParams layoutParams = new LinearLayout.LayoutParams(  
  78.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  79.         addView(scrollTitleView, layoutParams);  
  80.   
  81.         viewFlipper = (ViewFlipper) scrollTitleView  
  82.                 .findViewById(R.id.flipper_scrollTitle);  
  83.         viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, Android.R.anim.slide_in_left));  
  84.         viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_out_right));  
  85.         viewFlipper.startFlipping();  
  86.         View v = viewFlipper.getCurrentView();  
  87.           
  88.     }  
  89.       
  90.       
  91.     /**  
  92.      * 获取公告资讯  
  93.      */  
  94.     public void getPublicNotices(){  
  95.         //网络请求获取  
  96.     }  
  97.       
  98.     /**  
  99.      * 公告title监听  
  100.      * @author Nono  
  101.      *  
  102.      */  
  103.     class NoticeTitleOnClickListener implements OnClickListener{  
  104.         private Context context;  
  105.         private String titleid;  
  106.   
  107.         public NoticeTitleOnClickListener(Context context, String whichText){  
  108.             this.context = context;  
  109.             this.titleid = whichText;  
  110.         }  
  111.         public void onClick(View v) {  
  112.             // TODO Auto-generated method stub  
  113.             disPlayNoticeContent(context,titleid);  
  114.         }  
  115.           
  116.     }  
  117.   
  118.     /**  
  119.      * 显示notice的具体内容  
  120.      * @param context  
  121.      * @param titleid  
  122.      */  
  123.     public void disPlayNoticeContent(Context context, String titleid) {  
  124.         // TODO Auto-generated method stub  
  125.         Toast.makeText(context, titleid, Toast.LENGTH_SHORT).show();  
  126.         intent = new Intent(context, InformationContentActivity.class);  
  127.         intent.putExtra("tag", titleid);  
  128.         ((Activity)context).startActivity(intent);  
  129.     }  
  130.   
  131. }  

代码简单分析:
1.构造初始化,默认无网络情况下客户端两条信息滚动(比如公司简介,网址,以及一些介绍)。因为改两条数据我是xml写死的。没做点击处理。
具体布局xml:

[html]
  1. ?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout android:layout_width="fill_parent"  
  3.     android:layout_height="wrap_content" android:orientation="horizontal"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android">  
  5.       
  6.         <ImageView xmlns:android="http://schemas.android.com/apk/res/android"  
  7.          android:layout_width="wrap_content" android:layout_marginRight="10dip"  
  8.         android:layout_height="fill_parent" android:src="@drawable/main_notice1"  
  9.         android:layout_gravity="center" android:gravity="center"/>  
  10.     <ViewFlipper android:layout_gravity="center" android:padding="5dip"  
  11.         android:id="@+id/flipper_scrollTitle" android:background="@drawable/main_notice_bg"  
  12.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  13.         android:layout_margin="0.0dip" android:flipInterval="5000"  
  14.         android:layout_weight="1.0">  
  15.           
  16.         <TextView   
  17.             android:gravity="center" android:id="@+id/scrollTile_hd"  
  18.             android:layout_width="fill_parent" android:layout_height="fill_parent"  
  19.             android:text="@string/default_notice1"/>  
  20.         <TextView   
  21.             android:gravity="center" android:id="@+id/scrollTile_hm"  
  22.             android:layout_width="fill_parent" android:layout_height="fill_parent"  
  23.             android:text="@string/default_notice2" />  
  24.     </ViewFlipper>  
  25. </LinearLayout>  

用ViewFliper作为滚动布局的root,5000秒滚动。至于上下滚,左右滚,效果可自定义;

  • 1
  • 2
  • 下一页

相关内容