總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年1月7日 星期六

Marquee - ListView的跑馬燈效果








在layout加入以下屬性:
↓即使字串長度超過ListView的寬,也以單行顯示(超過的就算了)
android:singleLine="true"
↓設置跑馬燈功能
android:ellipsize="marquee"
↓跑馬燈次數無限制
android:marqueeRepeatLimit="marquee_forever"

最後必須取得焦點才能看到跑馬燈動起來~這裡只能選擇修改src, 加上setSelected(true), 完整程式碼如下:

Marquee_ListView.java

package com.tsots.Marquee_ListView;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

public class Marquee_ListView extends Activity 
{
 Context context = Marquee_ListView.this;
 ListView lv_marquee;
 Adapter_ListView adapter;
 String[] listview_Data = { "It was bitterly cold that night.",
          "The selfish old miser had a cold nature.",
          "Would you like a cold drink?"
        };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        lv_marquee = (ListView) findViewById (R.id.lv_marquee);
        adapter = new Adapter_ListView(this, android.R.layout.simple_list_item_1, listview_Data);
        lv_marquee.setAdapter(adapter);
    }
}


Adapter_ListView.java

package com.tsots.Marquee_ListView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Adapter_ListView extends BaseAdapter
{
 static final String TAG = "Adapter_ListView";
 private LayoutInflater mInflater;
 String[] adapter_listview_Data;
 TextView column1;
    
 public Adapter_ListView( Context context, 
                    int simple_list_item_single_choice, 
                    String[] listview_Data 
                  ) 
 {
  mInflater = LayoutInflater.from(context);
  adapter_listview_Data = listview_Data;
 }

 public int getCount()
 {
  return adapter_listview_Data.length;
 }

 public Object getItem(int position)
 {
  return adapter_listview_Data.length;
 }
  
 public long getItemId(int position)
 {
  return position;
 }
  
 public View getView(int position,View convertView,ViewGroup parent)
 {  
     convertView = mInflater.inflate(R.layout.simple_list_item_1_small,null);
     column1=(TextView)convertView.findViewById(R.id.column1);
     column1.setText(adapter_listview_Data[position].toString()); 
     //取得焦點
     column1.setSelected(true);
     return convertView;
 }
}


simple_list_item_1_small.xml


 
 

2 則留言: