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<AboutDetails> 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<String> contents;
public ArrayList<String> getContents() {
return contents;
}
public void setContents(ArrayList<String> contents) {
this.contents = contents;
}
public AboutDetails(ArrayList<String> contents) {
this.contents = contents;
}
@Override
public int getItemType() {
return AboutVersionAdapter.TYPE_LEVEL_1;
}
}
activity
AboutVersionAdapter aboutVersionAdapter;
// 外层标题集合
List<MultiItemEntity> detailList = new ArrayList<>();
// 更新日志
ArrayList<String> 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<MultiItemEntity, BaseViewHolder> {
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<MultiItemEntity> 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<String> 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
<?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="vertical">
<View
android:id="@+id/item_about_title_v1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d4d4d4"/>
<FrameLayout
android:layout_marginTop="13dp"
android:layout_marginBottom="13dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_about_title_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10dp"
android:text="功能介绍"
android:textColor="@color/wordcolor"
android:textSize="20sp" />
<ImageView
android:id="@+id/item_about_title_iv"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:src="@mipmap/jtx" />
</FrameLayout>
<View
android:id="@+id/item_about_title_v2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d4d4d4"/>
</LinearLayout>
item_about_details.cml
<?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.support.v7.widget.RecyclerView
android:id="@+id/item_aboutDetails_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
小白第一次写博客,所以写的比较基础。
如果有发现错误欢迎指正我及时修改,如果有好的建议欢迎留言。谢谢。