android 的 ListView 实现无限循环滚动

目前很多人 都想知道android中的ListView是怎样实现无限循环滚动的, 现在给大家一个例子, 是项目中的一个模块。

项目需求:每个Activity的顶端显示一个ListView, 内容是赞助公司的名称和简要信息。

实现:
1 装有ListView的xml布局
<ListView android:id="@+id/history_list"
          android:layout_width="fill_parent"
          android:layout_height="0dip"
          android:fadeScrollbars="true"
          android:dividerHeight="2dip"
          android:layout_weight="6"></ListView>

2 实现滚屏的java类
package com.beiruanneng.util;

import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.beiruanneng.activity.R;

public class TimerTaskForListViewRolling extends TimerTask {
private ListView listView;
private int smoothBy = 1;
private Context context;
private List<Map<String, String>> sponsorList;

private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
listView.smoothScrollBy(smoothBy, 0);
};
};

public TimerTaskForListViewRolling(ListView listView, Context context, List<Map<String, String>> sponsorList) {
this.listView = listView;
this.context = context;
this.sponsorList = sponsorList;

listView.setAdapter(new MyBaseAdapter());
}
@Override
public void run() {
Message msg = handler.obtainMessage();
handler.sendMessage(msg);
}

private class MyBaseAdapter extends BaseAdapter {

@Override
public int getCount() {
return Integer.MAX_VALUE;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.sponsor_item, null);;
}

//找到listView中的item, 并且给每个item设置值
TextView sponsorNameTextView = (TextView) convertView.findViewById(R.id.sponsor_name);
TextView sponsorInfoTextView = (TextView) convertView.findViewById(R.id.sponsor_info);
Map<String, String> map = sponsorList.get(position%sponsorList.size());
String sponsorCompanyName = map.get(DataUtil.SPONSOR_NAME);
String sponsorCompanyInfo = map.get(DataUtil.SPONSOR_INFO);
sponsorNameTextView.setText(sponsorCompanyName);
sponsorInfoTextView.setText(sponsorCompanyInfo);

return convertView;
}

}

}


3 在Activity中直接调用:
//广告滚动效果
        new Timer().schedule(new TimerTaskForListViewRolling(sponsorCompanyList, this, DataUtil.getSponsorDataForListView()), 100, 100);

然后就完成了, 呵呵。


大家 有什么更好的想法, 请指教。

你可能感兴趣的:(android,ListView,滚屏)