分类第二个版本

1.分类整体的布局

<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="horizontal">

    <android.support.v7.widget.RecyclerView
        android:layout_width="130dp"
        android:layout_height="match_parent"
        android:id="@+id/rece_fen"
        />

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/expan_view"
        />

</LinearLayout>

2.分类接口文档

    //左侧接口
    @GET("product/getCatagory")
    Observable<NewsZuo> getFenzuo();

    //右侧
    @GET("product/getProductCatagory")
    Observable<NewsYou> getFenyou(@Query("cid")int id);

3.分类的C层

public interface FenleiContract {
    //对于经常使用的关于UI的方法可以定义到IView中,如显示隐藏进度条,和显示文字消息
    interface View extends IView {

        //左侧
        void showData(NewsZuo newsZuo);

        //右侧
        void showData1(NewsYou newsYou);
    }

    //Model层定义接口,外部只需关心Model返回的数据,无需关心内部细节,即是否使用缓存
    interface Model extends IModel {

        //左侧
        Observable<NewsZuo> responseMsg();

        Observable<NewsYou> responseMsg1(int id);
    }
}

4.分类的M层

@Override
    public Observable<NewsZuo> responseMsg() {
        MService mService = mRepositoryManager.obtainRetrofitService(MService.class);
        Observable<NewsZuo> fenzuo = mService.getFenzuo();
        return fenzuo;
    }

    @Override
    public Observable<NewsYou> responseMsg1(int id) {
        MService mService = mRepositoryManager.obtainRetrofitService(MService.class);
        Observable<NewsYou> fenyou = mService.getFenyou(id);
        return fenyou;
    }

 

5.分类的P层

@SuppressLint("CheckResult")
    public void getfenzuo()
    {
        Observable<NewsZuo> newsZuoObservable = mModel.responseMsg();
        newsZuoObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<NewsZuo>() {
            @Override
            public void accept(NewsZuo newsZuo) throws Exception {
                mRootView.showData(newsZuo);
            }
        });
    }

    @SuppressLint("CheckResult")
    public void getfenyou(int id)
    {
        Observable<NewsYou> newsYouObservable = mModel.responseMsg1(id);
        newsYouObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<NewsYou>() {
            @Override
            public void accept(NewsYou newsYou) throws Exception {
                mRootView.showData1(newsYou);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        });
    }

 

6.分类主页面

public class FenleiFragment extends BaseFragment<FenleiPresenter> implements FenleiContract.View {

    @BindView(R.id.rece_fen)
    RecyclerView receFen;
    @BindView(R.id.expan_view)
    ExpandableListView expanView;
    Unbinder unbinder;

    public static FenleiFragment newInstance() {
        FenleiFragment fragment = new FenleiFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerFenleiComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .fenleiModule(new FenleiModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_fenlei, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    /**
     * 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
     * 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * <p>
     * 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
     * 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
     * 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
     * <p>
     * Example usage:
     * <pre>
     * public void setData(@Nullable Object data) {
     *     if (data != null && data instanceof Message) {
     *         switch (((Message) data).what) {
     *             case 0:
     *                 loadData(((Message) data).arg1);
     *                 break;
     *             case 1:
     *                 refreshUI();
     *                 break;
     *             default:
     *                 //do something
     *                 break;
     *         }
     *     }
     * }
     *
     * // call setData(Object):
     * Message data = new Message();
     * data.what = 0;
     * data.arg1 = 1;
     * fragment.setData(data);
     * </pre>
     *
     * @param data 当不需要参数时 {@code data} 可以为 {@code null}
     */
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void showData(NewsZuo newsZuo) {
        List<NewsZuo.DataBean> data = newsZuo.getData();
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        receFen.setLayoutManager(manager);
        MyFZuoAdapter myFZuoAdapter = new MyFZuoAdapter(getActivity(), data);
        receFen.setAdapter(myFZuoAdapter);
        myFZuoAdapter.setOnItemClickListener(new MyFZuoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                int cid = newsZuo.getData().get(position).getCid();
                mPresenter.getfenyou(cid);
            }
        });
    }

