歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android類似於滾動的通知欄實現

Android類似於滾動的通知欄實現

日期:2017/3/1 10:34:58   编辑:Linux編程

控件類似於網頁上的滾動播報欄

圖片1:


圖片2:


如上圖,實現滾動欄裡多條消息的自切換;

點擊後獲取具體內容。

簡單是實現代碼:

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

代碼簡單分析:
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. <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:layout_width="wrap_content" android:layout_marginRight="10dip"
  7. android:layout_height="fill_parent" android:src="@drawable/main_notice1"
  8. android:layout_gravity="center" android:gravity="center"/>
  9. <ViewFlipper android:layout_gravity="center" android:padding="5dip"
  10. android:id="@+id/flipper_scrollTitle" android:background="@drawable/main_notice_bg"
  11. android:layout_width="fill_parent" android:layout_height="fill_parent"
  12. android:layout_margin="0.0dip" android:flipInterval="5000"
  13. android:layout_weight="1.0">
  14. <TextView
  15. android:gravity="center" android:id="@+id/scrollTile_hd"
  16. android:layout_width="fill_parent" android:layout_height="fill_parent"
  17. android:text="@string/default_notice1"/>
  18. <TextView
  19. android:gravity="center" android:id="@+id/scrollTile_hm"
  20. android:layout_width="fill_parent" android:layout_height="fill_parent"
  21. android:text="@string/default_notice2" />
  22. </ViewFlipper>
  23. </LinearLayout>

用ViewFliper作為滾動布局的root,5000秒滾動。至於上下滾,左右滾,效果可自定義;

Copyright © Linux教程網 All Rights Reserved