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"?>

用ViewFliper作为滚动布局的root,5000秒滚动。至于上下滚,左右滚,效果可自定义;
2.网络请求获取数据:
public void getPublicNotices(){
//网络请求获取
}后,通过handler来刷新view
此处我模拟了一个
protected void bindNotices();
动态添加子view;
3.

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.点击事件;






--------csdn博客内容插入有点难搞。。第一次插入图片代码。。有点乱。
 
  
 
  


 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(Android类似于滚动的通知栏实现)