美文网首页Android开发经验谈
listView.addHeadView的用处

listView.addHeadView的用处

作者: 炚風霁月 | 来源:发表于2017-01-04 20:13 被阅读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);

    }

    }

    相关文章

      网友评论

        本文标题:listView.addHeadView的用处

        本文链接:https://www.haomeiwen.com/subject/kskavttx.html