ClassFragment
package com.example.a1512qjd.ui.fragment; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; import android.widget.ImageView; import com.example.a1512qjd.R; import com.example.a1512qjd.bean.CatagoryBean; import com.example.a1512qjd.bean.ProductCatagoryBean; import com.example.a1512qjd.presenter.CatagoryPresenterImp; import com.example.a1512qjd.presenter.ProductCatagoryPresenterImp; import com.example.a1512qjd.ui.adapter.ElvAdapter; import com.example.a1512qjd.ui.adapter.RvLeftAdapter; import com.example.a1512qjd.ui.inter.CatagoryView; import com.example.a1512qjd.ui.inter.OnItemClickListener; import com.example.a1512qjd.ui.inter.ProductCatagoryView; import java.util.List; /** * 分类 */ public class ClassFragment extends Fragment implements CatagoryView, ProductCatagoryView { private View view; private ImageView mIvZxing; private RecyclerView mRvLeft; private ImageView mIv; private ExpandableListView mElv; private CatagoryPresenterImp catagoryPresenterImp; private ProductCatagoryPresenterImp productCatagoryPresenterImp; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_class, null); //初始化组件 initView(view); //先绑定 catagoryPresenterImp = new CatagoryPresenterImp(this); productCatagoryPresenterImp = new ProductCatagoryPresenterImp(this); //请求数据 catagoryPresenterImp.getCatagory(); return view; } @Override public void onDestroy() { super.onDestroy(); //解绑 catagoryPresenterImp.detach(); } private void initView(View view) { mIvZxing = (ImageView) view.findViewById(R.id.ivZxing); mRvLeft = (RecyclerView) view.findViewById(R.id.rvLeft); mIv = (ImageView) view.findViewById(R.id.iv); mElv = (ExpandableListView) view.findViewById(R.id.elv); mIv.setBackgroundResource(R.drawable.timg); } @Override public void showData(final List<CatagoryBean.DataBean> data) { //设置布局管理器 mRvLeft.setLayoutManager(new LinearLayoutManager(getContext())); mRvLeft.addItemDecoration(new DividerItemDecoration(getContext(), RecyclerView.VERTICAL)); //创建适配器 final RvLeftAdapter adapter = new RvLeftAdapter(getContext(), data); //显示左侧列表数据 mRvLeft.setAdapter(adapter); //根据左侧列表第一项的cid去请求右侧的数据 productCatagoryPresenterImp.getProductCatagory(data.get(0).getCid() + ""); //设置第一个为默认选中 adapter.changeCheck(0, true); //设置点击事件 adapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(int position) { //改变DataBean里面check的属性值 adapter.changeCheck(position, true); //请求右侧对应的数据 productCatagoryPresenterImp.getProductCatagory(data.get(position).getCid() + ""); } @Override public void onLongItemClick(int position) { } }); } @Override public void showData(List<String> groupList, List<List<ProductCatagoryBean.DataBean.ListBean>> childList) { //使用ExpandableListView展示数据 ElvAdapter elvAdapter = new ElvAdapter(getContext(), groupList, childList); mElv.setAdapter(elvAdapter); //默认展开列表 for (int i = 0; i < groupList.size(); i++) { mElv.expandGroup(i); } } }
fragment_class.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="50dp" android:background="#ff3660" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/ivZxing" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="15dp" android:background="@drawable/a_s"/> <LinearLayout android:layout_width="match_parent" android:layout_height="30dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_weight="1" android:background="@drawable/shape_search"></LinearLayout> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:background="@drawable/my_msg_bai"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <android.support.v7.widget.RecyclerView android:id="@+id/rvLeft" 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="match_parent" android:layout_weight="7" android:orientation="vertical"> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="150dp"/> <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="match_parent" ></ExpandableListView> </LinearLayout> </LinearLayout>
</LinearLayout>
CatagoryModel
package com.example.a1512qjd.model.inter; import com.example.a1512qjd.net.OnNetListener; public interface CatagoryModel { void getCatagory(OnNetListener onNetListener);
}
CatagoryModelImp
package com.example.a1512qjd.model; import com.example.a1512qjd.bean.CatagoryBean; import com.example.a1512qjd.model.inter.CatagoryModel; import com.example.a1512qjd.net.Api; import com.example.a1512qjd.net.OkhttpUtils; import com.example.a1512qjd.net.OnNetListener; import com.google.gson.Gson; public class CatagoryModelImp implements CatagoryModel { @Override public void getCatagory(final OnNetListener onNetListener) { OkhttpUtils.getInstance().doGet(Api.CATAGORY_URL, new OnNetListener() { @Override public void onSuccess(String result) { CatagoryBean catagoryBean = new Gson().fromJson(result, CatagoryBean.class); if ("0".equals(catagoryBean.getCode())){ onNetListener.onSuccess(result); } } @Override public void onFailed(Exception e) { onNetListener.onFailed(e); } }); } }
CatagoryView
package com.example.a1512qjd.ui.inter; import com.example.a1512qjd.bean.CatagoryBean; import java.util.List; public interface CatagoryView { void showData(List<CatagoryBean.DataBean> data); }
CatagoryPresenter
package com.example.a1512qjd.presenter.inter; public interface CatagoryPresenter { void getCatagory(); }
CatagoryPresenterImp
package com.example.a1512qjd.presenter; import com.example.a1512qjd.bean.CatagoryBean; import com.example.a1512qjd.model.CatagoryModelImp; import com.example.a1512qjd.net.OnNetListener; import com.example.a1512qjd.presenter.inter.CatagoryPresenter; import com.example.a1512qjd.ui.inter.CatagoryView; import com.google.gson.Gson; public class CatagoryPresenterImp implements CatagoryPresenter { private final CatagoryModelImp catagoryModelImp; private CatagoryView catagoryView; public CatagoryPresenterImp(CatagoryView catagoryView) { this.catagoryView = catagoryView; catagoryModelImp = new CatagoryModelImp(); } @Override public void getCatagory() { catagoryModelImp.getCatagory(new OnNetListener() { @Override public void onSuccess(String result) { if (catagoryView != null) { CatagoryBean catagoryBean = new Gson().fromJson(result, CatagoryBean.class); catagoryView.showData(catagoryBean.getData()); } } @Override public void onFailed(Exception e) { if (catagoryView != null) { //失败的处理,未写,暂定 } } }); } public void detach() { if (catagoryView != null) { catagoryView = null; } }
}
ProductCatagoryModel
package com.example.a1512qjd.model.inter; import com.example.a1512qjd.net.OnNetListener; public interface ProductCatagoryModel { void getProductCatagory(String cid, OnNetListener onNetListener);
}
ProductCatagoryModelImp
package com.example.a1512qjd.model; import com.example.a1512qjd.bean.ProductCatagoryBean; import com.example.a1512qjd.model.inter.ProductCatagoryModel; import com.example.a1512qjd.net.Api; import com.example.a1512qjd.net.OkhttpUtils; import com.example.a1512qjd.net.OnNetListener; import com.google.gson.Gson; import java.util.HashMap; import java.util.Map; public class ProductCatagoryModelImp implements ProductCatagoryModel { @Override public void getProductCatagory(String cid, final OnNetListener onNetListener) { //创建Map集合 Map<String, String> params = new HashMap<>(); params.put("cid", cid); OkhttpUtils.getInstance().doPost(Api.PRODUCTCATAGORY_URL, params, new OnNetListener() { @Override public void onSuccess(String result) { ProductCatagoryBean productCatagoryBean = new Gson().fromJson(result, ProductCatagoryBean.class); if ("0".equals(productCatagoryBean.getCode())) { onNetListener.onSuccess(result); } } @Override public void onFailed(Exception e) { onNetListener.onFailed(e); } }); }
}
ProductCatagoryView
package com.example.a1512qjd.ui.inter; import com.example.a1512qjd.bean.ProductCatagoryBean; import java.util.List; public interface ProductCatagoryView { void showData(List<String> groupList, List<List<ProductCatagoryBean.DataBean.ListBean>> childList);
}
ProductCatagoryPresenter
package com.example.a1512qjd.presenter.inter; public interface ProductCatagoryPresenter { void getProductCatagory(String cid);
}
ProductCatagoryModelImp
package com.example.a1512qjd.presenter; import com.example.a1512qjd.bean.ProductCatagoryBean; import com.example.a1512qjd.model.ProductCatagoryModelImp; import com.example.a1512qjd.net.OnNetListener; import com.example.a1512qjd.presenter.inter.ProductCatagoryPresenter; import com.example.a1512qjd.ui.inter.ProductCatagoryView; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class ProductCatagoryPresenterImp implements ProductCatagoryPresenter { private ProductCatagoryView view; private final ProductCatagoryModelImp productCatagoryModelImp; public ProductCatagoryPresenterImp(ProductCatagoryView view) { this.view = view; productCatagoryModelImp = new ProductCatagoryModelImp(); } @Override public void getProductCatagory(String cid) { productCatagoryModelImp.getProductCatagory(cid, new OnNetListener() { @Override public void onSuccess(String result) { //定义一个集合用于存放title List<String> groupList = new ArrayList<>(); //定义一个集合用于存放title对应的内容 List<List<ProductCatagoryBean.DataBean.ListBean>> childList = new ArrayList<>(); ProductCatagoryBean productCatagoryBean = new Gson().fromJson(result, ProductCatagoryBean.class); List<ProductCatagoryBean.DataBean> data = productCatagoryBean.getData(); for (int i = 0; i < data.size(); i++) { groupList.add(data.get(i).getName()); List<ProductCatagoryBean.DataBean.ListBean> list = data.get(i).getList(); childList.add(list); } if (view != null) { view.showData(groupList, childList); } } @Override public void onFailed(Exception e) { } }); } }
OnItemClickListener
package com.example.a1512qjd.ui.inter; public interface OnItemClickListener { void onItemClick(int position); void onLongItemClick(int position);
}
RvLeftAdapter
package com.example.a1512qjd.ui.adapter; import android.content.Context; import android.graphics.Color; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.a1512qjd.R; import com.example.a1512qjd.bean.CatagoryBean; import com.example.a1512qjd.ui.inter.OnItemClickListener; import java.util.List; public class RvLeftAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<CatagoryBean.DataBean> list; private LayoutInflater inflater; private OnItemClickListener onItemClickListener; public RvLeftAdapter(Context context, List<CatagoryBean.DataBean> list) { this.context = context; this.list = list; inflater = LayoutInflater.from(context); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.rvleft_item, parent, false); LeftViewHolder leftViewHolder = new LeftViewHolder(view); return leftViewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) { CatagoryBean.DataBean dataBean = list.get(position); LeftViewHolder leftViewHolder = (LeftViewHolder) holder; leftViewHolder.tv.setText(dataBean.getName()); //设置字体颜色 if (dataBean.getChecked()) { leftViewHolder.tv.setTextColor(Color.RED); leftViewHolder.tv.setBackgroundColor(Color.GRAY); } else { leftViewHolder.tv.setTextColor(Color.BLACK); leftViewHolder.tv.setBackgroundColor(Color.WHITE); } leftViewHolder.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(position); } } }); } @Override public int getItemCount() { return list.size(); } class LeftViewHolder extends RecyclerView.ViewHolder { private final TextView tv; public LeftViewHolder(View itemView) { super(itemView); tv = itemView.findViewById(R.id.tv); } } /** * 选中后,改变字体颜色和背景色 * * @param position * @param bool */ public void changeCheck(int position, boolean bool) { //先还原一下 for (int i = 0;i<list.size();i++){ list.get(i).setChecked(false); } CatagoryBean.DataBean dataBean = list.get(position); dataBean.setChecked(bool); //刷新界面 notifyDataSetChanged(); }
}
rvleft_item.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:gravity="center"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center"/>
</LinearLayout>
ElvAdapter
package com.example.a1512qjd.ui.adapter; import android.content.Context; import android.content.Intent; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; import com.example.a1512qjd.R; import com.example.a1512qjd.bean.ProductCatagoryBean; import com.example.a1512qjd.ui.activity.ListActivity; import com.example.a1512qjd.ui.inter.OnItemClickListener; import java.util.List; public class ElvAdapter extends BaseExpandableListAdapter { private Context context; private List<String> groupList; private List<List<ProductCatagoryBean.DataBean.ListBean>> childList; private LayoutInflater inflater; public ElvAdapter(Context context, List<String> groupList, List<List<ProductCatagoryBean.DataBean.ListBean>> childList) { this.context = context; this.groupList = groupList; this.childList = childList; inflater = LayoutInflater.from(context); } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return 1;//因为二级列表是一个RecylerView,所以返回1即可 } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).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 true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder groupViewHolder; if (convertView == null) { groupViewHolder = new GroupViewHolder(); convertView = inflater.inflate(R.layout.rvleft_item, null); groupViewHolder.tv = convertView.findViewById(R.id.tv); convertView.setTag(groupViewHolder); } else { groupViewHolder = (GroupViewHolder) convertView.getTag(); } //显示数据 groupViewHolder.tv.setText(groupList.get(groupPosition)); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildViewHolder childViewHolder; if (convertView == null) { childViewHolder = new ChildViewHolder(); convertView = inflater.inflate(R.layout.elv_rv, null); childViewHolder.rv = convertView.findViewById(R.id.rv); convertView.setTag(childViewHolder); } else { childViewHolder = (ChildViewHolder) convertView.getTag(); } //显示数据 final List<ProductCatagoryBean.DataBean.ListBean> listBeans = childList.get(groupPosition); //设置布局管理器 childViewHolder.rv.setLayoutManager(new GridLayoutManager(context, 3)); //设置适配器 ElvRvAdapter elvRvAdapter = new ElvRvAdapter(context, listBeans); childViewHolder.rv.setAdapter(elvRvAdapter); elvRvAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(int position) { //点击跳转到列表页面 Intent intent = new Intent(context, ListActivity.class); int pscid = listBeans.get(position).getPscid(); intent.putExtra("pscid", pscid); context.startActivity(intent); } @Override public void onLongItemClick(int position) { } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class GroupViewHolder { TextView tv; } class ChildViewHolder { RecyclerView rv; }
}
elv_rv.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.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
ElvRvAdapter
package com.example.a1512qjd.ui.adapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.a1512qjd.R; import com.example.a1512qjd.bean.ProductCatagoryBean; import com.example.a1512qjd.ui.inter.OnItemClickListener; import java.util.List; public class ElvRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<ProductCatagoryBean.DataBean.ListBean> listBeans; private LayoutInflater inflater; private OnItemClickListener onItemClickListener; public ElvRvAdapter(Context context, List<ProductCatagoryBean.DataBean.ListBean> listBeans) { this.context = context; this.listBeans = listBeans; inflater = LayoutInflater.from(context); } public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.rvclass_item, parent, false); ElvRvAdapterViewHolder elvRvAdapterViewHolder = new ElvRvAdapterViewHolder(view); return elvRvAdapterViewHolder; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) { ElvRvAdapterViewHolder elvRvAdapterViewHolder = (ElvRvAdapterViewHolder) holder; ProductCatagoryBean.DataBean.ListBean listBean = listBeans.get(position); Glide.with(context).load(listBean.getIcon()).into(elvRvAdapterViewHolder.iv); elvRvAdapterViewHolder.tv.setText(listBean.getName()); //给条目设置点击事件 elvRvAdapterViewHolder.ll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListener != null) { onItemClickListener.onItemClick(position); } } }); } @Override public int getItemCount() { return listBeans.size(); } class ElvRvAdapterViewHolder extends RecyclerView.ViewHolder { private final ImageView iv; private final TextView tv; private final LinearLayout ll; public ElvRvAdapterViewHolder(View itemView) { super(itemView); ll = itemView.findViewById(R.id.ll); iv = itemView.findViewById(R.id.iv); tv = itemView.findViewById(R.id.tv); } } }
rvclass_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/iv" android:layout_width="50dp" android:layout_height="50dp"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>