Android Recyclerview简单两步实现二级列表展开,收起带动画效果

BaserecyclerViewAdapter这个框架网上关于展开收起的介绍感觉都不是很全,正好这次要复习学过的东西, 虽然不算高深,但是帮到有需要的人最好了,欢迎指教

效果展示

准备

这里需要的adapter是BaseRecyclerviewAdappter这个第三方库,这个框架大家应该已经熟悉了,封装了很多功能,使用方便,这里只所以下item的展开收拢的代码实现部分,具体配置这里不多介绍了,需要了解去作者github看:
https://github.com/CymChad/BaseRecyclerViewAdapterHelper/

以上图为例,需要一个集合,这个集合的内容是item收起来的时候的内容,这里包括更新日志,使用说明,联系技术三类。(最下面的那个检测更新不是recyclerView的 !!!!!),然后再从这三个里面在添加内容,思路就是这样

第一步,写实体类实现MultiItemEntity,继承AbstractExpandableItem
第二步,adapter convert中中getItemViewType判断,加载不同布局数据

主要代码

创建外层类
`
外层 更新日志 只有一个字符串做标题要用,继承AbstractExpandableItem<>,泛型根据自己的需要写实体类,并实现MultiItemEntity,实现getLevel,getItemType方法,修改getItemType返回值

public class AboutTitle extends AbstractExpandableItem implements MultiItemEntity {
    String aboutTitle;

    public AboutTitle(String aboutTitle) {
        this.aboutTitle = aboutTitle;
    }

    public String getAboutTitle() {
        return aboutTitle;
    }

    public void setAboutTitle(String aboutTitle) {
        this.aboutTitle = aboutTitle;
    }

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

    @Override
    public int getItemType() {
        return AboutVersionAdapter.TYPE_LEVEL_0;
    }
}

展开后的内容,没有下级,只需要实现MultiItemEntity

public class AboutDetails implements MultiItemEntity {
    private ArrayList contents;

    public ArrayList getContents() {
        return contents;
    }

    public void setContents(ArrayList contents) {
        this.contents = contents;
    }

    public AboutDetails(ArrayList contents) {
        this.contents = contents;
    }

    @Override
    public int getItemType() {
        return AboutVersionAdapter.TYPE_LEVEL_1;
    }
}

activity

     AboutVersionAdapter aboutVersionAdapter;
 //   外层标题集合
    List detailList = new ArrayList<>();
//    更新日志
    ArrayList updateLogList = new ArrayList<>();
    updateLogList.add("更新日志内容1");
    updateLogList.add("更新日志内容2");
    updateLogList.add("更新日志内容3");
    AboutTitle a = new AboutTitle("更新日志");
    a.addSubItem(new AboutDetails(updateLogList));
    detailList.add(a);
        aboutVersionAdapter = new AboutVersionAdapter(detailList);
        aboutList.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
		//解决数据加载不完的问题
        aboutList.setNestedScrollingEnabled(false);
        //解决数据加载完成后, 没有停留在顶部的问题
        aboutList.setFocusable(false);
        aboutList.setAdapter(aboutVersionAdapter);

adapter


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.fengtong.checksystem.Emity.AboutDetails;
import com.fengtong.checksystem.Emity.AboutTitle;
import com.fengtong.checksystem.R;

import java.util.ArrayList;
import java.util.List;

public class AboutVersionAdapter extends BaseMultiItemQuickAdapter {
    public static final int TYPE_LEVEL_0 = 0;
    public static 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 AboutVersionAdapter(List data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.item_about_title);
        addItemType(TYPE_LEVEL_1, R.layout.item_about_details);
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_LEVEL_0:
                final AboutTitle aboutTitle = (AboutTitle) item;
                helper.setImageResource(R.id.item_about_title_iv, aboutTitle.isExpanded() ? R.mipmap.jt : R.mipmap.jtx);
                helper.setText(R.id.item_about_title_content, ((AboutTitle) item).getAboutTitle());
                // 判断,如果是最后一个显示两个分割线
                if (!"联系技术".equals(aboutTitle.getAboutTitle())) {
                    helper.setVisible(R.id.item_about_title_v1, true);
                    helper.setVisible(R.id.item_about_title_v2, false);
                } else {
                    helper.setVisible(R.id.item_about_title_v1, true);
                    helper.setVisible(R.id.item_about_title_v2, true);

                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getLayoutPosition();
                        if (aboutTitle.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_LEVEL_1:
                AboutDetails aboutDetails = (AboutDetails) item;
                ArrayList contents = ((AboutDetails) item).getContents();
                RecyclerView recyclerView = helper.getView(R.id.item_aboutDetails_list);
                UpdateLogAdapter updateLogAdapter = new UpdateLogAdapter(R.layout.item_update_log, contents);
                recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                recyclerView.setAdapter(updateLogAdapter);
                break;
        }
    }
}

item_about_title.xml




    
        

        
    
    


item_about_details.cml




    


小白第一次写博客,所以写的比较基础。
如果有发现错误欢迎指正我及时修改,如果有好的建议欢迎留言。谢谢。

你可能感兴趣的:(android)