仿京东 分类页面 RecyclerView+ExpandableListView

分类页面布局

<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/classify_left"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
    <ExpandableListView
        android:id="@+id/classify_right"
        android:layout_width="0dp"
        android:layout_weight="8"
        android:layout_height="match_parent"></ExpandableListView>
</LinearLayout>

分类左侧条目布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/left_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:padding="10dp"
        android:text="xxxx"
        android:textSize="20dp" />
</RelativeLayout>

分类右侧 二级列表布局  父条目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <TextView
        android:id="@+id/classify_group_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:padding="20dp"/>

</LinearLayout>

分类右侧 二级列表布局 子条目

<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/classify_child_rlv"
    android:layout_width="match_parent"
    android:background="#ffffff"
    android:layout_marginBottom="10dp"
    android:padding="10dp"
    android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>

子条目内嵌套RecyclerView 条目

<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/classify_child_rlv"
    android:layout_width="match_parent"
    android:background="#ffffff"
    android:layout_marginBottom="10dp"
    android:padding="10dp"
    android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>

view 接口

public interface IClassifyView extends IBaseView {
    void successLift(Fenglei fenglei);
    void successRigth(List<Zifenglei.DataBean> groupList, List<List<Zifenglei.DataBean.ListBean>> childList);
}

model

public class ClassifyModel extends BaseModel {
    private RetrofitUtils retrofitUtils;

    public void getFenglei(final IFenglei iFenglei) {
        retrofitUtils = RetrofitUtils.getInstance();
        retrofitUtils.getApi().fengLei()
                .subscribeOn(io.reactivex.schedulers.Schedulers.io())
                .observeOn(io.reactivex.android.schedulers.AndroidSchedulers.mainThread())
                .subscribe(new Observer<Fenglei>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.e("onSubscribe", d + "");
                    }

                    @Override
                    public void onNext(Fenglei fenglei) {
                        iFenglei.success(fenglei);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("onErrer", e + "");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void getZifenglei(String cid, final IFenglei iFenglei){
        retrofitUtils=RetrofitUtils.getInstance();
        HashMap<String,String> params=new HashMap<>();
        params.put("cid",cid);
        retrofitUtils.getApi().zifenglei(params)
                .subscribeOn(io.reactivex.schedulers.Schedulers.io())
                .observeOn(io.reactivex.android.schedulers.AndroidSchedulers.mainThread())
                .subscribe(new Observer<Zifenglei>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.e("zonSubscribe", d + "");
                    }

                    @Override
                    public void onNext(Zifenglei zifenglei) {
                        iFenglei.successRigth(zifenglei);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("zonErrer", e + "");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public interface IFenglei {
        void success(Fenglei fenglei);
        void successRigth(Zifenglei zifenglei);
    }
}
 

presenter

public class ClassifyPresenter extends BasePresenter<ClassifyModel,IClassifyView>{
    public void leftData(){
        model.getFenglei(new ClassifyModel.IFenglei() {
            @Override
            public void success(Fenglei fenglei) {
                view.successLift(fenglei);
            }

            @Override
            public void successRigth(Zifenglei zifenglei) {

            }
        });
    }
        public void rightData(String cid){
        model.getZifenglei(cid,new ClassifyModel.IFenglei(){
            @Override
            public void success(Fenglei fenglei) {

            }

            @Override
            public void successRigth(Zifenglei zifenglei) {
                List<Zifenglei.DataBean> data = zifenglei.getData();
                ArrayList<List<Zifenglei.DataBean.ListBean>> childList = new ArrayList<>();
                for(int i=0;i<data.size();i++){
                    List<Zifenglei.DataBean.ListBean> list = data.get(i).getList();
                    childList.add(list);
                }
                view.successRigth(data,childList);
            }

        });
    }

}
 

activity

public class Classify extends BaseFragment<ClassifyPresenter> implements IClassifyView {

    private ExpandableListView right;
    private RecyclerView left;

    @Override
    protected void initData() {
        presenter.leftData();
        presenter.rightData("1");
    }

    @Override
    protected BaseModel initModel() {
        return new ClassifyModel();
    }

    @Override
    protected ClassifyPresenter initPresenter() {
        return new ClassifyPresenter();
    }

    @Override
    protected void initView(View view) {
        right = view.findViewById(R.id.classify_right);
        left=view.findViewById(R.id.classify_left);


    }

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

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void serverFail(String msg) {

    }

    @Override
    public void successLift(Fenglei fenglei) {
        LiftAdapter liftAdapter = new LiftAdapter(fenglei.getData(), getContext());
        left.setLayoutManager(new GridLayoutManager(getContext(),1, LinearLayoutManager.VERTICAL,false));
        left.setAdapter(liftAdapter);
        liftAdapter.setOnItemClickListener(new LiftAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, String cid) {
                Toast.makeText(getContext(), ""+cid, Toast.LENGTH_SHORT).show();
                presenter.rightData(cid);
            }
        });
    }

    @Override
    public void successRigth(List<Zifenglei.DataBean> groupList, List<List<Zifenglei.DataBean.ListBean>> childList) {

        RightAdapter rightAdapter = new RightAdapter(groupList,childList,getActivity());
        right.setAdapter(rightAdapter);
        right.setGroupIndicator(null);
        //默认全部展开
        for(int i=0;i<groupList.size();i++){
            right.expandGroup(i);
        }

    }


}
 

adatpter

左侧条目

public class LiftAdapter extends RecyclerView.Adapter<LiftAdapter.MyHolderView> {
    private List<Fenglei.DataBean> list;
    private Context context;
    OnItemClickListener onItemClickListener;

    public LiftAdapter(List<Fenglei.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public MyHolderView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_left,parent,false);
        return new MyHolderView(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolderView holder, int position) {
        if(list!=null){
            holder.name.setText(list.get(position).getName());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getAdapterPosition();
                    int cid = list.get(pos).getCid();
                    onItemClickListener.onClick(v,cid+"");
                }
            });
        }
    }
    public interface OnItemClickListener{
        void onClick(View view,String cid);
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolderView extends RecyclerView.ViewHolder{

        private final TextView name;

        public MyHolderView(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.left_name);
        }
    }
}
 

