116.Android 简单的二级或三级列表(BaseRecyclerViewAdapterHelper)

导读:本篇文章讲解 116.Android 简单的二级或三级列表(BaseRecyclerViewAdapterHelper),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

116.Android 简单的二级或三级列表(BaseRecyclerViewAdapterHelper)

//1.第一步 导入依赖库:

//RecyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'

//RecyclerAdapter
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'

//刷新控件
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-28'

//2.第二步 新建ExpandableListActivity页面,添加数据时,自行控制列表级别,可以自定义二级或者三级或者更多:

//manifest注册:

<activity
    android:name=".phone.activity.ExpandableListActivity"
    android:launchMode="singleTop"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden"
    tools:ignore="LockedOrientationActivity" />

//activity代码:

public class ExpandableListActivity extends AppCompatActivity {
    private final LinearLayoutManager manager = new LinearLayoutManager(this);
    private final ExpandableListAdapter adapter = new ExpandableListAdapter(new ArrayList<>());

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_list);

        RecyclerView mTreeListRecy = findViewById(R.id.mTreeListRecy);

        //禁止滑动  布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(this) {
            //禁止竖向滑动 RecyclerView 为垂直状态(VERTICAL)
            @Override
            public boolean canScrollVertically() {
                return false;
            }
            //禁止横向滑动 RecyclerView 为水平状态(HORIZONTAL)
            /*@Override
            public boolean canScrollHorizontally() {
                return false;
            }*/
        };
        mTreeListRecy.setLayoutManager(manager);

        //设置列表默认动画效果
        mTreeListRecy.setItemAnimator(new DefaultItemAnimator());

        //解决数据加载不完的问题
        mTreeListRecy.setNestedScrollingEnabled(false);
        //解决数据加载完成后, 没有停留在顶部的问题
        mTreeListRecy.setFocusable(false);
        mTreeListRecy.setAdapter(adapter);

        //        默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左)
//        adapter.openLoadAnimation(BaseQuickAdapter.FOOTER_VIEW);

//        //一行代码开启动画
//        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//        //更换动画
//        //ALPHAIN, SCALEIN, SLIDEIN_BOTTOM, SLIDEIN_LEFT, SLIDEIN_RIGHT, //SLIDEIN_LEFT_RIGHT, SLIDEIN_BOTTOM_TOP, CUSTOMIN
//        adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
//        //自定义
//        adapter.openLoadAnimation(new BaseAnimation() {
//            @Override
//            public Animator[] getAnimators(View view) {
//                return new Animator[]{ObjectAnimator.ofFloat(view, "alpha", 0.5f, 1.0f),
//                        ObjectAnimator.ofFloat(view, "scaleX", 0.5f, 1.0f)};
//            }
//        });


        List<MultiItemEntity> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ExpandableListBean bean = new ExpandableListBean("一级name" + i, 0);
            ExpandableListBean bean1 = new ExpandableListBean(bean.getName() + "的下属 二级name", 0);
            ExpandableListBean bean2 = new ExpandableListBean(bean1.getName() + "的下属 三级name,我是三级三级三级", 1);
            bean.addSubItem(bean1);
            bean1.addSubItem(bean2);
            list.add(bean);
        }

        adapter.addData(list);
        
    }

}

//3.第三步 新建ExpandableListAdapter适配器:

/**
 * @author CJF
 */
public class ExpandableListAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    /**
     * 多布局的类型
     */
    private final int TYPE_LEVEL_0 = 0;
    private final int TYPE_LEVEL_1 = 1;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ExpandableListAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.expandable_list_item1);
        addItemType(TYPE_LEVEL_1, R.layout.expandable_list_item2);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        ExpandableListBean bean = (ExpandableListBean) item;
        String name = bean.getName();
        //打开所有列表
//        expandAll();

        switch (helper.getItemViewType()) {
            case TYPE_LEVEL_0:
                helper.setText(R.id.mExpandableList1Name, name)
                        .setImageResource(R.id.mExpandableList1Expand, bean.isExpanded() ? R.drawable.svg_expand_more : R.drawable.svg_navigate_next);
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getLayoutPosition();
                        if (bean.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_LEVEL_1:
                helper.setText(R.id.mExpandableList2Name, name);
                break;
            default:
                break;
        }
    }

}

//4.第四步 新建ExpandableListBean类:

/**
 * @author CJF
 */
public class ExpandableListBean extends AbstractExpandableItem implements MultiItemEntity {
    private String name;
    private int itemType;

    public ExpandableListBean() {
    }

    public ExpandableListBean(String name, int itemType) {
        this.name = name;
        this.itemType = itemType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setItemType(int itemType) {
        this.itemType = itemType;
    }

    @Override
    public int getLevel() {
        return 0;
    }

    @Override
    public int getItemType() {
        return itemType;
    }

}

//第五步 各个xml布局文件:

//activity_tree_list:

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/color_white"
    android:orientation="vertical">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srlEnableLoadMore="true"
        app:srlEnableRefresh="true">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/mTreeListRecy"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <com.scwang.smartrefresh.layout.footer.FalsifyFooter
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50" />

        <com.scwang.smartrefresh.layout.header.FalsifyHeader
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50" />

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</LinearLayout>

//expandable_list_item1:

<?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="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@drawable/selector_common_item">

    <ImageView
        android:id="@+id/mExpandableList1Expand"
        android:layout_width="@dimen/dp_50"
        android:layout_height="@dimen/dp_50"
        android:background="@drawable/selector_common_item"
        android:gravity="center"
        android:padding="@dimen/dp_10"
        android:src="@drawable/svg_navigate_next" />

    <TextView
        android:id="@+id/mExpandableList1Name"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/selector_common_item"
        android:gravity="left|center_vertical"
        android:maxLines="1"
        android:minHeight="@dimen/dp_50"
        android:padding="@dimen/dp_10"
        android:text="text"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_15" />

</LinearLayout>

//expandable_list_item2:

<?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="wrap_content"
    android:layout_marginBottom="@dimen/dp_5"
    android:layout_marginLeft="@dimen/dp_20"
    android:layout_marginRight="@dimen/dp_20"
    android:layout_marginTop="@dimen/dp_5"
    android:background="@drawable/selector_common_item"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/mExpandableList2Name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:background="@drawable/selector_common_item"
        android:gravity="left|center_vertical"
        android:minHeight="@dimen/dp_50"
        android:padding="@dimen/dp_10"
        android:text="text"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_15" />

</LinearLayout>

//第六步 两个svg图片文件:

//svg_expand_more:

<vector android:alpha="0.85" android:autoMirrored="true"
    android:height="24dp" android:tint="#3B76EC"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#ffffffff" android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z"/>
</vector>

//svg_navigate_next:

<vector android:alpha="0.85" android:autoMirrored="true"
    android:height="24dp" android:tint="#3B76EC"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#ffffffff" android:pathData="M10,6L8.59,7.41 13.17,12l-4.58,4.59L10,18l6,-6z"/>
</vector>

//——————————————————–END———————————————————-

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/118278.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!