Skip to content

Commit

Permalink
1、setNodes不再强制刷新列表(当列表有大量node时,可能会很耗时,此时允许在其他线程进行)
Browse files Browse the repository at this point in the history
2、修改接口返回的node id方法名,避免与greendao的实体类方法名冲突
  • Loading branch information
pye52 committed Oct 23, 2017
1 parent c20dca4 commit 592a420
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ allprojects {
}
dependencies {
compile 'com.github.pye52:TreeView:0.2.2'
compile 'com.github.pye52:TreeView:0.2.3'
}
```

Expand Down Expand Up @@ -86,8 +86,10 @@ Type type = new TypeToken<List<User>>(){}.getType();
List<User> list = new Gson().fromJson(testStr, type);
// TreeAdapter<User> adapter = new TreeAdapter<>(this, list);
TreeAdapter<User> adapter = new TreeAdapter<>(this);
// 若不在初始化时指定数据,则会等到执行setNodes时才会有数据
// 若不在初始化时指定数据,则需要手动调用notifyDataSetChanged来刷新列表
adapter.setNodes(list);
adapter.notifyDataSetChanged();

recyclerview.setAdapter(adapter);

final List<User> C1Childs = new ArrayList<>();
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/kanade/treeview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,21 @@ protected void onCreate(Bundle savedInstanceState) {

Type type = new TypeToken<List<User>>(){}.getType();
list = new Gson().fromJson(testStr, type);
rv = (RecyclerView) findViewById(R.id.rv);
rv = findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
final TreeAdapter<User> adapter = new TreeAdapter<>(this);

final List<User> C1Childs = new ArrayList<>();
C1Childs.add(new User(11, 3, "C11"));
C1Childs.add(new User(12, 3, "C12"));
C1Childs.add(new User(13, 3, "C13"));
final List<User> childs = new ArrayList<>();
childs.add(new User(11, 3, "C11"));
childs.add(new User(12, 3, "C12"));
childs.add(new User(13, 3, "C13"));

adapter.setNodes(list);
adapter.setListener(new TreeItemClickListener() {
@Override
public void OnClick(Node node) {
if (node.getId() == 3) {
adapter.addChildrenById(3, C1Childs);
adapter.addChildrenById(3, childs);
}
Toast.makeText(MainActivity.this, node.getName(), Toast.LENGTH_SHORT).show();
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/kanade/treeview/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public User(long id, long pid, String title) {
}

@Override
public long getId() {
public long getNid() {
return id;
}

Expand Down
2 changes: 1 addition & 1 deletion treeadapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "0.2.2"
versionName "0.2.3"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.kanade.treeadapter;

public interface RvTree {
long getId();
long getNid();

long getPid();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public T getItemById(int id) {
public void setNodes(List<T> data) {
List<Node<T>> allNodes = TreeHelper.getSortedNode(data, 0);
mNodes = TreeHelper.<T>filterVisibleNode(allNodes);
notifyDataSetChanged();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static <T extends RvTree> List<Node<T>> convertData2Node(List<T> data) {
List<Node<T>> list = new ArrayList<>();
for (T item : data) {
Node<T> node = new Node<>();
node.setId(item.getId());
node.setId(item.getNid());
node.setpId(item.getPid());
node.setName(item.getTitle());
node.setResId(item.getImageResId());
Expand Down

0 comments on commit 592a420

Please sign in to comment.