控件類似於網頁上的滾動播報欄
圖片1:
圖片2:
如上圖,實現滾動欄裡多條消息的自切換;
點擊後獲取具體內容。
簡單是實現代碼:
[html]
- 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:
[html]
- ?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秒滾動。至於上下滾,左右滾,效果可自定義;