右侧条目

public class RightAdapter extends BaseExpandableListAdapter{
    private List<Zifenglei.DataBean> shangList;
    private List<List<Zifenglei.DataBean.ListBean>> childList;
    private Context context;

    public RightAdapter(List<Zifenglei.DataBean> shangList, List<List<Zifenglei.DataBean.ListBean>> childList, Context context) {
        this.shangList = shangList;
        this.childList = childList;
        this.context = context;
    }


    @Override
    public int getGroupCount() {
        return shangList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return shangList.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 false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder holder;
        if(convertView==null){
            holder=new GroupViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item_classify_grooup,null);
            holder.classify_group_tv=convertView.findViewById(R.id.classify_group_tv);
            convertView.setTag(holder);
        }else{
            holder=(GroupViewHolder)convertView.getTag();
        }
        holder.classify_group_tv.setText(shangList.get(groupPosition).getName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        if(convertView==null){
            holder= new ChildViewHolder();
            convertView=LayoutInflater.from(context).inflate(R.layout.item_classify_child,null);
            holder.classify_child_rlv=convertView.findViewById(R.id.classify_child_rlv);
            convertView.setTag(holder);
        }else{
            holder=(ChildViewHolder)convertView.getTag();
        }
        ClassifyChildViewAdapter classifyChildViewAdapter = new ClassifyChildViewAdapter(childList.get(groupPosition), context);
        holder.classify_child_rlv.setLayoutManager(new GridLayoutManager(context,3, LinearLayoutManager.VERTICAL,false));
        holder.classify_child_rlv.setAdapter(classifyChildViewAdapter);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
    class GroupViewHolder{
        TextView classify_group_tv;
    }
    class ChildViewHolder{
        RecyclerView classify_child_rlv;
    }
}
 

右侧子条目

public class ClassifyChildViewAdapter extends RecyclerView.Adapter<ClassifyChildViewAdapter.MyViweHolder> {
    private List<Zifenglei.DataBean.ListBean> list;
    private Context context;

    public ClassifyChildViewAdapter(List<Zifenglei.DataBean.ListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViweHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_classify_child_rlv,parent,false);
        return new MyViweHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViweHolder holder, int position) {
        holder.sdv.setImageURI(list.get(position).getIcon());
        holder.tv.setText(list.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViweHolder extends RecyclerView.ViewHolder {

        private final SimpleDraweeView sdv;
        private final TextView tv;

        public MyViweHolder(View itemView) {
            super(itemView);
            sdv = itemView.findViewById(R.id.sdv);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}



Base类在另一篇文章之中,需要的可以去找一下  mvp库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值