ExpandRecyclerView

前言

大家經(jīng)常會(huì)用到RecycleView致份,RecycleView雖然很好用献烦,但是沒(méi)有了ExpandListView的功能曙求,這時(shí)候,我們就需要自己去寫了度气。今天給大家介紹一個(gè)ExpandableRecyclerview割按,個(gè)人感覺(jué)封裝挺好的,分享給大家磷籍。

先看效果圖:

效果是模擬機(jī)錄的哲虾,看起來(lái)會(huì)閃爍的感覺(jué),真機(jī)沒(méi)有的T袷尽J铡!

引入依賴庫(kù)

compile 'com.thoughtbot:expandablerecyclerview:1.0'

定制POJO類

//Group
@SuppressLint("ParcelCreator")
public class GroupText extends ExpandableGroup<ChildText> {
    public GroupText(String title, List<ChildText> items) {
        super(title, items);
    }
}
//Child
public class ChildText implements Parcelable {
    private String name;

    public ChildText(Parcel in) {
        name = in.readString();
    }

    public String getName() {
        return name;
    }

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

    public ChildText(String name) {
        this.name = name;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
    }

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

    public static final Creator<ChildText> CREATOR = new Creator<ChildText>() {
        @Override
        public ChildText createFromParcel(Parcel in) {
            return new ChildText(in);
        }

        @Override
        public ChildText[] newArray(int size) {
            return new ChildText[size];
        }
    };
}

GroupViewHolder和ChildViewHolder

//Group
public class GroupContentViewHolder extends cn.expandrecyclerview.demo.viewholders.GroupViewHolder {

    private TextView name;

    public GroupContentViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.mobile_os);
    }

    @Override
    public void expand() {
        name.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.below_arrow_icon, 0);
    }

    @Override
    public void collapse() {
        name.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.up_arrow, 0);
    }

    public void setGroupName(ExpandableGroup group) {
        osName.setText(group.getTitle());
    }
}

GroupViewHolder 布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/black">

    <TextView
        android:id="@+id/mobile_os"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:drawablePadding="5dp"
        android:drawableRight="@mipmap/down_arrow"
        android:gravity="left|center"
        android:padding="8dp"
        android:textColor="#e6e600" />

</RelativeLayout>
//ChildViewHolder
public class ChildContentViewHolder extends cn.expandrecyclerview.demo.viewholders.ChildViewHolder {
    private TextView name;
    public ChildContentViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.phone_name);
    }

    public void onBind(ChildText child, ExpandableGroup group) {
        name.setText(child.getName());
        if (group.getTitle().equals("水果")) {
            name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
        } else if (group.getTitle().equals("球類")) {
            name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
        } else {
            name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
        }
    }
}

ChildViewHolder布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

    <TextView
        android:id="@+id/phone_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="10dp"/>
</RelativeLayout>

RecyclerAdapter

public class RecyclerAdapter extends ExpandableRecyclerViewAdapter<OSViewHolder, PhoneViewHolder> {

    private Activity activity;

    public RecyclerAdapter(Activity activity, List<? extends ExpandableGroup> groups) {
        super(groups);
        this.activity = activity;
    }

    @Override
    public OSViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.group_view_holder, parent, false);
        return new GroupContentViewHolder(view);
    }

    @Override
    public PhoneViewHolder onCreateChildViewHolder(ViewGroup parent, final int viewType) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.child_view_holder, parent, false);
        return new ChildContentViewHolder(view);
    }

    @Override
    public void onBindChildViewHolder(ChildContentViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
        final ChildText child = ((GroupText) group).getItems().get(childIndex);
        holder.onBind(child,group);
    }

    @Override
    public void onBindGroupViewHolder(GroupContentViewHolder holder, int flatPosition, ExpandableGroup group) {
        holder.setGroupName(group);
    }
}

使用方法

public class ExpandRecyclerViewActivity extends AppCompatActivity{


