欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android程序开发之ListView实现横向滚动(带表头与固定列)

程序员文章站 2024-03-03 22:31:58
问题背景:在做图表展示的时候,listview可以上下左右滑动,但最左边一列在向右滑动时,保持不变,表头在向下滑动时保持不变。 有用两个listview实现的,但测试过,...

问题背景:在做图表展示的时候,listview可以上下左右滑动,但最左边一列在向右滑动时,保持不变,表头在向下滑动时保持不变。

有用两个listview实现的,但测试过,好像有些问题

这个例子是通过(listview + horizontalscrollview)实现的

效果图:

 Android程序开发之ListView实现横向滚动(带表头与固定列)

activity代码

/** 
* 
* 带滑动表头与固定列的listview 
*/ 
public class hlistactivity extends activity{ 
private listview mlistview; 
//方便测试,直接写的public 
public horizontalscrollview mtouchview; 
//装入所有的hscrollview 
protected list<chscrollview> mhscrollviews =new arraylist<chscrollview>(); 
@override 
protected void oncreate(bundle savedinstancestate) { 
super.oncreate(savedinstancestate); 
setcontentview(r.layout.scroll); 
initviews(); 
} 
private void initviews() { 
list<map<string, string>> datas = new arraylist<map<string,string>>(); 
map<string, string> data = null; 
chscrollview headerscroll = (chscrollview) findviewbyid(r.id.item_scroll_title); 
//添加头滑动事件 
mhscrollviews.add(headerscroll); 
mlistview = (listview) findviewbyid(r.id.scroll_list); 
for(int i = 0; i < 100; i++) { 
data = new hashmap<string, string>(); 
data.put("title", "title_" + i); 
data.put("data_" + 1, "date_" + 1 + "_" +i ); 
data.put("data_" + 2, "date_" + 2 + "_" +i ); 
data.put("data_" + 3, "date_" + 3 + "_" +i ); 
data.put("data_" + 4, "date_" + 4 + "_" +i ); 
data.put("data_" + 5, "date_" + 5 + "_" +i ); 
data.put("data_" + 6, "date_" + 6 + "_" +i ); 
datas.add(data); 
} 
simpleadapter adapter = new scrolladapter(this, datas, r.layout.item 
, new string[] { "title", "data_1", "data_2", "data_3", "data_4", "data_5", "data_6", } 
, new int[] { r.id.item_title 
, r.id.item_data1 
, r.id.item_data2 
, r.id.item_data3 
, r.id.item_data4 
, r.id.item_data5 
, r.id.item_data6 }); 
mlistview.setadapter(adapter); 
} 
public void addhviews(final chscrollview hscrollview) { 
if(!mhscrollviews.isempty()) { 
int size = mhscrollviews.size(); 
chscrollview scrollview = mhscrollviews.get(size - 1); 
final int scrollx = scrollview.getscrollx(); 
//第一次满屏后,向下滑动,有一条数据在开始时未加入 
if(scrollx != 0) { 
mlistview.post(new runnable() { 
@override 
public void run() { 
//当listview刷新完成之后,把该条移动到最终位置 
hscrollview.scrollto(scrollx, 0); 
} 
}); 
} 
} 
mhscrollviews.add(hscrollview); 
} 
public void onscrollchanged(int l, int t, int oldl, int oldt){ 
for(chscrollview scrollview : mhscrollviews) { 
//防止重复滑动 
if(mtouchview != scrollview) 
scrollview.smoothscrollto(l, t); 
} 
} 
class scrolladapter extends simpleadapter { 
private list<? extends map<string, ?>> datas; 
private int res; 
private string[] from; 
private int[] to; 
private context context; 
public scrolladapter(context context, 
list<? extends map<string, ?>> data, int resource, 
string[] from, int[] to) { 
super(context, data, resource, from, to); 
this.context = context; 
this.datas = data; 
this.res = resource; 
this.from = from; 
this.to = to; 
} 
@override 
public view getview(int position, view convertview, viewgroup parent) { 
view v = convertview; 
if(v == null) { 
v = layoutinflater.from(context).inflate(res, null); 
//第一次初始化的时候装进来 
addhviews((chscrollview) v.findviewbyid(r.id.item_scroll)); 
view[] views = new view[to.length]; 
for(int i = 0; i < to.length; i++) { 
view tv = v.findviewbyid(to[i]);; 
tv.setonclicklistener(clicklistener); 
views[i] = tv; 
} 
v.settag(views); 
} 
view[] holders = (view[]) v.gettag(); 
int len = holders.length; 
for(int i = 0 ; i < len; i++) { 
((textview)holders[i]).settext(this.datas.get(position).get(from[i]).tostring()); 
} 
return v; 
} 
} 
//测试点击的事件 
protected view.onclicklistener clicklistener = new view.onclicklistener() { 
@override 
public void onclick(view v) { 
toast.maketext(hlistactivity.this, ((textview)v).gettext(), toast.length_short).show(); 
} 
}; 
} 

滑动view代码

/** 
* 滑动代码 
*/ 
public class chscrollview extends horizontalscrollview { 
hlistactivity activity; 
public chscrollview(context context, attributeset attrs, int defstyle) { 
super(context, attrs, defstyle); 
activity = (hlistactivity) context; 
} 
public chscrollview(context context, attributeset attrs) { 
super(context, attrs); 
activity = (hlistactivity) context; 
} 
public chscrollview(context context) { 
super(context); 
activity = (hlistactivity) context; 
} 
@override 
public boolean ontouchevent(motionevent ev) { 
//进行触摸赋值 
activity.mtouchview = this; 
return super.ontouchevent(ev); 
} 
@override 
protected void onscrollchanged(int l, int t, int oldl, int oldt) { 
//当当前的chscrollview被触摸时,滑动其它 
if(activity.mtouchview == this) { 
activity.onscrollchanged(l, t, oldl, oldt); 
}else{ 
super.onscrollchanged(l, t, oldl, oldt); 
} 
} 
} 

以上所述是小编给大家介绍的android程序开发之listview实现横向滚动(带表头与固定列),希望对大家有所帮助