在使用ListView的时候,有一些场景,可能需要在子View中判断是否已经从ListView移出,然后做一些列操作。一般我们在普通的Layout,例如LinearLayou中,会把这些操作写在了子View的onDetachedFromWindow里。但是在使用ListView的是,发现子View这个方法不会调用。接下来一起探究下如何解决这个问题。
1.只有Android N上子View移出,会调用View的onDetachedFromWindow方法
经过测试发现这个方法只有在N上调用。设置断点查看
然后看下上图中对应蓝色的AbsListView中的这段代码
/**
* At the end of a layout pass, all temp detached views should either be re-attached or
* completely detached. This method ensures that any remaining view in the scrap list is
* fully detached.
*/
void fullyDetachScrapViews() {
final int viewTypeCount = mViewTypeCount;
final ArrayList<View>[] scrapViews = mScrapViews;
for (int i = 0; i < viewTypeCount; ++i) {
final ArrayList<View> scrapPile = scrapViews[i];
for (int j = scrapPile.size() - 1; j >= 0; j--) {
final View view = scrapPile.get(j);
if (view.isTemporarilyDetached()) {
removeDetachedView(view, false);
}
}
}
}
mScrapViews这个ArrayList[]保存的就是移出屏幕的View,以便ListView复用。这个方法主要就是遍历了数组中的每个List,然后调用他们的onDetachedFromWindow方法。
最后对比下android-25的ListView源码与android-22的这一块的区别。
左边为android-25,右侧为android-22,显然22中没有这些逻辑,子View的onDetachFromWindow也不会调用。
2.如何在Android低版本判断子View被移出
2.1相关源码分析
在查看AbsListView(ListView的父类)源码时候,查看他的内部类RecycleBin的addScrapView方法。这个方法主要是把需要缓存的View添加到一个ArrayList数组中,以便下次复用。ListView中的View移出之后会调用这个方法,查看其中有一句
scrap.dispatchStartTemporaryDetach();
如图
由于我的item外层是个LinearLayout,LinearLayout中没有这个方法,查看他的父类ViewGroup中有这个方法。如下
这个方法主要就是遍历了下自己的子View,调用他们的dispatchStartTemporaryDetach方法,子View是个TextView,其中没有这个方法,查看父类View中的dispatchStartTemporaryDetach方法。
这个方法调用了View的onStartTemporaryDetach方法。
从上边代码可以得出,当View被缓存的时候会递归调用View树上的onStartTemporaryDetach方法。
2.2测试是否可以实现
所以我们写个Demo测试下,自定义一个View继承自TextView,重写他的onStartTemporaryDetach方法,在方法中打印日志
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void onStartTemporaryDetach() {
super.onStartTemporaryDetach();
//移出屏幕调用
Log.i("pyt", "onStartTemporaryDetach=" + getText().toString());
}
@Override
public void onFinishTemporaryDetach() {
super.onFinishTemporaryDetach();
//移入屏幕调用
Log.i("pyt", "onFinishTemporaryDetach" + getText().toString());
}
}
创建一个ListView的item布局item1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.yuntao.testlistview.CusLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"/>
</com.yuntao.testlistview.CusLayout>
</LinearLayout>
在MainActivity中写入ListView相关代码,MainActivity的布局里就是写了一个普通的ListView,这里就不给出了。
public class MainActivity extends AppCompatActivity {
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.list_view);
String[] a2 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"};
mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.item1, R.id.text, a2));
}
}
要达到的效果就是编号是几的View移出屏幕,然后打印这个View的编号。查看运行效果如下图:
在View滑出的时候确实会调用该方法,但是有个坑爹的问题,就是ListView初始化的时候会多次执行onStartTemporaryDetach。
我猜测是ListView开始多次Layout导致的(这里没有源码验证不够严谨,希望有心人验证下)。我的解决办法是在onStartTemporaryDetach方法中做一个是否attachToWindow的判断,View在api19以上提供了一个isAttachedToWindow()方法,测试了下,果然可以解决。由于要支持低版本,所以使用了个比较挫的方法。当View附加到ListView,它的rootView应该是DecorView(DecorView是View树的根),所以获取他的rootView判断是否是DecorView来判断是否attach。此段逻辑只在Android4.4,6.0,7.1上验证,如果要使用请大家做严格的机型测试。最终代码如下
@Override
public void onStartTemporaryDetach() {
super.onStartTemporaryDetach();
if (!"DecorView".equals(getRootView().getClass().getSimpleName())) {
return;
}
Log.i("pyt", "onStartTemporaryDetach=" + getText().toString());
}
最终效果,每个View移出ListView,都会打印log。
最后与onStartTemporaryDetach回调对应,还有一个onFinishTemporaryDetach,同理这个方法可以判断View添加到ListView。