加入的依赖
compile 'com.hjm:BottomTabBar:1.1.1' implementation 'com.android.support:recyclerview-v7:26.1.0' implementation 'io.reactivex:rxjava:1.1.0' implementation 'io.reactivex:rxandroid:1.1.0' implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4' implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4' implementation 'com.squareup.okhttp3:okhttp:3.10.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0' implementation 'com.facebook.fresco:fresco:1.5.0' implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7' implementation 'org.greenrobot:eventbus:3.0.0'
public class MainActivity extends AppCompatActivity { private BottomTabBar bottab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottab = (BottomTabBar) findViewById(R.id.bottab); bottab.init(getSupportFragmentManager()) .setImgSize(120,120) .setFontSize(0) .setTabPadding(4,6,10) .setChangeColor(Color.RED,Color.DKGRAY)//点击切换颜色 .addTabItem(null,R.drawable.ac0, HomeFragment.class) .addTabItem(null,R.drawable.abw, FenleiFragment.class) .addTabItem(null,R.drawable.abu,ShoppingFragment.class) .addTabItem(null,R.drawable.ac2,MineFragment.class) .isShowDivider(false) .setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() { @Override public void onTabChange(int position, String name) {} }); }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="maxinawei.bw.com.gouwuche.MainActivity"> <com.hjm.bottomtabbar.BottomTabBar android:id="@+id/bottab" android:layout_width="match_parent" android:layout_height="match_parent" ></com.hjm.bottomtabbar.BottomTabBar> </LinearLayout>
分类===
===================================================
/** * Created by dell on 2018/6/29. */ public class FenleiFragment extends Fragment { private RecyclerView fenlei; private ExpandableListView fenlei_zi; List<ZiLeiBean.DataBean> groupList = new ArrayList<>(); List<List<ZiLeiBean.DataBean.ListBean>> childList = new ArrayList<>(); private int page; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fenlei_fragment, container, false); initView(view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initRecyView(); } private void initRecyView() { RetrofitApi retrofitApi = RetrofitHelper.getRetrofitHelper("https://www.zhaoapi.cn/") .retrofitApi(); retrofitApi.getFenleiDate().subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<FenLeiBean>() { @Override public void call(FenLeiBean fenLeiBean) { final List<FenLeiBean.DataBean> list = fenLeiBean.getData(); fenlei.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false)); FenLieAdapter fenleiadapter= new FenLieAdapter(list,getContext()); fenlei.setAdapter(fenleiadapter); fenleiadapter.setItemClickListener(new FenLieAdapter.OnItemClickListener() { @Override public void onItemClick(int position) { childList.clear(); groupList.clear(); int cid = list.get(position).getCid(); initZifenlei(cid); } }); } }); } private void initZifenlei(int cid){ RetrofitApi retrofitApi = RetrofitHelper.getRetrofitHelper("https://www.zhaoapi.cn/") .retrofitApi(); retrofitApi.getZiBean(cid).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<ZiLeiBean>() { @Override public void call(ZiLeiBean ziLeiBean) { List<ZiLeiBean.DataBean> data = ziLeiBean.getData(); groupList.addAll(data); for (int i = 0; i < data.size(); i++) { List<ZiLeiBean.DataBean.ListBean> listBeans = data.get(i).getList(); childList.add(listBeans); } CateExpandableAdapter expandableAdapter = new CateExpandableAdapter(getActivity(), groupList, childList); fenlei_zi.setAdapter(expandableAdapter); fenlei_zi.setGroupIndicator(null); //默认展开 for (int i = 0; i < expandableAdapter.getGroupCount(); i++) { fenlei_zi.expandGroup(i); } } }); } private void initView(View view) { fenlei = (RecyclerView) view.findViewById(R.id.fenlei); fenlei_zi = (ExpandableListView) view.findViewById(R.id.fenlei_zi); } }
二级列表的适配器==========================
/** * Created by dell on 2018/6/29. */ class CateExpandableAdapter extends BaseExpandableListAdapter implements ChildRcyAdapter.setOnItemClickListener { private Context context; List<ZiLeiBean.DataBean> groupList; List<List<ZiLeiBean.DataBean.ListBean>> childList; public CateExpandableAdapter(Context context, List<ZiLeiBean.DataBean> groupList, List<List<ZiLeiBean.DataBean.ListBean>> childList) { this.context = context; this.groupList = groupList; this.childList = childList; } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder groupViewHolder; if (convertView == null) { convertView = View.inflate(context, R.layout.group_layout, null); groupViewHolder = new GroupViewHolder(); groupViewHolder.text_group = (TextView) convertView.findViewById(R.id.text_group); convertView.setTag(groupViewHolder); } else { groupViewHolder = (GroupViewHolder) convertView.getTag(); } //赋值 groupViewHolder.text_group.setText(groupList.get(groupPosition).getName()); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder childViewHolder; if (convertView == null) { convertView = View.inflate(context, R.layout.child_layout, null); childViewHolder = new ChildViewHolder(); childViewHolder.child_rcy = convertView.findViewById(R.id.child_rcy); convertView.setTag(childViewHolder); } else { childViewHolder = (ChildViewHolder) convertView.getTag(); } //获取组 List<ZiLeiBean.DataBean.ListBean> listBeans = childList.get(groupPosition); //设置布局管理器 GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false); childViewHolder.child_rcy.setLayoutManager(gridLayoutManager); //适配器 ChildRcyAdapter childRcyAdapter = new ChildRcyAdapter(context, listBeans); childViewHolder.child_rcy.setAdapter(childRcyAdapter); //获取接口回调 childRcyAdapter.setMsetOnItemClickListener(this); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public void setItemClick(int position) { Intent intent = new Intent(context, ProductListActivity.class); intent.putExtra("position", position); context.startActivity(intent); } class GroupViewHolder { private TextView text_group; } class ChildViewHolder { private RecyclerView child_rcy; } }
分类RecyclerView============================================
/** * Created by dell on 2018/6/29. */ class FenLieAdapter extends RecyclerView.Adapter<FenLieAdapter.FenLeiHolder> implements View.OnClickListener { private List<FenLeiBean.DataBean> list; private Context context; public FenLieAdapter(List<FenLeiBean.DataBean> list, Context context) { this.list = list; this.context = context; } @Override public FenLeiHolder onCreateViewHolder(final ViewGroup parent, int viewType) { View itemView = View.inflate(context, R.layout.fenlei_item, null); FenLeiHolder holder=new FenLeiHolder(itemView); itemView.setOnClickListener(this); return holder; } @Override public void onBindViewHolder(FenLeiHolder holder, int position) { holder.name.setText(list.get(position).getName()); holder.itemView.setTag(position); } @Override public int getItemCount() { return list.size(); } @Override public void onClick(View v) { if (mItemClickListener!=null){ mItemClickListener.onItemClick((Integer) v.getTag()); } } class FenLeiHolder extends RecyclerView.ViewHolder{ private final TextView name; public FenLeiHolder(View itemView) { super(itemView); name =(TextView) itemView.findViewById(R.id.fenlei_name); } } public interface OnItemClickListener{ void onItemClick(int position); } private OnItemClickListener mItemClickListener; //暴露一个方法方便给别的类去调用 public void setItemClickListener(OnItemClickListener itemClickListener) { mItemClickListener = itemClickListener; } }
购物车页面=============================
/** * Created by dell on 2018/6/29. */ public class ShoppingFragment extends Fragment { private ExpandableListView shopcart_expandable; private CheckBox shopcart_check; private TextView shopcart_price; private TextView shopcart_jiesuan; private SmartRefreshLayout shopcart_srl; private ShopCartExpandAdapter adapter; private String image; private String title; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.shopping_fragment, container, false); initView(view); EventBus.getDefault().register(this); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //加载数据 initData(); //全选监听 shopcart_check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.changeAllListCbState(shopcart_check.isChecked()); } }); } private void initView(View view) { shopcart_expandable = (ExpandableListView) view.findViewById(R.id.shopcart_expandable); shopcart_check = (CheckBox) view.findViewById(R.id.shopcart_check); shopcart_price = (TextView) view.findViewById(R.id.shopcart_price); shopcart_jiesuan = (TextView) view.findViewById(R.id.shopcart_jiesuan); shopcart_srl = (SmartRefreshLayout) view.findViewById(R.id.shopcart_srl); } private void initData() { RetrofitApi retrofitApi = RetrofitHelper.getRetrofitHelper("https://www.zhaoapi.cn/") .retrofitApi(); retrofitApi.getCarDate("15843","4CD2868C43B7918EB647D8FC9CD2E22E") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<CarBean>() { @Override public void call(CarBean carBean) { List<CarBean.DataBean> list_group = new ArrayList<>(); List<List<CarBean.DataBean.ListBean>> list_child = new ArrayList<>(); List<CarBean.DataBean> data = carBean.getData(); list_group.addAll(data);//组 for (int i = 0; i < data.size(); i++) { List<CarBean.DataBean.ListBean> list = data.get(i).getList(); list_child.add(list); } //设置适配器 adapter = new ShopCartExpandAdapter(getActivity(), list_group, list_child); shopcart_expandable.setAdapter(adapter); shopcart_expandable.setGroupIndicator(null); //默认展开 for (int i = 0; i < adapter.getGroupCount(); i++) { shopcart_expandable.expandGroup(i); } adapter.notifyDataSetChanged(); } }); } @Subscribe public void onMessageEvent(PriceAndCountEvent event) { shopcart_jiesuan.setText("结算(" + event.getCount() + ")"); shopcart_price.setText(event.getPrice() + ""); title = event.getTitle(); image = event.getImage(); } //注销 //注销 @Override public void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }
Retrofit接口拼接数据===========================
public interface RetrofitApi { // https://www.zhaoapi.cn/ @GET("product/getCatagory") Observable<FenLeiBean> getFenleiDate(); // https://www.zhaoapi.cn/product/getProductCatagory?cid=1 @GET("product/getProductCatagory?") Observable<ZiLeiBean> getZiBean(@Query("cid") int cid); // https://www.zhaoapi.cn/uid=15843&token=4CD2868C43B7918EB647D8FC9CD2E22E @GET("product/getCarts?") Observable<CarBean> getCarDate(@Query("uid") String uid,@Query("token") String token); }封装Retrofit+Rxjava类
public class RetrofitHelper { private static RetrofitApi mRetrofitApi; private static volatile RetrofitHelper retrofitHelper; private RetrofitHelper(String url){ OkHttpClient client = new OkHttpClient.Builder() .writeTimeout(1000, TimeUnit.SECONDS) .readTimeout(1000, TimeUnit.SECONDS) .connectTimeout(1000, TimeUnit.SECONDS) .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client) .build(); mRetrofitApi= retrofit.create(RetrofitApi.class); }; public static RetrofitHelper getRetrofitHelper(String url){ if (retrofitHelper==null){ synchronized(RetrofitHelper.class){ if (retrofitHelper==null){ retrofitHelper = new RetrofitHelper(url); } } } return retrofitHelper; } public RetrofitApi retrofitApi(){ return mRetrofitApi; } }EventBus类=================
public class MessageEvent { private boolean checked; public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } }2===========================================
public class PriceAndCountEvent { private int price; private int count; private String title; private String image; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }购物车二级列表的适配器============================在Bean实体类封装进去一个ischeckd
public class ChildRcyAdapter extends RecyclerView.Adapter implements View.OnClickListener { private Context context; private List<ZiLeiBean.DataBean.ListBean> list; public ChildRcyAdapter(Context context, List<ZiLeiBean.DataBean.ListBean> list) { this.context = context; this.list = list; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View inflate = View.inflate(context, R.layout.child_rcy_layout, null); inflate.setOnClickListener(this); return new MyViewHolder(inflate); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { MyViewHolder viewHolder = (MyViewHolder) holder; viewHolder.text_child.setText(list.get(position).getName()); Uri parse = Uri.parse(list.get(position).getIcon()); viewHolder.img_child.setImageURI(parse); viewHolder.itemView.setTag(position); } @Override public int getItemCount() { return list.size(); } private class MyViewHolder extends RecyclerView.ViewHolder { private TextView text_child; private ImageView img_child; public MyViewHolder(View itemView) { super(itemView); img_child = itemView.findViewById(R.id.img_child); text_child = itemView.findViewById(R.id.text_child); } } //接口回调 private setOnItemClickListener msetOnItemClickListener; public interface setOnItemClickListener { void setItemClick(int position); } public void setMsetOnItemClickListener(setOnItemClickListener msetOnItemClickListener) { this.msetOnItemClickListener = msetOnItemClickListener; } @Override public void onClick(View v) { if (msetOnItemClickListener != null) { msetOnItemClickListener.setItemClick((Integer) v.getTag()); } } }
XML====================================
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="maxinawei.bw.com.gouwuche.ProductListActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/show_review" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
cart_child_layout.xml=============================================
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <CheckBox android:id="@+id/child_chek" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" /> <ImageView android:id="@+id/child_img" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginLeft="5dp" android:src="@mipmap/ic_launcher_round" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:orientation="vertical"> <TextView android:id="@+id/child_content" android:layout_width="150dp" android:layout_height="wrap_content" android:text="content" /> <TextView android:id="@+id/child_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="price" android:textColor="#ff00" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/child_jian" android:layout_width="20dp" android:layout_height="20dp" android:background="@mipmap/jian" /> <TextView android:id="@+id/child_num" android:layout_width="20dp" android:layout_height="20dp" android:text="1" android:textColor="#ff00" /> <ImageView android:id="@+id/child_jia" android:layout_width="20dp" android:layout_height="20dp" android:background="@mipmap/jia" /> </LinearLayout> <TextView android:id="@+id/child_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="25dp" android:text="删除" android:textSize="18dp" /> </LinearLayout> </LinearLayout>
cart_group_layout.xml============================================================
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/group_line" android:layout_height="wrap_content" android:background="#ccc"> <CheckBox android:id="@+id/group_check" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/group_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="name" android:textColor="@color/colorAccent" android:textSize="20dp" /> </LinearLayout>
child_layout.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="match_parent" android:gravity="center" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/child_rcy" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </LinearLayout>
child_rcy_layout.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="match_parent" android:layout_marginLeft="10dp" android:padding="5dp" android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/img_child" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher_round" /> <TextView android:id="@+id/text_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" /> </LinearLayout>
fenlei_fragment.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="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <android.support.v7.widget.RecyclerView android:id="@+id/fenlei" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3"></android.support.v7.widget.RecyclerView> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="7" android:orientation="vertical"> <ImageView android:id="@+id/cateGroup_banner" android:layout_width="250dp" android:layout_height="150dp" android:src="@drawable/yila" /> <ExpandableListView android:padding="5dp" android:id="@+id/fenlei_zi" android:layout_width="match_parent" android:layout_height="wrap_content"> </ExpandableListView> </LinearLayout> </LinearLayout> </LinearLayout>
group_layout.xml===========================================================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ccc" android:gravity="center"> <TextView android:id="@+id/text_group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" android:textColor="#ff00" android:textSize="28dp" /> </LinearLayout>
shopping_fragment.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="match_parent" android:orientation="vertical"> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/shopcart_srl" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.5" android:gravity="center" android:text="购物车" android:textColor="#ff00" android:textSize="20dp" /> <ExpandableListView android:id="@+id/shopcart_expandable" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9"></ExpandableListView> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:gravity="center_vertical"> <CheckBox android:id="@+id/shopcart_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:focusable="false" /> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/shopcart_check" android:gravity="center_vertical" android:text="全选" android:textSize="20sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="合计 :" /> <TextView android:id="@+id/shopcart_price" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="10dp" android:paddingRight="10dp" android:text="0" android:textColor="@android:color/holo_red_light" /> <TextView android:id="@+id/shopcart_jiesuan" android:layout_width="wrap_content" android:layout_height="50dp" android:background="@android:color/holo_red_dark" android:gravity="center" android:padding="10dp" android:text="结算(0)" android:textColor="@android:color/white" /> </LinearLayout> </RelativeLayout> </LinearLayout> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </LinearLayout>