android BaseRecyclerViewAdapterHelper 二级item

     官网:  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;
    }
}

简单记录一下,当然你要有实现点击事件,可以直接看他们官网的,更清楚.

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Android中,城市二级列表联动是指根据用户在一级列表中所选中的省份,动态显示对应的二级子列表中的城市。这样的功能在很多应用中都会用到,比如选择所在城市、选择配送地址等。 要实现城市二级列表联动,首先我们需要准备城市数据。可以通过使用城市数据库或者本地存储文件的方式来获取城市数据,包括省和市的名称。根据这些数据,我们可以构建一个二维的数据结构,使得每一个省份对应一个城市列表。 接下来,在Android中可以将一级列表使用RecyclerView或者ListView来展示,通过适配器将省份数据绑定到列表上。当用户点击某个省份时,可以通过监听点击事件获取到省份的位置信息,根据位置信息获取对应的城市列表数据。 然后,我们可以将二级城市列表数据展示在另一个RecyclerView或者ListView上。同样,需要使用适配器将城市数据绑定到二级列表上。通过刷新适配器的方式实现二级列表的动态更新。 最后,我们还需要处理联动的逻辑。当用户点击一级列表的省份时,我们需要根据省份的位置信息获取对应的城市列表数据,并将城市列表数据绑定到二级列表的适配器上。这样,在界面上就会实现二级列表的联动效果。 在实现过程中需要考虑的一些问题有:如何获取并加载城市数据、如何处理列表的点击事件、如何实现二级列表的动态更新等。总之,通过使用RecyclerView或者ListView以及相应的适配器,我们可以比较容易地实现Android城市二级列表联动的功能。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值