分类主代码
public class TypeFragment extends Fragment implements TypeView {
@BindView(R.id.type_Catagory)
RecyclerView typeCatagory;
@BindView(R.id.type_exl)
ExpandableListView typeExl;
Unbinder unbinder;
private TypePresenter presenter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.type_fg, null);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
presenter = new TypePresenter(this);
//初始化分类
initCatagory();
}
private void initCatagory() {
typeCatagory.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
presenter.getCatagory();
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
presenter.onDestroy();
presenter = null;
}
@Override
public void onCatagorySuccess(Catagory catagory) {
List<Catagory.DataBean> data = catagory.getData();
CatagoryAdapter adapter = new CatagoryAdapter(data);
typeCatagory.setAdapter(adapter);
//默认请求cid=1的数据
presenter.getProductCatagory("1");
//分类点击回调
adapter.setOnCatagoryLisenter(new CatagoryAdapter.OnCatagoryLisenter() {
@Override
public void onNameClick(int cid) {
//调用p层请求子分类
presenter.getProductCatagory(cid + "");
}
});
}
@Override
public void onCatagoryFaild() {
Toast.makeText(getActivity(), "获得数据失败", Toast.LENGTH_SHORT).show();
}
@Override
public void onProductCatagorySuccess(ProductCatagory productCatagory) {
if (productCatagory.getData().size() == 0) {
Toast.makeText(getActivity(), "该分类暂时没有商品", Toast.LENGTH_SHORT).show();
} else {
List<ProductCatagory.DataBean> data = productCatagory.getData();
ExAdapter adapter = new ExAdapter(data);
typeExl.setAdapter(adapter);
//默认展开所有一级item
for (int i = 0; i < data.size(); i++) {
typeExl.expandGroup(i);
}
}
}
}
主界面
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/type_Catagory"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
<ExpandableListView
android:groupIndicator="@null"
android:id="@+id/type_exl"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="match_parent"></ExpandableListView>
</LinearLayout>
</LinearLayout>
mvp-model-TypeModel
public class TypeModel {
//获取分类
public Observable<Catagory> getCatagory() {
return RetrofitUtil.getDefault().create(MyRetrofit.class).getCatagory();
}
//获取子分类
public Observable<ProductCatagory> getProductCatagory(String cid) {
return RetrofitUtil.getDefault().create(MyRetrofit.class).getProductCatagory(cid);
}
//获取商品列表
public Observable<Products> getProducts(String pscid) {
return RetrofitUtil.getDefault().create(MyRetrofit.class).getProducts(pscid);
}
}
mvp-presenter-TypePresenter
public class TypePresenter extends BasePresenter<TypeView> {
private TypeModel typeModel;
public TypePresenter(TypeView view) {
super(view);
}
@Override
public void initModel() {
typeModel = new TypeModel();
}
//获得分类
public void getCatagory() {
typeModel.getCatagory()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Catagory>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(Catagory catagory) {
view.onCatagorySuccess(catagory);
}
@Override
public void onError(Throwable e) {
view.onCatagoryFaild();
}
@Override
public void onComplete() {
}
});
}
//获得子分类
public void getProductCatagory(String cid) {
typeModel.getProductCatagory(cid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ProductCatagory>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(ProductCatagory productCatagory) {
view.onProductCatagorySuccess(productCatagory);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
mvp-view-view
public interface TypeView extends IView{
void onCatagorySuccess(Catagory catagory);
void onCatagoryFaild();
void onProductCatagorySuccess(ProductCatagory productCatagory);
}
adapter-CatagroyAdapter
public class CatagoryAdapter extends RecyclerView.Adapter {
private List<Catagory.DataBean> list;
public CatagoryAdapter(List<Catagory.DataBean> list) {
this.list = list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.catagory_item, null);
MViewHolder mViewHolder = new MViewHolder(view);
return mViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
MViewHolder mHolder = (MViewHolder) holder;
final Catagory.DataBean bean = list.get(position);
mHolder.catagory_name.setText(bean.getName());
mHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onCatagoryLisenter != null) {
onCatagoryLisenter.onNameClick(bean.getCid());
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MViewHolder extends RecyclerView.ViewHolder {
private final TextView catagory_name;
public MViewHolder(View itemView) {
super(itemView);
catagory_name = itemView.findViewById(R.id.catagory_name);
}
}
public interface OnCatagoryLisenter {
void onNameClick(int cid);
}
private OnCatagoryLisenter onCatagoryLisenter;
public void setOnCatagoryLisenter(OnCatagoryLisenter onCatagoryLisenter) {
this.onCatagoryLisenter = onCatagoryLisenter;
}
}
adapter-ExAdapter
public class ExAdapter extends BaseExpandableListAdapter {
private List<ProductCatagory.DataBean> list;
public ExAdapter(List<ProductCatagory.DataBean> list) {
this.list = list;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).getList().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) {
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.group_item, null);
}
TextView group_name = convertView.findViewById(R.id.group_name);
group_name.setText(list.get(groupPosition).getName());
return convertView;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.grid_view, null);
}
//二级item加载一个gridview布局
MyGridView grid = convertView.findViewById(R.id.grid);
final List<ProductCatagory.DataBean.ListBean> list = this.list.get(groupPosition).getList();
grid.setAdapter(new GridAdapter(list));
//为gridview设置条目点击监听
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toast.makeText(parent.getContext(), list.get(position).getPscid() + "==", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(parent.getContext(), ProductsActivity.class);
intent.putExtra("pscid", list.get(position).getPscid());
parent.getContext().startActivity(intent);
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
二级列表,父列表group_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/huo"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/group_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
子列表
<?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:stretchMode="spacingWidthUniform">
<com.bw.movie.diyview.MyGridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"></com.bw.movie.diyview.MyGridView>
</LinearLayout>
自定义的gridview
public class MyGridView extends GridView {
public MyGridView(Context context) {
this(context, null, 0);
}
public MyGridView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//重新计算高度
int newHeight = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, newHeight);
}
}