Android类似于滚动通知信息播报栏通知功能如下:
该功能长用于手机顶端播放信息:
先上源码:
==(1)关键代码
public class PublicNoticeView extends LinearLayout {
private static final String TAG = "WSL";
private Context mcontext;
private ViewFlipper viewFlipper;
private View scrollTitleView;
private Intent intent;
Handler mhandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
bindNotices();
break;
case -1:
break;
default:
break;
}
}
};
public PublicNoticeView(Context context) {
super(context);
mcontext = context;
init();
}
public PublicNoticeView(Context context, AttributeSet attrs) {
super(context, attrs);
mcontext = context;
init();
}
// 初始化自定义布局
public void bindLinearLayout() {
scrollTitleView = LayoutInflater.from(mcontext).inflate(R.layout.ll,
null);
LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
addView(scrollTitleView, params);
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();
}
private void init() {
bindLinearLayout();
Message msg = new Message();
msg.what = 1;
mhandler.sendMessageDelayed(msg, 3000);
}
/**
* 网络请求后返回公告内容进行适配
*/
private void bindNotices() {
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++;
}
}
/**
* 获取公告资讯
*/
public void getPublicNotices() {
// 网络请求获取
}
/**
* 公告title监听
*
* @author wsl
*
*/
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);
}
}
package com.example.gundongtongzhilan;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
==
<LinearLayout 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=".MainActivity" >
<com.example.gundongtongzhilan.PublicNoticeView
android:id="@+id/viewf"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></com.example.gundongtongzhilan.PublicNoticeView>
</LinearLayout>
===(3)activity
package com.example.gundongtongzhilan;
import android.app.Activity;
import android.os.Bundle;
public class InformationContentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
==
<LinearLayout 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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</LinearLayout>
=========================
总结:
/**
* 1.用ViewFliper作为滚动布局的root,5000秒滚动。至于上下滚,左右滚,效果可自定义;
* 2.网络请求获取数据:
* public void getPublicNotices(){ //网络请求获取 }后,通过handler来刷新view
* 此处模拟了一个 protected void bindNotices(); 动态添加子view;
* 绑定前,把默认的两个view去掉了。然后动态添加,并给每个view设置监听事件<BR>
* 点击可以以dialog或是activity显示具体的数据和内容
* ============================================
* ============================================
* 总结:1.自定义view;<BR>
* 2.简单的借助了viewflipper控件;<BR>
* 3.动态添加view;<BR>
* 4.点击事件;<BR>
*
源码下载: