BaseAdapter

关于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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chinabinlang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值