日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2025/6/15 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android类似于滚动的通知栏实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

圖片1:

?

圖片2:

?

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

點擊后獲取具體內容。

簡單是實現代碼:

public class PublicNoticeView extends LinearLayout {

private static final String TAG = "LILITH";
private Context mContext;
private ViewFlipper viewFlipper;
private View scrollTitleView;
private Intent intent;

Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:

//bindNotices();
break;

case -1:
break;
}
}
};

/**
* 構造
*
@param context
*/
public PublicNoticeView(Context context) {
super(context);
mContext = context;
init();
}


public PublicNoticeView(Context context,AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();

}

/**
* 網絡請求后返回公告內容進行適配
*/
protected void bindNotices() {
// TODO Auto-generated method stub
viewFlipper.removeAllViews();
int i = 0;
while(i<5){
String text = "公告:中獎了 5000w-------";
TextView textView = new TextView(mContext);
textView.setText(text);
textView.setOnClickListener(new NoticeTitleOnClickListener(mContext,i+text));
LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
viewFlipper.addView(textView,lp);
i++;
}
}


private void init(){
bindLinearLayout();
Message msg = new Message();
msg.what = 1;
mHandler.sendMessageDelayed(msg, 3000);

}

/**
* 初始化自定義的布局
*/
public void bindLinearLayout() {
scrollTitleView = LayoutInflater.from(mContext).inflate(
R.layout.main_public_notice_title, null);
LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
addView(scrollTitleView, layoutParams);

viewFlipper = (ViewFlipper) scrollTitleView
.findViewById(R.id.flipper_scrollTitle);
viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left));
viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_out_right));
viewFlipper.startFlipping();
View v = viewFlipper.getCurrentView();

}


/**
* 獲取公告資訊
*/
public void getPublicNotices(){
//網絡請求獲取
}

/**
* 公告title監聽
*
@author Nono
*
*/
class NoticeTitleOnClickListener implements OnClickListener{
private Context context;
private String titleid;

public NoticeTitleOnClickListener(Context context, String whichText){
this.context = context;
this.titleid = whichText;
}
public void onClick(View v) {
// TODO Auto-generated method stub
disPlayNoticeContent(context,titleid);
}

}

/**
* 顯示notice的具體內容
*
@param context
*
@param titleid
*/
public void disPlayNoticeContent(Context context, String titleid) {
// TODO Auto-generated method stub
Toast.makeText(context, titleid, Toast.LENGTH_SHORT).show();
intent = new Intent(context, InformationContentActivity.class);
intent.putExtra("tag", titleid);
((Activity)context).startActivity(intent);
}

}

代碼簡單分析:
1.構造初始化,默認無網絡情況下客戶端兩條信息滾動(比如公司簡介,網址,以及一些介紹)。因為改兩條數據我是xml寫死的。沒做點擊處理。
具體布局xml:

?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:layout_height
="wrap_content" android:orientation="horizontal"
xmlns:android
="http://schemas.android.com/apk/res/android">

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="wrap_content" android:layout_marginRight="10dip"
android:layout_height
="fill_parent" android:src="@drawable/main_notice1"
android:layout_gravity
="center" android:gravity="center"/>
<ViewFlipper android:layout_gravity="center" android:padding="5dip"
android:id
="@+id/flipper_scrollTitle" android:background="@drawable/main_notice_bg"
android:layout_width
="fill_parent" android:layout_height="fill_parent"
android:layout_margin
="0.0dip" android:flipInterval="5000"
android:layout_weight
="1.0">

<TextView
android:gravity="center" android:id="@+id/scrollTile_hd"
android:layout_width
="fill_parent" android:layout_height="fill_parent"
android:text
="@string/default_notice1"/>
<TextView
android:gravity="center" android:id="@+id/scrollTile_hm"
android:layout_width
="fill_parent" android:layout_height="fill_parent"
android:text
="@string/default_notice2" />
</ViewFlipper>
</LinearLayout>

用ViewFliper作為滾動布局的root,5000秒滾動。至于上下滾,左右滾,效果可自定義;
2.網絡請求獲取數據:
public void getPublicNotices(){
//網絡請求獲取
}后,通過handler來刷新view
此處我模擬了一個
protected void bindNotices();
動態添加子view;
3.

<pre name="code" class="java"><pre name="code" class="java">protected void bindNotices() {
// TODO Auto-generated method stub
viewFlipper.removeAllViews();
int i = 0;
while(i<5){
String text = "公告:中獎了 5000w-------";
TextView textView = new TextView(mContext);
textView.setText(text);
textView.setOnClickListener(new NoticeTitleOnClickListener(mContext,i+text));
LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
viewFlipper.addView(textView,lp);
i++;
}
}

綁定前,我是把默認的兩個view去掉了。然后動態添加,并給每個view設置監聽事件
點擊可以以dialog或是activity顯示具體的數據和內容。


基本代碼如上


總結:1.自定義view;
2.簡單的借助了viewflipper控件;
3.動態添加view;
4.點擊事件;

原文:http://blog.csdn.net/nono_love_lilith/article/details/7074800#

?

?

?

轉載于:https://www.cnblogs.com/shanzei/archive/2012/04/06/2419384.html

總結

以上是生活随笔為你收集整理的Android类似于滚动的通知栏实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。