Android ListView和BaseAdapter

为什么80%的码农都做不了架构师?>>>   hot3.png

1.

        android:entries="@array/..."

        android:divider="#ccc"/"图片"

        android:dividerHeight="10dp"/>

2.class MyAdapter extends BaseAdapter {

    private Context context;

    private ArrayList list;

    public MyAdapter(Context context, ArrayList list) {

        this.context = context;

        this.list = list;

    }

    public int getCount() {

        return list.size()

    }

    public Object getItem(int postion) {

        return list.get(postion);

    }

    public long getId(int postion) {

        return postion;

    }

    public View getView(int postion, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if(convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.item_layout, null);    //第二个参数表示加到哪个父节点,跟生命周期有关

            viewHolder = new ViewHolder();

            viewHolder.name = covertView.findViewById(R.id.name);

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder)convertView.getTag();

        }

        Bean bean = list.get(postion);

        viewHolder.name.setText(bean.getName());

        return convertView;

    }

    class ViewHolder {

        TextView name;

    }

}

3. lv.setOnItemClickListener(new onItemClickListener {

        public void onItemClick(AdapterView parent,View view,int postion,long id) {

                parent.getItemAtPosition(postion)为适配器中getItem()的返回值;

                View为适配器中getView()的返回值;

                id为适配器中getItemId()的返回值;

        }

    })

    

4.ListView监听器2:OnScrollListener

    onScrollStateChanged(AbsListView view, int scrollState) {

            //scrollState: SCROLL_STATE_TOUCH_SCROLL、SCROLL_STATE_FLING、SCROLL_STATE_IDLE(停止滑动)

    }

    onScroll(AbsListView view,int firstVisibleItem, int visbleItemCount, int totalItemCount) {}

5.lv.addHeaderView(布局或view);    //放在绑定适配器之前可添加多个

   lv.addFooterView(view);

   lv.setEmptyView(view);        //view必须先存在Listview所在布局里,并“gone”。

6.GridView(控件)绑定适配器等与Listview相同,只是不能添加HeaderView、FooterView.

   

        android:numColumns="2"

        ...        />

转载于:https://my.oschina.net/glfei/blog/3008044

你可能感兴趣的:(Android ListView和BaseAdapter)