发布于 2017-09-14 22:27:57 | 189 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了Android ExpandableRecyclerView的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

 本文为大家分享了Android ExpandableRecyclerView的使用,供大家参考,具体内容如下

1.目前只支持两级结构。
2.支持所有组同时全部展开,支持同一时间只能展开一组。
3.GroupView,ChildView高度自定义。
4.支持初始化数据时,指定展开某组数据。
5.支持GroupItem,ChildItem的Onlick,OnLongClick事件。
6.展开收起带动画。

效果图:


使用步骤:加入依赖


compile 'com.drawthink:expandable-recyclerview:0.0.3'

1.继承BaseViewHolder,实现自己的ViewHolder

1.1 在构造函数中初始化你的View(包括GroupView,和childView).

1.2 分别实现以下两个方法,并在对应方法中返回对应Layout布局文件中根节点的ID。


public int getGroupViewResId()

public int getChildViewResId()

示例代码:


public class ImageViewHolder extends BaseViewHolder {

  public ImageView image;
  public TextView tvTitle;

  /**
   * 初始化你的View(这里包括GroupView,和childView)
   */
  public ImageViewHolder(Context ctx, View itemView, int viewType) {
    super(ctx,itemView, viewType);
    image = (ImageView) itemView.findViewById(R.id.iv_image);
    tvTitle = (TextView)itemView.findViewById(R.id.tv_title);
  }

  /**
   * @return 返回你的GroupView 布局文件中根节点的ID
   */
  @Override
  public int getGroupViewResId() {
    return R.id.group;
  }

  /**
   * @return 返回你的ChildView 布局文件中根节点的ID
   */
  @Override
  public int getChildViewResId() {
    return R.id.child;
  }

}

2.继承BaseRecyclerViewAdapter

/**
 * author:Drawthink
 * describe:
 * date: 2017/5/22
 * T :group data
 * S :child data
 * VH :ViewHolder
 */

public abstract class BaseRecyclerViewAdapter<T,S,VH extends BaseViewHolder> extends RecyclerView.Adapter<VH>

示例Adapter代码:


public class ImageAdapter extends BaseRecyclerViewAdapter<String,ImageBean,ImageViewHolder> {

  private Context ctx;
  private List datas;
  private LayoutInflater mInflater;

  public ImageAdapter(Context ctx, List<RecyclerViewData> datas) {
    super(ctx, datas);
    mInflater = LayoutInflater.from(ctx);
    this.ctx = ctx;
    this.datas = datas;
  }

  @Override
  public void onBindGroupHolder(ImageViewHolder holder, int groupPos,int position, String groupData) {
    holder.tvTitle.setText(groupData);
  }

  @Override
  public void onBindChildpHolder(ImageViewHolder holder, int groupPos,int childPos,int position, ImageBean childData) {
    holder.image.setBackgroundResource(childData.getResId());
  }

  @Override
  public View getGroupView(ViewGroup parent) {
    return mInflater.inflate(R.layout.title_item_layout,parent,false);
  }

  @Override
  public View getChildView(ViewGroup parent) {
    return mInflater.inflate(R.layout.item_image_layout,parent,false);
  }

  @Override
  public ImageViewHolder createRealViewHolder(Context ctx, View view, int viewType) {
    return new ImageViewHolder(ctx,view,viewType);
  }
}

完成以上两步之后,基本大工告成,由于ExpandableRecyclerView的数据是要分组的,所以提供了RecyclerViewData来封装


/**
   * @param groupData
   * @param childDatas
   * @param isExpand  初始化展示数据时,该组数据是否展开
   */
public RecyclerViewData(T groupData, List<S> childDatas,boolean isExpand)

那接下来看下数据具体是怎样封装的。


    mDatas = new ArrayList<>();
    List<ImageBean> bean1 = new ArrayList<>();
    List<ImageBean> bean2 = new ArrayList<>();
    List<ImageBean> bean3 = new ArrayList<>();
    // 每个子列表长度可以不相同
    bean1.add(new ImageBean("Dog", R.mipmap.dog));
    bean1.add(new ImageBean("Dog", R.mipmap.dog));
    bean2.add(new ImageBean("Cat", R.mipmap.cat));
    bean3.add(new ImageBean("Bird", R.mipmap.bird));

    mDatas.add(new RecyclerViewData("Dog", bean1, true));
    mDatas.add(new RecyclerViewData("Cat", bean2, true));
    mDatas.add(new RecyclerViewData("Bird", bean3, true));

所有工作以完成,现在你可以象平常使用Adapter,RecyclerView一样,来愉快的写代码了。

注意:在对元数据mDatas进行增删操作时,要调用adapter.notifyRecyclerViewData();否则会造成数据索引错乱的问题。

代码github地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务