Skip to content

Commit 661da8d

Browse files
author
liyu20
committed
1、修复IndexOutOfBoundsException异常
1 parent b2120ff commit 661da8d

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

demo/src/main/java/com/yu/extended/recyclerview/demo/MainActivity.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import android.support.v7.app.AppCompatActivity;
66
import android.support.v7.widget.RecyclerView;
77
import android.view.LayoutInflater;
8+
import android.view.View;
89
import android.view.ViewGroup;
910

10-
import com.yu.extended.recyclerview.demo.R;
1111
import com.yu.bundles.extended.recyclerview.ExtendedHolder;
1212
import com.yu.bundles.extended.recyclerview.ExtendedHolderFactory;
1313
import com.yu.bundles.extended.recyclerview.ExtendedNode;
@@ -57,15 +57,15 @@ public ExtendedHolder getHolder(ExtendedRecyclerViewHelper helper, ViewGroup par
5757
.setEnableExtended(true)
5858
.complete();
5959

60-
new Handler().postDelayed(new Runnable() {
60+
findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
6161
@Override
62-
public void run() {
62+
public void onClick(View v) {
6363
ArrayList<ExtendedNode> list = new ArrayList<>();
6464
list.add(new ExtendedNode(new Type2Bean(111), false, new ExtendedNode(new Type3Bean(1111))));
6565
list.add(new ExtendedNode(new Type2Bean(111)));
6666
extendedRecyclerViewHelper.insertItems(initList.get(0), 0, list);
6767
}
68-
}, 3000);
68+
});
6969
}
7070

7171
private ArrayList<ExtendedNode> getInitData(){
+14-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
4-
android:layout_height="match_parent">
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<Button
8+
android:id="@+id/add"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:paddingBottom="10dp"
12+
android:paddingTop="10dp"
13+
android:paddingLeft="15dp"
14+
android:paddingRight="15dp"
15+
android:text="在第一个节点添加两个子节点"/>
516

617
<android.support.v7.widget.RecyclerView
718
android:id="@+id/recyclerView"
819
android:layout_width="match_parent"
920
android:layout_height="match_parent"/>
1021

11-
</FrameLayout>
22+
</LinearLayout>

extendedRecyclerView/src/main/java/com/yu/bundles/extended/recyclerview/ExtendedRecyclerViewBuilder.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.yu.bundles.extended.recyclerview;
22

3+
import android.content.Context;
34
import android.support.v7.widget.LinearLayoutManager;
45
import android.support.v7.widget.RecyclerView;
56

@@ -19,7 +20,7 @@ public class ExtendedRecyclerViewBuilder {
1920

2021
private ExtendedRecyclerViewBuilder(RecyclerView recyclerView) {
2122
this.recyclerView = recyclerView;
22-
this.recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
23+
this.recyclerView.setLayoutManager(new ExtendLayoutManager(recyclerView.getContext()));
2324
isInit = false;
2425
}
2526

@@ -49,4 +50,18 @@ public ExtendedRecyclerViewHelper complete(){
4950
return adapter;
5051
}
5152

53+
private class ExtendLayoutManager extends LinearLayoutManager {
54+
ExtendLayoutManager(Context context) {
55+
super(context);
56+
}
57+
58+
@Override
59+
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
60+
try {
61+
super.onLayoutChildren(recycler, state);
62+
} catch (IndexOutOfBoundsException e){
63+
//兼容异常
64+
}
65+
}
66+
}
5267
}

me_config.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ ext {
1212
design : "com.android.support:design:${SUPPORT_VERSION}",
1313
annotations : "com.android.support:support-annotations:${SUPPORT_VERSION}",
1414
appcompat : "com.android.support:appcompat-v7:${SUPPORT_VERSION}",
15-
recyclerview: "com.android.support:recyclerview-v7:${SUPPORT_VERSION}",
15+
recyclerview: "com.android.support:recyclerview-v7:${SUPPORT_VERSION}"
1616
]
1717
}

0 commit comments

Comments
 (0)