前言
大家经常会用到RecycleView,RecycleView虽然很好用,但是没有了ExpandListView的功能,这时候,我们就需要自己去写了。今天给大家介绍一个ExpandableRecyclerview,个人感觉封装挺好的,分享给大家。
先看效果图:
效果是模拟机录的,看起来会闪烁的感觉,真机没有的!!!
引入依赖库
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("主机游戏"));
windowPhones.add(new ChildText("FPS游戏"));
windowPhones.add(new ChildText("挂机游戏"));
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地址,喜欢的可以去自己去研究研究。