android:layout_width="100dp"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent">
//左边数据布局
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:text="text"
android:gravity="center"/>
//二级列表里套一个RecyclerView布局
android:layout_width="match_parent"
android:layout_height="match_parent">
//二级列表第一级
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="父布局"
android:textSize="26dp"/>
//二级列表第二级
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="text"
android:textSize="26dp"
android:layout_marginTop="10dp"/>
//M层(需要两个bean类)
public class ClassifyModel {
public void loginleft(int cid,final ModelView modelView){
String url="https://www.zhaoapi.cn/product/getCatagory";
OkhtttpUtils.getInstance().doGet(url, new OkhtttpUtils.OkCallback() {
@Override
public void onFailure(Exception e) {
}
@Override
public void onResponse(String json) {
ClassifyLeftBean classifyLeftBean = new Gson().fromJson(json, ClassifyLeftBean.class);
String code = classifyLeftBean.getCode();
if ("0".equalsIgnoreCase(code)){
if (modelView!=null){
modelView.onSuccess(classifyLeftBean);
}
}else {
if (modelView!=null){
modelView.onFailure("失败");
}
}
}
});
String u="https://www.zhaoapi.cn/product/getProductCatagory";
Map
map.put("cid",cid+"");
OkhtttpUtils.getInstance().doPost(u, map, new OkhtttpUtils.OkCallback() {
@Override
public void onFailure(Exception e) {
}
@Override
public void onResponse(String json) {
ClassifyRightBean classifyRightBean = new Gson().fromJson(json, ClassifyRightBean.class);
String code = classifyRightBean.getCode();
if ("0".equalsIgnoreCase(code)){
if (modelView!=null){
modelView.onSuccess2(classifyRightBean);
}
}else {
if (modelView!=null){
modelView.onFailure2("失败");
}
}
}
});
}
public interface ModelView{
void onSuccess(ClassifyLeftBean classifyLeftBean);
void onFailure(String error);
void onSuccess2(ClassifyRightBean classifyRightBean);
void onFailure2(String error);
}
}
//P层
public class ClassifyPresenter extends BasePresenter
private ClassifyModel classifyModel;
public ClassifyPresenter(ClassifyView view) {
super(view);
}
@Override
protected void initModel() {
classifyModel = new ClassifyModel();
}
public void loginleft(int cid){
classifyModel.loginleft(cid,new ClassifyModel.ModelView() {
@Override
public void onSuccess(ClassifyLeftBean classifyLeftBean) {
if (view!=null){
view.onClassifySuccess(classifyLeftBean);
}
}
@Override
public void onFailure(String error) {
if (view!=null){
view.onClassifyFailure(error);
}
}
@Override
public void onSuccess2(ClassifyRightBean classifyRightBean) {
if (view!=null){
view.onClassifySuccess2(classifyRightBean);
}
}
@Override
public void onFailure2(String error) {
if (view!=null){
view.onClassifyFailure2(error);
}
}
});
}
}
//V层
public class ClassifyFragment extends BaseFragment
private List
private RecyclerView recyclerView;
private MyRecyclerAdapter myRecyclerAdapter;
private int cid=1;
private MyExpandAdapter myExpandAdapter;
private ExpandableListView expandableListView;
@Override
protected void initListener() {
}
@Override
protected void initDatas() {
presenter.loginleft(cid);
}
@Override
protected void initViews() {
recyclerView = getView().findViewById(R.id.recycler);
expandableListView = getView().findViewById(R.id.classify_expand);
}
@Override
protected ClassifyPresenter providePresenter() {
return new ClassifyPresenter(this);
}
@Override
protected int provideLayoutId() {
return R.layout.activity_classify_fragment;
}
@Override
public void onClassifySuccess(ClassifyLeftBean classifyLeftBean) {
List
list.addAll(data);
myRecyclerAdapter = new MyRecyclerAdapter(list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(myRecyclerAdapter);
myRecyclerAdapter.setOnClassifyClickListance(new MyRecyclerAdapter.OnClassifyClickListance() {
@Override
public void onClassifyClick(int cid) {
++cid;
presenter.loginleft(cid);
}
});
}
@Override
public void onClassifyFailure(String error) {
}
@Override
public void onClassifySuccess2(ClassifyRightBean classifyRightBean) {
List
myExpandAdapter = new MyExpandAdapter(data, getContext());
expandableListView.setAdapter(myExpandAdapter);
for (int i = 0; i < data.size(); i++) {
expandableListView.expandGroup(i);
}
}
@Override
public void onClassifyFailure2(String error) {
}
@Override
public Context context() {
return getActivity();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
//左边RecyclerView适配器
public class MyRecyclerAdapter extends RecyclerView.Adapter
private List
public MyRecyclerAdapter(List
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.classify_recycler_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
holder.textView.setText(list.get(position).getName());
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int cid = list.get(position).getCid();
onClassifyClickListance.onClassifyClick(cid);
}
});
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.classify_recycler_textview);
}
}
OnClassifyClickListance onClassifyClickListance;
public void setOnClassifyClickListance(OnClassifyClickListance onClassifyClickListance){
this.onClassifyClickListance=onClassifyClickListance;
}
public interface OnClassifyClickListance{
void onClassifyClick(int cid);
}
}
//二级列表里的RecyclerView适配器
public class MyChildRecyclerAdapter extends RecyclerView.Adapter
private List
private Context context;
public MyChildRecyclerAdapter(List
this.list = list;
this.context = context;
}
@NonNull
@Override
public IViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.classify_child_item_recycler, parent, false);
return new IViewHolder(view);
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
@Override
public void onBindViewHolder(@NonNull IViewHolder holder, int position) {
String icon = list.get(position).getIcon();
ImageLoader.getInstance().displayImage(icon,holder.imageView, MyApp.getOptions());
holder.textView.setText(list.get(position).getName());
}
public class IViewHolder extends RecyclerView.ViewHolder{
private final TextView textView;
private final ImageView imageView;
public IViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.classify_expand_child_recycler_image);
textView = itemView.findViewById(R.id.classify_expand_child_recycler_text);
}
}
}
//二级列表适配器
public class MyExpandAdapter extends BaseExpandableListAdapter {
private List
private Context context;
private MyChildRecyclerAdapter childRecyclerAdapter;
public MyExpandAdapter(List
this.list = list;
this.context = context;
}
@Override
public int getGroupCount() {
return list == null ? 0 : list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ParentViewHolder parentViewHolder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.classify_expand_parent, null);
parentViewHolder = new ParentViewHolder(convertView);
convertView.setTag(parentViewHolder);
} else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
parentViewHolder.classifyExpandParentName.setText(list.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.classify_expand_child, null);
childViewHolder=new ChildViewHolder(convertView);
convertView.setTag(childViewHolder);
}else {
childViewHolder=(ChildViewHolder)convertView.getTag();
}
List
GridLayoutManager gridLayoutManager = new GridLayoutManager(parent.getContext(), 2);
childRecyclerAdapter=new MyChildRecyclerAdapter(listBeans,parent.getContext());
childViewHolder.classifyExpandChildRecycler.setLayoutManager(gridLayoutManager);
childViewHolder.classifyExpandChildRecycler.setAdapter(childRecyclerAdapter);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
static class ParentViewHolder {
@BindView(R.id.classify_expand_parent_name)
TextView classifyExpandParentName;
ParentViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
static class ChildViewHolder {
@BindView(R.id.classify_expand_child_recycler)
RecyclerView classifyExpandChildRecycler;
ChildViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
//V层接口
public interface ClassifyView extends IView{
void onClassifySuccess(ClassifyLeftBean classifyLeftBean);
void onClassifyFailure(String error);
void onClassifySuccess2(ClassifyRightBean classifyRightBean);
void onClassifyFailure2(String error);
}