官网: https://github.com/CymChad/BaseRecyclerViewAdapterHelper 在这里简单说明记录一下,它有两个版本2.x和3.x版
我这里用的是最新的3.x,环境也是在,用的是androidx:
compileSdkVersion 29
buildToolsVersion "29.0.2"
上图:
1.添加依赖
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'
2.新建activity布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".recy.BashRecyActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
3.新建适配器类 (两个类)
public class NodeTreeAdapter extends BaseNodeAdapter {
public NodeTreeAdapter() {
super();
addNodeProvider(new FirstProvider());
addNodeProvider(new SecondProvider());
}
@Override
protected int getItemType(@NotNull List<? extends BaseNode> list, int i) {
BaseNode node = list.get(i);
if (node instanceof FirstNode) {
return 1;
} else if (node instanceof SecondNode) {
return 2;
}
return -1;
}
public static final int EXPAND_COLLAPSE_PAYLOAD = 110;
}
1.FirstProvider
public class FirstProvider extends BaseNodeProvider {
@Override
public int getItemViewType() {
return 1;
}
@Override
public int getLayoutId() {
return R.layout.item_node_first;
}
@Override
public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode) {
FirstNode entity = (FirstNode) baseNode;
baseViewHolder.setText(R.id.title, entity.getTitle());
baseViewHolder.setImageResource(R.id.iv, R.mipmap.ic_launcher);
setArrowSpin(baseViewHolder, baseNode, false);
}
@Override
public void convert(@NotNull BaseViewHolder helper, @NotNull BaseNode data, @NotNull List<?> payloads) {
for (Object payload : payloads) {
if (payload instanceof Integer && (int) payload == NodeTreeAdapter.EXPAND_COLLAPSE_PAYLOAD) {
// 增量刷新,使用动画变化箭头
setArrowSpin(helper, data, true);
}
}
}
private void setArrowSpin(BaseViewHolder helper, BaseNode data, boolean isAnimate) {
FirstNode entity = (FirstNode) data;
ImageView imageView = helper.getView(R.id.iv);
if (entity.isExpanded()) {
if (isAnimate) {
ViewCompat.animate(imageView).setDuration(200)
.setInterpolator(new DecelerateInterpolator())
.rotation(0f)
.start();
} else {
imageView.setRotation(0f);
}
} else {
if (isAnimate) {
ViewCompat.animate(imageView).setDuration(200)
.setInterpolator(new DecelerateInterpolator())
.rotation(90f)
.start();
} else {
imageView.setRotation(90f);
}
}
}
@Override
public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
// 这里使用payload进行增量刷新(避免整个item刷新导致的闪烁,不自然)
getAdapter().expandOrCollapse(position, true, true, NodeTreeAdapter.EXPAND_COLLAPSE_PAYLOAD);
}
}
FirstNode
public class FirstNode extends BaseExpandNode {
private List<BaseNode> childNode;
private String title;
public FirstNode(List<BaseNode> childNode, String title) {
this.childNode = childNode;
this.title = title;
setExpanded(false);
}
public String getTitle() {
return title;
}
@Nullable
@Override
public List<BaseNode> getChildNode() {
return childNode;
}
}
2.SecondProvider
public class SecondProvider extends BaseNodeProvider {
@Override
public int getItemViewType() {
return 2;
}
@Override
public int getLayoutId() {
return R.layout.item_node_second;
}
@Override
public void convert(@NotNull BaseViewHolder helper, @NotNull BaseNode data) {
SecondNode entity = (SecondNode) data;
helper.setText(R.id.title, entity.getTitle());
if (entity.isExpanded()) {
helper.setImageResource(R.id.iv, R.mipmap.ic_launcher);
} else {
helper.setImageResource(R.id.iv, R.mipmap.ic_launcher);
}
}
@Override
public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
SecondNode entity = (SecondNode) data;
if (entity.isExpanded()) {
getAdapter().collapse(position);
} else {
getAdapter().expandAndCollapseOther(position);
}
}
SecondNode
public class SecondNode extends BaseExpandNode {
private List<BaseNode> childNode;
private String title;
public SecondNode(List<BaseNode> childNode, String title) {
this.childNode = childNode;
this.title = title;
setExpanded(false);
}
public String getTitle() {
return title;
}
@Nullable
@Override
public List<BaseNode> getChildNode() {
return childNode;
}
}
4.主activity
public class BashRecyActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private NodeTreeAdapter adapter = new NodeTreeAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bash_recy);
setTitle("Node Use (Tree)");
mRecyclerView = findViewById(R.id.rv_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(adapter);
adapter.setList(getEntity());
Log.i("显示", "onCreate: "+getEntity());
// 模拟新增node
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
SecondNode seNode = new SecondNode(new ArrayList<BaseNode>(), "Second Node(This is added)");
SecondNode seNode2 = new SecondNode(new ArrayList<BaseNode>(), "Second Node(This is added)");
List<SecondNode> nodes = new ArrayList<>();
nodes.add(seNode);
nodes.add(seNode2);
//第一个夫node,位置为子node的3号位置
adapter.nodeAddData(adapter.getData().get(0), 2, nodes);
// adapter.nodeSetData(adapter.getData().get(0), 2, seNode2);
// adapter.nodeReplaceChildData(adapter.getData().get(0), nodes);
Toast.makeText(BashRecyActivity.this,"新插入了两个node",Toast.LENGTH_LONG).show();
}
}, 2000);
}
private List<BaseNode> getEntity() {
List<BaseNode> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
List<BaseNode> secondNodeList = new ArrayList<>();
for (int n = 0; n <= 3; n++) {
SecondNode seNode = new SecondNode(secondNodeList, "Second Node " + n);
secondNodeList.add(seNode);
}
FirstNode entity = new FirstNode(secondNodeList, "First Node " + i);
// 模拟 默认第0个是展开的
entity.setExpanded(i == 0);
list.add(entity);
}
return list;
}
}
简单记录一下,当然你要有实现点击事件,可以直接看他们官网的,更清楚.