GeorgeYang'Blog

my technology blog

android中RecyclerView横向居中显示和Expandable的实现

阅读:1923 创建时间:16-01-06 00:12:45 tags:android,RecyclerView

RecyclerView横向居中显示

横向的RecyclerView默认从最左边开始,如果你的项目要求item数量不足以铺满最大宽度时需要居中显示,此时需要自定义RecyclerView达到这种效果,代码如下:

 /**
  * 水平居中的RecyclerView
  * Created by george.yang on 2015/12/30.
  */
 public class HorCenterRecyclerView extends RecyclerView {
     public HorCenterRecyclerView(Context context) {
         super(context);
     }

     public HorCenterRecyclerView(Context context, @Nullable AttributeSet attrs) {
         super(context, attrs);
     }

     public HorCenterRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
     }

     private int lastWidth;
     @Override
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
         super.onLayout(changed, l, t, r, b);
         if (getChildCount() > 0) {
             int newWidth = 0;
             for (int i = 0; i < getChildCount(); i++) {
                 newWidth += getChildAt(i).getMeasuredWidth();
             }
             if (lastWidth!=newWidth) {
                 lastWidth = newWidth;

                 int empty = getMeasuredWidth() - newWidth;
                 if (empty > 0) {
                     if (getPaddingLeft() == empty / 2) {
                         return;
                     }

                     setPadding(empty / 2, 0, empty / 2, 0);
                     //如果不再一次onLayout,子view就不会有padding
                     super.onLayout(changed, l, t, r, b);
                 }
             }
         }
     }

 //    //固定宽度的居中方式
 //    protected void onMeasure(int widthSpec, int heightSpec) {
 //        if (getAdapter()==null) {
 //            super.onMeasure(widthSpec, heightSpec);
 //        } else {
 //            int specHeight = MeasureSpec.getSize(heightSpec);
 //            int maxHeight = MeasureSpec.makeMeasureSpec(specHeight, MeasureSpec.AT_MOST);
 //            //item_data weidth:60dp
 //            setMeasuredDimension(DensityUtil.dip2px(getContext(),60*getAdapter().getItemCount()), maxHeight);
 //        }
 //    }

 }

原理是往RecyclerView里面加padding

RecyclerView Expandable的实现

最简单的实现是 列出所有子显示数据和标题显示数据一齐显示,适时的使用以下方法收缩扩展:

 public final void notifyDataSetChanged()
         public final void notifyItemChanged(int position)
         public final void notifyItemRangeChanged(int positionStart, int itemCount)
         public final void notifyItemInserted(int position) 
         public final void notifyItemMoved(int fromPosition, int toPosition)
         public final void notifyItemRangeInserted(int positionStart, int itemCount)
         public final void notifyItemRemoved(int position)
         public final void notifyItemRangeRemoved(int positionStart, int itemCount)

github上有一个不错的实现,不用重写RecyclerView,只需要重写adapter,写法优雅~ https://github.com/bignerdranch/expandable-recycler-view

 public class MettingListAdapter extends ExpandableRecyclerAdapter<ListParentViewHolder, ListChlidViewHolder> {
 public MettingListAdapter2(@NonNull List<MettingVo> parentItemList) {
         super(parentItemList);
     }

     @Override
     public MettingListParentViewHolder onCreateParentViewHolder(ViewGroup parentViewGroup) {
         View view= LayoutInflater.from(parentViewGroup.getContext()).inflate(ID,parentViewGroup,false);
         return new MettingListParentViewHolder(view);
     }

     @Override
     public MettingListChlidViewHolder onCreateChildViewHolder(ViewGroup childViewGroup) {
         View view= LayoutInflater.from(childViewGroup.getContext()).inflate(childId,childViewGroup,false);
         return new MettingListChlidViewHolder(view);
     }

     @Override
     public void onBindParentViewHolder(MettingListParentViewHolder parentViewHolder, int position, ParentListItem parentListItem) {
         //onbind是自定义的
         parentViewHolder.onBind(parentViewHolder,position,parentListItem);
     }

     @Override
     public void onBindChildViewHolder(MettingListChlidViewHolder childViewHolder, int position, Object childListItem) {
         childViewHolder.onBind(childViewHolder,position,childListItem,mchildViewClickListener);
     }

     @Override
     public void onParentListItemCollapsed(int position) {
         super.onParentListItemCollapsed(position);

     }

     @Override
     public void onParentListItemExpanded(int position) {
         super.onParentListItemExpanded(position);
         //自定义展开动画
     }
 }