listView.addHeadView的用處

需求:完成如下界面布局


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的特點(diǎn)

1.只能嵌套一個(gè)控件

2.不能和ListView嵌套使用,因?yàn)槎际谴怪被瑒?dòng),從而獲取不到焦點(diǎn)

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);//可設(shè)置頭布局寬和高

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

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

//? ? ? ? lv.setFocusable (false);

lv.setAdapter (adapter);

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容