    @Override
    public void showData1(NewsYou newsYou) {

        List<NewsYou.DataBean> data = newsYou.getData();
        MyFyouAdapter myFyouAdapter = new MyFyouAdapter(getActivity(), newsYou);
        expanView.setAdapter(myFyouAdapter);

        int count = expanView.getCount();
        for (int i = 0; i < count; i++) {
            expanView.expandGroup(i);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        mPresenter.getfenzuo();
        mPresenter.getfenyou(1);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
 

7.左边适配器

public class MyFZuoAdapter extends RecyclerView.Adapter<MyFZuoAdapter.LieZuoViewHolder> {

    private Context context;
    private List<NewsZuo.DataBean> listzuo;

    public MyFZuoAdapter(Context context, List<NewsZuo.DataBean> listzuo) {
        this.context = context;
        this.listzuo = listzuo;
    }

    private OnItemClickListener   mOnItemClickListener;

    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    @NonNull
    @Override
    public LieZuoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.fenzuo_item, null);
        LieZuoViewHolder lieZuoViewHolder = new LieZuoViewHolder(inflate);
        return lieZuoViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull LieZuoViewHolder holder, int position) {

        holder.text_zuo.setText(listzuo.get(position).getName());
        if(mOnItemClickListener !=null)
        {
            holder.text_zuo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }

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

    class LieZuoViewHolder extends RecyclerView.ViewHolder
    {


        private final TextView text_zuo;

        public LieZuoViewHolder(View itemView) {
            super(itemView);
            text_zuo = (TextView)itemView.findViewById(R.id.text_zuo);
        }
    }
}
 

8.左边布局

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="XXXXX"
        android:id="@+id/text_zuo"
        />

</LinearLayout>
 

9.分类右边适配器

public class MyFyouAdapter extends BaseExpandableListAdapter {

    private Context context;
    private NewsYou newsYou;

    public MyFyouAdapter(Context context, NewsYou newsYou) {
        this.context = context;
        this.newsYou = newsYou;
    }

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

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

    @Override
    public Object getGroup(int groupPosition) {
        return newsYou.getData().get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return newsYou.getData().get(groupPosition).getList().get(groupPosition);
    }

    @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) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.youtou_item, null);
        TextView text_tou = inflate.findViewById(R.id.text_tou);
        text_tou.setText(newsYou.getData().get(groupPosition).getName());
        return inflate;
    }

    @Override
    public View getChildView(int i, int i1, boolean isLastChild, View convertView, ViewGroup parent) {
        View inflate1 = LayoutInflater.from(context).inflate(R.layout.younei_item, null);
        RecyclerView rece_nei = inflate1.findViewById(R.id.rece_nei);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        rece_nei.setLayoutManager(gridLayoutManager);
        MyNeiAdapter myNeiAdapter = new MyNeiAdapter(context, (ArrayList<NewsYou.DataBean>) newsYou.getData(), i, i1);
        rece_nei.setAdapter(myNeiAdapter);
        return inflate1;
    }

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

10.右边头部标签

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="XXXXX"
        android:id="@+id/text_tou"
        />

</LinearLayout>

 

11.右边内部布局

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rece_nei"
        />

</LinearLayout>

 

12.右边内部适配器

public class MyNeiAdapter extends RecyclerView.Adapter<MyNeiAdapter.NeiViewHolder> {

    private Context context;
    private ArrayList<NewsYou.DataBean> listBeans;
    private int i,i1;

    public MyNeiAdapter(Context context, ArrayList<NewsYou.DataBean> listBeans, int i, int i1) {
        this.context = context;
        this.listBeans = listBeans;
        this.i = i;
        this.i1 = i1;
    }

    @NonNull
    @Override
    public NeiViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.include, null);
        NeiViewHolder neiViewHolder = new NeiViewHolder(inflate);
        return neiViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull NeiViewHolder holder, int position) {

        holder.text_name.setText(listBeans.get(position).getName());
        String[] splitImages = listBeans.get(position).getList().get(position).getIcon().split("\\|");
        Uri parse = Uri.parse(splitImages[0]);
        holder.simp_nei.setImageURI(parse);
    }

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

    class NeiViewHolder extends RecyclerView.ViewHolder
    {

        private final SimpleDraweeView simp_nei;
        private final TextView text_name;

        public NeiViewHolder(View itemView) {
            super(itemView);
            simp_nei = (SimpleDraweeView)itemView.findViewById(R.id.simp_nei);
            text_name = (TextView)itemView.findViewById(R.id.text_name);
        }
    }
}
 

13.内部适配器布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <com.facebook.drawee.view.SimpleDraweeView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/simp_nei"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="xxx"
        android:id="@+id/text_name"
        />

</LinearLayout>

 

 

 

分类图片跳转在网格适配器里面写,具体操作如下:

@Override
public void onBindViewHolder(@NonNull YouneiViewHolder holder, int position) {

    holder.text_nei.setText(newsYou.getData().get(i).getName());
    String icon1 = newsYou.getData().get(i).getList().get(0).getIcon();
    Uri parse = Uri.parse(icon1);
    holder.simp_nei.setImageURI(parse);
   holder.simp_nei.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           String cid = newsYou.getData().get(position).getPcid();
           Intent intent = new Intent(context,ZhanXiangActivity.class);
           intent.putExtra("pid",cid);
           context.startActivity(intent);
       }
   });

 

如果图片不出来 自己在固定接口

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值