因项目中用到九宫格展开与隐藏的功能,利用recyclerView简单的实现了下。
效果
MainActivity
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private void initData() {
List<CategoryEntity.ChildEntity> childList = new ArrayList<>();
CategoryEntity.ChildEntity childEntity1 = new CategoryEntity.ChildEntity();
childEntity1.setId("1");
childEntity1.setName("child1");
CategoryEntity.ChildEntity childEntity2 = new CategoryEntity.ChildEntity();
childEntity2.setId("2");
childEntity2.setName("child2");
CategoryEntity.ChildEntity childEntity3 = new CategoryEntity.ChildEntity();
childEntity3.setId("3");
childEntity3.setName("child3");
CategoryEntity.ChildEntity childEntity4 = new CategoryEntity.ChildEntity();
childEntity4.setId("4");
childEntity4.setName("child4");
CategoryEntity.ChildEntity childEntity5 = new CategoryEntity.ChildEntity();
childEntity5.setId("5");
childEntity5.setName("child5");
CategoryEntity.ChildEntity childEntity6 = new CategoryEntity.ChildEntity();
childEntity6.setId("6");
childEntity6.setName("child6");
CategoryEntity.ChildEntity childEntity7 = new CategoryEntity.ChildEntity();
childEntity7.setId("7");
childEntity7.setName("child7");
childList.add(childEntity1);
childList.add(childEntity2);
childList.add(childEntity3);
childList.add(childEntity4);
childList.add(childEntity5);
childList.add(childEntity6);
childList.add(childEntity7);
CategoryEntity categoryEntity = new CategoryEntity();
categoryEntity.setId("1");
categoryEntity.setName("标题1");
categoryEntity.setChild(childList);
List<CategoryEntity> mList = new ArrayList<>();//数据太多数组中只加一组数据了
mList.add(categoryEntity);
MainAdapter adapter = new MainAdapter(this,mList);
recyclerView.setAdapter(adapter);
}
MainAdapter代码
private List<CategoryEntity> mList;
private Context mContext;
public MainAdapter(Context context, List<CategoryEntity> list){
this.mContext = context;
this.mList = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_main,parent,false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final CategoryEntity entity = getItem(position);
holder.tvTitle.setText(entity.getName());
final MyGrideAdapter myGrideAdapter = initGridView(holder,entity);
holder.tvMore.setText(entity.showMore? "收起":"更多");
holder.ivArrow.setImageResource(entity.showMore? R.mipmap.arrow_up:R.mipmap.arrow_down);
holder.llMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
entity.showMore = !entity.showMore;
myGrideAdapter.setShowMore(entity.showMore);
holder.tvMore.setText(entity.showMore?"收起":"更多");
holder.ivArrow.setImageResource(entity.showMore?R.mipmap.arrow_up:R.mipmap.arrow_down);
notifyItemChanged(mList.indexOf(entity));
}
});
}
private MyGrideAdapter initGridView(ViewHolder holder, CategoryEntity entity) {
List<CategoryEntity.ChildEntity> child = entity.getChild();
MyGrideAdapter myGrideAdapter = new MyGrideAdapter(entity);
holder.childGrideView.setAdapter(myGrideAdapter);
return myGrideAdapter;
}
@Override
public int getItemCount() {
return mList==null? 0:mList.size();
}
public CategoryEntity getItem(int position){
if(mList!=null){
return mList.get(position);
}
return null;
}
class ViewHolder extends RecyclerView.ViewHolder{
private final TextView tvTitle,tvMore;
private CustomGridView childGrideView;
private final ImageView ivArrow;
private final LinearLayout llMore;
public ViewHolder(View itemView) {
super(itemView);
tvTitle = (TextView)itemView.findViewById(R.id.tv_title);
tvMore = (TextView)itemView.findViewById(R.id.tv_more);
ivArrow = (ImageView)itemView.findViewById(R.id.iv_arrow);
childGrideView = (CustomGridView)itemView.findViewById(R.id.cgv_child);
llMore = (LinearLayout)itemView.findViewById(R.id.ll_more);
}
}
class MyGrideAdapter extends BaseAdapter{
public boolean showMore = false;
private List<CategoryEntity.ChildEntity> mChildList;
public MyGrideAdapter(CategoryEntity entity){
showMore = entity.showMore;
this.mChildList = entity.getChild();
}
@Override
public int getCount() {
return mChildList == null ? 0 : (showMore ? mChildList.size() : (mChildList.size() > 3 ? 3 : mChildList.size()));
}
@Override
public Object getItem(int position) {
return mChildList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView= new TextView(parent.getContext());
((TextView)convertView).setHeight(getPixByDp(40, parent.getContext()));
((TextView)convertView).setPadding(getPixByDp(16,parent.getContext()),0,0,0);
((TextView)convertView).setGravity(Gravity.CENTER_VERTICAL);
((TextView)convertView).setTextSize(16f);
((TextView)convertView).setSingleLine();
((TextView)convertView).setEllipsize(TextUtils.TruncateAt.END);
Drawable drawable =parent.getContext().getResources().getDrawable(R.drawable.divider_line);
drawable.setBounds(0,0,getPixByDp(1,parent.getContext()),getPixByDp(20,parent.getContext()));
if(position%3 == 2){
((TextView) convertView).setCompoundDrawables(null,null, null,null);
}else{
((TextView) convertView).setCompoundDrawables(null,null, drawable,null);
}
}
((TextView) convertView).setText(mChildList.get(position).getName());
return convertView;
}
public void setShowMore(boolean showMore) {
this.showMore = showMore;
this.notifyDataSetChanged();
}
}
/**
* 根据dp获取pix值
* @param dp
* @param context
* @return
*/
public static int getPixByDp(int dp,Context context){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
CustomGrideView代码
public class CustomGridView extends GridView {
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* 设置不滚动
*/
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
CategoryEntity代码
/**
* child : [{"icon":"","id":"2541","name":"立体车库","url":"http://m.3158.17kb.com/qy/liticheku/"},{"icon":"","id":"2555","name":"洗车","url":"http://m.3158.17kb.com/qy/xiche/"},{"icon":"","id":"2569","name":"汽车配件","url":"http://m.3158.17kb.com/qy/qichepeijian/"},{"icon":"","id":"2583","name":"汽修产品","url":"http://m.3158.17kb.com/qy/qixiuchanpin/"},{"icon":"","id":"2597","name":"汽车外饰及配件","url":"http://m.3158.17kb.com/qy/qichewaishipeijian/"},{"icon":"","id":"2611","name":"汽车改装","url":"http://m.3158.17kb.com/qy/qichegaizhuang/"},{"icon":"","id":"2625","name":"影音导航","url":"http://m.3158.17kb.com/qy/yingyindaohang/"},{"icon":"","id":"2639","name":"汽车内饰用品","url":"http://m.3158.17kb.com/qy/qicheneishiyongpin/"},{"icon":"","id":"2653","name":"座垫脚垫","url":"http://m.3158.17kb.com/qy/zuodianjiaodian/"},{"icon":"","id":"2667","name":"汽车防爆膜","url":"http://m.3158.17kb.com/qy/qichefangbaomo/"},{"icon":"","id":"2681","name":"车载电器","url":"http://m.3158.17kb.com/qy/chezaidianqi/"},{"icon":"","id":"2695","name":"汽车美容养护","url":"http://m.3158.17kb.com/qy/qichemeirongyanghu/"},{"icon":"","id":"2709","name":"儿童安全座椅","url":"http://m.3158.17kb.com/qy/dongtonganquanzuoyi/"},{"icon":"","id":"2723","name":"便捷式GPS导航","url":"http://m.3158.17kb.com/qy/cheyonggpsdaohang/"},{"icon":"","id":"2737","name":"行车记录仪","url":"http://m.3158.17kb.com/qy/xingchejiluyi/"},{"icon":"","id":"2751","name":"车载摄像头","url":"http://m.3158.17kb.com/qy/chezaishexiangtou/"},{"icon":"","id":"2765","name":"智能后视镜导航","url":"http://m.3158.17kb.com/qy/houshijingdaohang/"},{"icon":"","id":"2779","name":"汽车改装件","url":"http://m.3158.17kb.com/qy/qichegaizhuangjian/"},{"icon":"","id":"2806","name":"汽保工具套装","url":"http://m.3158.17kb.com/qy/qibaogongjutaozhuang/"},{"icon":"","id":"2819","name":"维修工具","url":"http://m.3158.17kb.com/qy/weixiugongju/"},{"icon":"","id":"2831","name":"车用轮胎","url":"http://m.3158.17kb.com/qy/cheyongluntai/"},{"icon":"","id":"2843","name":"自驾装备","url":"http://m.3158.17kb.com/qy/zijiazhuangbei/"},{"icon":"","id":"2854","name":"车用汽油添加剂","url":"http://m.3158.17kb.com/qy/cheyongqiyoutianjiaji/"},{"icon":"","id":"2863","name":"汽车清洁工具","url":"http://m.3158.17kb.com/qy/qicheqingjiegongju/"},{"icon":"","id":"2871","name":"其他","url":"http://m.3158.17kb.com/qy/qitaqiyefuwu/"}]
* id : 2527
* name : 汽车服务
*/
private String id;
private String name;
public boolean showMore;
/**
* id : 2541
* name : 立体车库
*/
private List<ChildEntity> child;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<ChildEntity> getChild() {
return child;
}
public void setChild(List<ChildEntity> child) {
this.child = child;
}
public static class ChildEntity implements Serializable {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
divider_line代码
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="1dp" android:height="15dp"/>
<solid android:color="#f5f5f5"/>
</shape>
所用到的图片以及边距大小可以自由替换。