分類第二個版本
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>