浅析Adapter中hasStableIds的作用

QuibblerQuibbler 2024-08-27 约 10 分钟 1906 次阅读

浅析Adapter中hasStableIds的作用

1、ListView Adapter中的hasStableIds

在使用ListView的时候需要自定义Adapter,一般是继承Adapter或者其子类比如BaseAdapter,并实现方法。其中有两个方法大家一定不陌生:其中一个是hasStableIds():

/**
     * Indicates whether the item ids are stable across changes to the
     * underlying data.
     * 
     * @return True if the same id always refers to the same object.
     */
    boolean hasStableIds();

在众多Adapter的子类中默认实现都是返回false:

public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter {
        public boolean hasStableIds() {
            return false;
        }
	...
    }

另一个方法则是getItemId(int position),需要开发者重写,一般都是直接返回传入的当前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);

2、RecyclerView Adapter中的hasStableIds

同样,在RecyclerView中的Adapter也用到了hasStableIds,不过不是抽象方法,而是改成了私有成员变量mHasStableIds,且默认是false:

/**
     * Base class for an Adapter
     *
     * <p>Adapters provide a binding from an app-specific data set to views that are displayed
     * within a {@link RecyclerView}.</p>
     *
     * @param <VH> A class that extends ViewHolder that will be used by the adapter.
     */
    public abstract static class Adapter<VH extends ViewHolder> {
	
	private boolean mHasStableIds = false;
	
	...
    }

RecyclerView.Adapter中的私有成员变量mHasStableIds需要借助setHasStableIds(boolean hasStableIds)设置(默认是false,无需设置):

/**
     * Indicates whether each item in the data set can be represented with a unique identifier
     * of type {@link java.lang.Long}.
     *
     * @param hasStableIds Whether items in data set have unique identifiers or not.
     * @see #hasStableIds()
     * @see #getItemId(int)
     */
    public void setHasStableIds(boolean hasStableIds) {
	if (hasObservers()) {
		throw new IllegalStateException("Cannot change whether this adapter has "
				+ "stable IDs while the adapter has registered observers.");
	}
	mHasStableIds = hasStableIds;
    }

通过RecyclerView.Adapter中的hasStableIds()获取该属性值:

/**
     * Returns true if this adapter publishes a unique <code>long</code> value that can
     * act as a key for the item at a given position in the data set. If that item is relocated
     * in the data set, the ID returned for that item should be the same.
     *
     * @return true if this adapter's items have stable IDs
     */
    public final boolean hasStableIds() {
    	return mHasStableIds;
    }

与之相配合使用的是RecyclerView.Adapter中的getItemId(int position)方法,默认返回NO_ID。

/**
     * Return the stable ID for the item at <code>position</code>. If {@link #hasStableIds()}
     * would return false this method should return {@link #NO_ID}. The default implementation
     * of this method returns {@link #NO_ID}.
     *
     * @param position Adapter position to query
     * @return the stable ID of the item at position
     */
    public long getItemId(int position) {
	return NO_ID;
    }

3、hasStableIds的作用

前面铺垫了这么多,可以看到ListView、RecyclerView等列表在设计的时候都会使用到hasStableIds,本节将彻底讲清楚它的含义:是否为每个列表项分配一个稳定的 ID,以确保当数据集改变时,列表可以正确地识别每个项目的ID,更有效地处理数据项的更新和回收从而提升性能。

3.1、返回false

默认情况下返回是false,表示当数据源发生了变化的时候,原有数据项的id会发生变化。且getItemId(int position)方法需要返回NO_ID,此方法的默认实现就是返回NO_ID,因此无需额外进行任何设置。

public long getItemId(int position) {
        return NO_ID;
    }

此时列表只会根据position进行复用,不会根据ID。

3.2、返回true

如果为true,当数据源发生了变化的时候,原有数据项的id不会发生变化。

//RecyclerView的Adapter则需要通过此方法设置为true
    setHasStableIds(true)
    
    //Adapter重写Adapter中的方法返回true
    public boolean hasStableIds() {
	return true;
    }

则必须重写getItemId(int position)方法,以便为每个列表项返回一个唯一的ID:

// 重写 getItemId 方法,确保每个项目的 ID 是稳定的
    @Override
    public long getItemId(int position) {
        return dataSet.get(position).getId(); // 假设 getId() 方法返回一个唯一标识符
    }

需要注意:如果将mHasStableIds设置为true,‌但实际上数据集并没有稳定的ID,‌那么可能会导致数据项显示出现问题。‌其实就算设置返回为true带来的性能提升也是微乎其微。

相关推荐

精选
ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager
Android

ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager

ViewPager1、ViewPagerandroidx.viewpager.widget.ViewPager,Android中使用非常广泛的控件,可以说是APP必备:首次打开引导页、页面Banner广告等。常用方法:setAdapter() 设置适配器setOffscreenPageLimit() 设置缓存的页面个数,默认是 1setCurrentItem() 跳转到特定的页面setOnPage

1.1k
获取Android内置WebView内核版本
Android

获取Android内置WebView内核版本

获取Android内置WebView内核版本竟然能遇到这样奇葩的事情,网页用的技术过于新颖,以至于只支持高版本Chromium内核的,低版本安卓系统中内置的内核版本较低,无法加载前端页面。1、设置查看在系统设置里 > 应用 > 应用管理 > 显示系统应用,查看WebView组件:2、页面查看通过WebView发起的网络请求,都会带上浏览器的UA,通常页面都可以通过UA判断浏览器的内核版本。这里有两

1.1w
Android 14适配总结
Android

Android 14适配总结

Android 14适配总结毕业工作至今已经适配了三个Android大版本,从Android 11到Android 12、再到Android 13。2023年,Google即将推出的Android 14,上半年已经开始第一批适配。现在,第四个Android版本已经适配完,总结记录一下。1、Android 14计划Google一般会在2月份对外发布预告,同时放出开发者预览版。“拉通”各大平台、厂商以

1.1w