    private RecyclerView recyclerView;
    private ArrayList<GroupText> mobileOSes;
    private RecyclerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expand_recyclerview_layout);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mobileOSes = new ArrayList<>();

        setData();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new RecyclerAdapter(this, mobileOSes);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        adapter.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        adapter.onRestoreInstanceState(savedInstanceState);
    }

    private void setData() {
        ArrayList<ChildText> iphones = new ArrayList<>();
        iphones.add(new ChildText("蘋果"));
        iphones.add(new ChildText("橘子"));
        iphones.add(new ChildText("芒果"));
        iphones.add(new ChildText("香蕉"));
        iphones.add(new ChildText("火龍果"));
        iphones.add(new ChildText("草莓"));
        iphones.add(new ChildText("柚子"));
        iphones.add(new ChildText("哈密瓜"));

        ArrayList<ChildText> nexus = new ArrayList<>();
        nexus.add(new ChildText("足球"));
        nexus.add(new ChildText("籃球"));
        nexus.add(new ChildText("乒乓球"));
        nexus.add(new ChildText("棒球"));
        nexus.add(new ChildText("保齡球"));
        nexus.add(new ChildText("溜溜球"));
        nexus.add(new ChildText("橄欖球"));

        ArrayList<ChildText> windowPhones = new ArrayList<>();
        windowPhones.add(new ChildText("單擊游戲"));
        windowPhones.add(new ChildText("主機(jī)游戲"));
        windowPhones.add(new ChildText("FPS游戲"));
        windowPhones.add(new ChildText("掛機(jī)游戲"));
        windowPhones.add(new ChildText("小游戲"));
        windowPhones.add(new ChildText("手游"));

        mobileOSes.add(new GroupText("水果", iphones));
        mobileOSes.add(new GroupText("球類", nexus));
        mobileOSes.add(new GroupText("游戲", windowPhones));
    }
}

activity 布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
    >

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

最后附上library地址,喜歡的可以去自己去研究研究栅盲。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末汪诉,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌扒寄,老刑警劉巖鱼鼓,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件域庇,死亡現(xiàn)場(chǎng)離奇詭異援制,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)胚委,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門课竣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)嘉赎,“玉大人,你說(shuō)我怎么就攤上這事于樟」酰” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵迂曲,是天一觀的道長(zhǎng)靶橱。 經(jīng)常有香客問(wèn)我,道長(zhǎng)路捧,這世上最難降的妖魔是什么关霸? 我笑而不...
    開封第一講書人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮杰扫,結(jié)果婚禮上队寇,老公的妹妹穿的比我還像新娘。我一直安慰自己涉波,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開白布炭序。 她就那樣靜靜地躺著啤覆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪惭聂。 梳的紋絲不亂的頭發(fā)上窗声,一...
    開封第一講書人閱讀 49,792評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音辜纲,去河邊找鬼笨觅。 笑死,一個(gè)胖子當(dāng)著我的面吹牛耕腾,可吹牛的內(nèi)容都是我干的见剩。 我是一名探鬼主播,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼扫俺,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼苍苞!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤羹呵,失蹤者是張志新(化名)和其女友劉穎骂际,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體冈欢,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡歉铝,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了凑耻。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片太示。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖拳话,靈堂內(nèi)的尸體忽然破棺而出先匪,到底是詐尸還是另有隱情,我是刑警寧澤弃衍,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布呀非,位于F島的核電站,受9級(jí)特大地震影響镜盯,放射性物質(zhì)發(fā)生泄漏岸裙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一速缆、第九天 我趴在偏房一處隱蔽的房頂上張望降允。 院中可真熱鬧,春花似錦艺糜、人聲如沸剧董。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)翅楼。三九已至,卻和暖如春真慢,著一層夾襖步出監(jiān)牢的瞬間毅臊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工黑界, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留管嬉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓朗鸠,卻偏偏與公主長(zhǎng)得像蚯撩,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子烛占,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,761評(píng)論 25 707
  • afinalAfinal是一個(gè)android的ioc求厕,orm框架 https://github.com/yangf...
    passiontim閱讀 15,410評(píng)論 2 45
  • 很久沒(méi)有認(rèn)認(rèn)真真、仔仔細(xì)細(xì)的看看QQ上的那些好友了,就在昨天呀癣,我重新審視那一個(gè)個(gè)的好友美浦,那些逐漸模糊的頭像,那些不...
    顛覆閱讀 529評(píng)論 0 11
  • BANGBANGNT閱讀 247評(píng)論 0 0
  • #世界欠你的一聲晚安我來(lái)還#“清酒和冷粥是你项栏,涼風(fēng)和路燈是你浦辨,冷若冰霜是你,不諳世事是你沼沈,凜冬中流酬,我站在午夜的街口...
    清伊Yui閱讀 260評(píng)論 0 0