Android 深入理解BaseAdapter和实现ListView并处理点击事件

这里主要讲解使用BaseAdapter来实现对ListView的Item内容自定义填充,重写后各个方法的作用,以及Item点击事件。

查看源码可以看到BaseAdapter实现了ListAdapter,SpinnerAdapter这两个接口,继续查看,发现这两个接口继承于接口Adapter。

废话不多说,上源码。

方法int getcount()的源码:

 /**
     * How many items are in the data set represented by this Adapter.
     * 
     * @return Count of items.
     */
    int getCount();   

意思大概是:此适配器所代表的数据集中有多少项目,返回适配器的item数目。

方法long getItem(int position)的源码:

/**
     * 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.
     */
    Object getItem(int position);

意思大概是:获取与数据集中指定位置关联的数据项。

                    position参数解释:我们想要适配器中的数据集的项目位置,即position。返回指定位置的数据,这里的指定位置是指点击的位置。

方法long getItemId(int position)的源码:

/**
     * 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.
     */
    long getItemId(int position);

意思大概是:获取与列表中指定位置关联的行ID。

                     position参数解释:我们想要指定item位置的适配器的数据集的行id。返回指定位置的item的id,即点击位置位置的item的行id。(其实这个id和你传到Adapter的数据集的相对应的顺序id一致。)

方法View getView(final int position, View convertView, ViewGroup parent)的源码:

 /**
     * 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 android.view.LayoutInflater#inflate(int, android.view.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.
     */
    View getView(int position, View convertView, ViewGroup parent);

大概意思是:得到显示的数据集的指定位置的数据视图。你也可以手动创建一个视图(即显示在屏幕上,这里不知道翻译成什么好)或者填充一个xml布局文件。当视图被填充,父视图将会应用默认的布局参数,除非你使用方法inflate(int,android.view.ViewGroup,boolean)来指定一个根视图并防止附加到根视图上。

                    position参数解释:在视图中我们想要的适配器的数据集的item的位置。

                    convertView参数解释:如果可能的话,旧的视图重用。注意:你应该在使用之前检查这个视图是非空的并且是合适的类型。如果无法将此视图转换为显示正确的数据,则此方法可以创建新视图。异构列表可以指定其视图类型的数量,以便此视图总是正确的类型(请参阅{@link #getViewTypeCount()}和{@link #getItemViewType(int)})。

                    parent参数解释:此视图将会被附加到父视图。

                    返回值:返回一个与指定位置数据相对应的视图。(即我们填充视图)



总结一下:

1   以上的position参数都是一样,都为点击的item的id,只不过方法返回的值不一样,一个返回数据集即一个对象,一个返回id。

2   在get View()方法中最好检查一下旧的视图是否为空。然后再决定是否创建新视图。

    这里的旧视图解释一下:在初始显示的时候,每次显示一个item都调用一次getview方法但是每次调用的时候convertview为空(因为还没有旧的view),当显示完了之后。如果屏幕移动了之后,并且导致有些Item(也可以说是view)跑到屏幕外面,此时如果还有新的item需要产生,则这些item显示时调用的getview方法中的convertview参数就不是null,而是那些移出屏幕的view(旧view),我们所要做的就是将需要显示的item填充到这些回收的view(旧view)中去,最后注意convertview为null的不仅仅是初始显示的那些item,还有一些是已经开始移入屏幕但是还没有view被回收的那些item。

这里详细的converView参数解析可以看这篇博文,解析的很好,也涉及了对converview数据的更新操作。

    链接:http://home.bdqn.cn/thread-54626-1-1.html



接下来是点击事件:

这个直接在getview中处理就行了,

 view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Context, xxx.class); 
                .........................................
                mContext.startActivity(intent);
            }
        });

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值