BaseAdapter

来源:互联网 发布:长空栈道 知乎 编辑:程序博客网 时间:2024/05/19 14:37

关于BaseAdapter简单应用可以参考《Android讲义》这本书;


这里主要说一下BaseAdapter的应用;


在Android studio中可以在MainActivity类中直接通过匿名类的方法实现继承BaseAdapter类,这样可以方便的应用Context参数,就是直接用主窗口的this就可以了;

但是一般为了更好的模块化,可以右键直接创建java class,然后 extendsBaseAdapter就可以了;但是如果要用Context,可以同构造函数直接传参数,或是添加一个set函数添加;

然后通过快捷键,“Alt + Insert”,实现Implement需要实现的函数;


重要的函数有

public int getCount() :返回值表示了显示的列表数; 通过函数参数修改返回值,然后重新ListView.setAdapter重新加载显示列表控件;


public View getView(int position, View convertView, ViewGroup parent) :列表中显示View的; 
因为Android中的所有控件都是View包括Layout,所以可以返回非常复杂的列表Item;

position参数,表示当前加载的Viewd的索引序号值;



public class TestBaseAdpter extends BaseAdapter{    public TestBaseAdpter( Context pContext  )    {        m_pContext = pContext;    }    Context m_pContext;    /**     * How many items are in the data set represented by this Adapter.     *     * @return Count of items.     */    @Override    public int getCount()    {        return  1000;    }    /**     * Get the data item associated with the specified position in the data set.     *     * @param position Position of the item whose data we want within the adapter's     *                 data set.     * @return The data at the specified position.     */    @Override    public Object getItem(int position)    {        return null;    }    /**     * Get the row id associated with the specified position in the list.     *     * @param position The position of the item within the adapter's data set whose row id we want.     * @return The id of the item at the specified position.     */    @Override    public long getItemId(int position)    {        return 0;    }    /**     * Get a View that displays the data at the specified position in the data set. You can either     * create a View manually or inflate it from an XML layout file. When the View is inflated, the     * parent View (GridView, ListView...) will apply default layout parameters unless you use     * {@link LayoutInflater#inflate(int, ViewGroup, boolean)}     * to specify a root view and to prevent attachment to the root.     *     * @param position    The position of the item within the adapter's data set of the item whose view     *                    we want.     * @param convertView The old view to reuse, if possible. Note: You should check that this view     *                    is non-null and of an appropriate type before using. If it is not possible to convert     *                    this view to display the correct data, this method can create a new view.     *                    Heterogeneous lists can specify their number of view types, so that this View is     *                    always of the right type (see {@link #getViewTypeCount()} and     *                    {@link #getItemViewType(int)}).     * @param parent      The parent that this view will eventually be attached to     * @return A View corresponding to the data at the specified position.     */    @Override    public View getView(int position, View convertView, ViewGroup parent)    {        TextView pView = new TextView( m_pContext );        pView.setText( "新建字符串" );        return  pView;    }}






如果要查看复杂的设计方法, 一般可以添加 List链表,返回的是List.count(), 这样就可以比较容易动态控制链表,具体可以参考Android中的ArrayAdapter;


0 0