仿京东分类

首先是布局
RightAdapter里的布局
布局一
R.layout.group_item

<TextView
        android:id="@+id/group_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

布局二
group_item

<TextView
        android:id="@+id/group_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

布局三
grid_item.xml

<com.bwei.MyJD.customview.MyGridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3"></com.bwei.MyJD.customview.MyGridView>

MyGridView

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);
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwei.MyJD.fragment.ClassifyFragment">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="48dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="21sp"
            android:text="全部分类"/>
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1.5dp"
        android:background="#d4d4d4"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/left_Recycler"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
        <View
            android:layout_width="0.5dp"
            android:layout_height="match_parent"
            android:background="#d4d4d4"/>
        <ExpandableListView
            android:id="@+id/right_Recycler"
            android:groupIndicator="@null"
            android:layout_width="0dp"
            android:layout_weight="8"
            android:layout_height="match_parent"></ExpandableListView>

    </LinearLayout>

</LinearLayout>

一、连接

public interface FenLeiApi {

    @POST("product/getCatagory")
    Observable<FenLeiEntity> FenLeiData();

}
public interface RightApi {

    @POST("product/getProductCatagory")
    @FormUrlEncoded
    Observable<Rightentity> RightData(@FieldMap HashMap<String,String> map);
}

二、分类右契约

public interface Rightcontract {

    //p
    abstract class RightPersenter extends BasePresenter<RightModel,RightVIew>{
        @Override
        public RightModel getmModel() {
            return new com.bwei.MyJD.mvp.model.RightModel();
        }
        public abstract void rightdata(HashMap<String,String> map);
    }

    //m
    interface RightModel extends IBaseModel{
        Observable<Rightentity> rightdata(HashMap<String,String> map);
    }
    //v
    interface RightVIew extends IBaseView{
        void rightsuccess(Rightentity rightentity);
        void fail(String msg);
    }

}

三、右model

public class RightModel implements Rightcontract.RightModel {
    @Override
    public Observable<Rightentity> rightdata(HashMap<String, String> map) {
        Observable<Rightentity> on = RetrofitUtils.getInstance().createApi(Contants.BASE_RELEASE_URL, RightApi.class)
                .RightData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        return on;
    }
}

四、右p层

public class RightPersenter extends Rightcontract.RightPersenter{
    @Override
    public void rightdata(HashMap<String, String> map) {
        mModel.rightdata(map).subscribe(new Consumer<Rightentity>() {
            @Override
            public void accept(Rightentity rightentity) throws Exception {
                mView.rightsuccess(rightentity);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        });
    }
}

五、分类fragment

public class ClassifyFragment extends BaseMvpFragment<Rightcontract.RightModel,Rightcontract.RightPersenter> implements Rightcontract.RightVIew {

    @BindView(R.id.left_Recycler)
    RecyclerView left_Recycler;
    //一种用于垂直滚动展示两级列表的视图,
    // 和 ListView 的不同之处就是它可以展示两级列表,分组可以单独展开显示子选项。
    @BindView(R.id.right_Recycler)
    ExpandableListView right_Recycler;
    private int cid=1;


    //布局
    @Override
    protected int bindLayoutId() {
        return R.layout.fragment_classify;
    }

    @Override
    public BasePresenter initPresenter() {
        return new RightPersenter();
    }

    //获取数据
    @Override
    protected void initData() {
        super.initData();
        //默认展示右边数据de cid
        HashMap<String, String> map = new HashMap<>();
        map.put("cid",cid+"");
        presenter.rightdata(map);

        RetrofitUtils.getInstance().createApi(Contants.BASE_RELEASE_URL, FenLeiApi.class).FenLeiData()
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<FenLeiEntity>() {
                    @Override
                    public void accept(FenLeiEntity fenLeiEntity) throws Exception {
                        //获取左边数据
                        final List<FenLeiEntity.DataBean> data = fenLeiEntity.getData();
                        //设置左边条目
                        left_Recycler.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
                        LeftAdapter leftAdapter = new LeftAdapter(R.layout.left_item, data);
                        left_Recycler.setAdapter(leftAdapter);
                        //添加item分割线
                        left_Recycler.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
                        //設置左边子条目点击
                        leftAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                int cid = data.get(position).getCid();
                                HashMap<String, String> map = new HashMap<>();
                                map.put("cid",cid+"");
                                presenter.rightdata(map);
                            }
                        });
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    //获取右边数据
    @Override
    public void rightsuccess(Rightentity rightentity) {

        if (rightentity.getData().size()==0){
            Toast.makeText(getActivity(), "该分类暂时没有商品", Toast.LENGTH_SHORT).show();
        }else {
            List<Rightentity.DataBean> data = rightentity.getData();
            RightAdapter rightAdapter = new RightAdapter(data);
            right_Recycler.setAdapter(rightAdapter);
            //默认展开所有一级item
            for (int i = 0; i < data.size(); i++) {
                right_Recycler.expandGroup(i);
            }
        }
    }

    @Override
    public void fail(String msg) {
        Toast.makeText(getActivity(), "获得数据失败", Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void initView() {

    }
}

六、LeftAdapter

public class LeftAdapter extends BaseQuickAdapter<FenLeiEntity.DataBean,BaseViewHolder> {

    public LeftAdapter(int layoutResId, @Nullable List<FenLeiEntity.DataBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FenLeiEntity.DataBean item) {
        helper.setText(R.id.fenlei_biaoti,item.getName());
    }
}

七、RightAdapter

public class RightAdapter extends BaseExpandableListAdapter {

    private List<Rightentity.DataBean> list;

    public RightAdapter(List<Rightentity.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, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView=View.inflate(parent.getContext(),R.layout.grid_item,null);
        }
        //二级列表
        MyGridView grid = convertView.findViewById(R.id.grid);
        final List<Rightentity.DataBean.ListBean> list = this.list.get(groupPosition).getList();
        grid.setAdapter(new GridAdapter(list));
        //设置点击事件
        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();
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值