Android开发经验谈

listView.addHeadView的用处

2017-01-04  本文已影响0人  炚風霁月

需求:完成如下界面布局

listView.jpg

/**

* Layout container for a view hierarchy that can be scrolled by the user,

* allowing it to be larger than the physical display.  A ScrollView

* is a {@linkFrameLayout}, meaning you should place one child in it

* containing the entire contents to scroll; this child may itself be a layout

* manager with a complex hierarchy of objects.  A child that is often used

* is a {@linkLinearLayout} in a vertical orientation, presenting a vertical

* array of top-level items that the user can scroll through.

*

You should never use a ScrollView with a {@linkListView}, because

* ListView takes care of its own vertical scrolling.  Most importantly, doing this

* defeats all of the important optimizations in ListView for dealing with

* large lists, since it effectively forces the ListView to display its entire

* list of items to fill up the infinite container supplied by ScrollView.

*

The {@linkTextView} class also

* takes care of its own scrolling, so does not require a ScrollView, but

* using the two together is possible to achieve the effect of a text view

* within a larger container.

*

*

ScrollView only supports vertical scrolling. For horizontal scrolling,

* use {@linkHorizontalScrollView}.

*

*@attrref android.R.styleable#ScrollView_fillViewport

*/

ScrollView的特点

1.只能嵌套一个控件

2.不能和ListView嵌套使用,因为都是垂直滑动,从而获取不到焦点

3.支持垂直和水平

public class ListViewActivity extends Activity {

privateListView lv;

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

setContentView (R.layout.activity_listview);

initView ();

}

private voidinitView() {

lv = (ListView) findViewById (R.id.lv);

ArrayList datas =newArrayList ();

for(inti =0; i <50; i++) {

datas.add ("迎接2017年"+i);

}

LayoutInflater inflater = LayoutInflater.from(this);

View view = inflater.inflate (R.layout.listview_head, lv,false);//可设置头布局宽和高

lv.addHeaderView (view);//添加布局到ListView中

ArrayAdapter adapter =newArrayAdapter (this, android.R.layout.simple_list_item_1, datas);

//        lv.setFocusable (false);

lv.setAdapter (adapter);

}

}

上一篇下一篇

猜你喜欢

热点阅读