Http包
//Apiservice接口
package com.example.com.jingdongfenlei.http; import com.example.com.jingdongfenlei.entity.LeftCategory; import com.example.com.jingdongfenlei.entity.MessageBean; import com.example.com.jingdongfenlei.entity.RightCategory; import java.util.HashMap; import java.util.List; import io.reactivex.Flowable; import io.reactivex.Observable; import retrofit2.http.GET; import retrofit2.http.Query; /** * Created by 丶未央 on 2018/4/20. */ public interface ApiService { @GET("product/getCatagory") Observable<MessageBean<List<LeftCategory>>> getFirstCategory(); @GET("product/getProductCatagory") Flowable<MessageBean<List<RightCategory>>> getSecondCategory(@Query("cid") String cid); }
ApiService类
package com.example.com.jingdongfenlei.http; /** * Created by 丶未央 on 2018/4/20. */ public class ServiceUrl { public static final String BASE_URL="https://www.zhaoapi.cn/"; }
//inter包
Ipresenter类
package com.example.com.jingdongfenlei.inter; /** * Created by 丶未央 on 2018/4/20. */ public interface IPresenter<T> { void onReceived(T t); void onError(Throwable t); }
IView类
package com.example.com.jingdongfenlei.inter; /** * Created by 丶未央 on 2018/4/20. */ public interface IView<T> { void onSuccess(T t); void onFailed(Throwable t);
}
OnRecyclerViewItemClickListener类
public interface OnRecyclerViewItemClickListener { void onItemClick(int position); }
model包
LeftModel类
package com.example.com.jingdongfenlei.model; import android.content.Context; import android.util.Log; import com.example.com.jingdongfenlei.entity.LeftCategory; import com.example.com.jingdongfenlei.entity.MessageBean; import com.example.com.jingdongfenlei.inter.IPresenter; import com.example.com.jingdongfenlei.utils.HttpUtils; import java.util.List; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; import io.reactivex.subscribers.DisposableSubscriber; /** * Created by 丶未央 on 2018/4/20. */ public class LeftModel { IPresenter presenter; public LeftModel( IPresenter presenter){ this.presenter=presenter; } public void getData() { HttpUtils.getInstance().getService() .getFirstCategory() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<MessageBean<List<LeftCategory>>>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(MessageBean<List<LeftCategory>> listMessageBean) { presenter.onReceived(listMessageBean); Log.d("--", "onNext: "+listMessageBean.getData().size()); } @Override public void onError(Throwable e) { Log.d("--", "onError: ."+e.getMessage()); } @Override public void onComplete() { } }); } }
RightModel类
package com.example.com.jingdongfenlei.model; import android.util.Log; import com.example.com.jingdongfenlei.entity.MessageBean; import com.example.com.jingdongfenlei.entity.RightCategory; import com.example.com.jingdongfenlei.inter.IPresenter; import com.example.com.jingdongfenlei.utils.HttpUtils; import java.util.List; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import io.reactivex.subscribers.DisposableSubscriber; /** * Created by eric on 2018/4/18. */ public class RightModel { private IPresenter presenter; public RightModel(IPresenter presenter) { this.presenter = presenter; } public void getData(String cid) { HttpUtils.getInstance().getService() .getSecondCategory(cid) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisposableSubscriber<MessageBean<List<RightCategory>>>() { @Override public void onNext(MessageBean<List<RightCategory>> listMessageBean) { Log.d("--", "onNext: "+listMessageBean.getData().size()); presenter.onReceived(listMessageBean); } @Override public void onError(Throwable t) { Log.d("--", "onError: "+t.getMessage()); presenter.onError(t); } @Override public void onComplete() { } }); } }
Presenteer包
//LeftPresenter
package com.example.com.jingdongfenlei.presenter; import com.example.com.jingdongfenlei.entity.LeftCategory; import com.example.com.jingdongfenlei.entity.MessageBean; import com.example.com.jingdongfenlei.inter.IPresenter; import com.example.com.jingdongfenlei.inter.IView; import com.example.com.jingdongfenlei.model.LeftModel; import java.util.List; /** * Created by 丶未央 on 2018/4/20. */ public class LeftPresenter implements IPresenter<MessageBean<List<LeftCategory>>> { private IView iv; public void attachView(IView iv) { this.iv = iv; } public void detachView() { if (iv != null) { iv = null; } } public void getData() { LeftModel model = new LeftModel(this); model.getData(); } @Override public void onReceived(MessageBean<List<LeftCategory>> listMessageBean) { iv.onSuccess(listMessageBean); } @Override public void onError(Throwable t) { iv.onFailed(t); } }
utils包
http类
package com.example.com.jingdongfenlei.utils; import com.example.com.jingdongfenlei.http.ApiService; import com.example.com.jingdongfenlei.http.ServiceUrl; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; import static okhttp3.internal.Internal.instance; /** * Created by 丶未央 on 2018/4/20. */ public class HttpUtils { private static volatile HttpUtils instance; private Retrofit retrofit; private HttpUtils(){ //日志拦截器 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); retrofit = new Retrofit.Builder() .baseUrl(ServiceUrl.BASE_URL) //.client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } public static HttpUtils getInstance() { if (instance == null) { synchronized (HttpUtils.class) { if (null == instance) { instance = new HttpUtils(); } } } return instance; } public ApiService getService() { return retrofit.create(ApiService.class); } }
View包
LeftFragment类
package com.example.com.jingdongfenlei.view; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.example.com.jingdongfenlei.R; import com.example.com.jingdongfenlei.adapter.LeftListAdapter; import com.example.com.jingdongfenlei.entity.LeftCategory; import com.example.com.jingdongfenlei.entity.MessageBean; import com.example.com.jingdongfenlei.inter.IView; import com.example.com.jingdongfenlei.inter.OnRecyclerViewItemClickListener; import com.example.com.jingdongfenlei.presenter.LeftPresenter; import org.greenrobot.eventbus.EventBus; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; /** * Created by 丶未央 on 2018/4/20. */ public class LeftFragment extends Fragment implements IView<MessageBean<List<LeftCategory>>> { private static final String TAG = "LeftFragment"; /** * 左侧列表的视图 */ @BindView(R.id.rv_left) RecyclerView rvLeft; Unbinder unbinder; private Context mActivity; private LeftListAdapter adapter; private List<LeftCategory> list; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivity = getActivity(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_left, null); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); list = new ArrayList<>(); adapter = new LeftListAdapter(mActivity, list); adapter.setListener(new OnRecyclerViewItemClickListener() { @Override public void onItemClick(int position) { int cid = list.get(position).getCid(); EventBus.getDefault().post(cid+""); } }); rvLeft.setLayoutManager(new LinearLayoutManager(mActivity)); rvLeft.setAdapter(adapter); LeftPresenter presenter = new LeftPresenter(); presenter.attachView(this); presenter.getData(); //rvLeft.setAdapter(adapter); } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } @Override public void onSuccess(MessageBean<List<LeftCategory>> listMessageBean) { if (listMessageBean != null) { List<LeftCategory> data = listMessageBean.getData(); if (data != null) { list.addAll(data); adapter.notifyDataSetChanged(); } } } @Override public void onFailed(Throwable t) { Toast.makeText(mActivity, "数据获取失败:" + t.getMessage(), Toast.LENGTH_SHORT).show(); }
RightFragment类
package com.example.com.jingdongfenlei.view; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.example.com.jingdongfenlei.R; import com.example.com.jingdongfenlei.adapter.RightListAdapter; import com.example.com.jingdongfenlei.entity.MessageBean; import com.example.com.jingdongfenlei.entity.RightCategory; import com.example.com.jingdongfenlei.inter.IView; import com.example.com.jingdongfenlei.presenter.RightPresenter; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; /** * Created by 丶未央 on 2018/4/21. */ public class RightFragment extends Fragment implements IView<MessageBean<List<RightCategory>>> { /** * 左侧列表的视图 */ @BindView(R.id.rv_right) RecyclerView rvRight; Unbinder unbinder; private Context mActivity; private RightListAdapter adapter; private List<RightCategory> list; private RightPresenter presenter; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivity = getActivity(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_right, null); unbinder = ButterKnife.bind(this, view); EventBus.getDefault().register(this); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); list = new ArrayList<>(); adapter = new RightListAdapter(mActivity, list); rvRight.setLayoutManager(new LinearLayoutManager(mActivity)); rvRight.setAdapter(adapter); presenter = new RightPresenter(); presenter.attachView(this); presenter.getData("1"); } @Subscribe(threadMode = ThreadMode.MAIN) public void receiveData(String cid) { presenter.getData(cid); } @Override public void onDestroyView() { super.onDestroyView(); EventBus.getDefault().unregister(this); unbinder.unbind(); } @Override public void onSuccess(MessageBean<List<RightCategory>> listMessageBean) { if (listMessageBean != null) { List<RightCategory> data = listMessageBean.getData(); Log.d("--", "onSuccess: "+data.size()); if (data != null) { list.clear(); list.addAll(data); adapter.notifyDataSetChanged(); } } } @Override public void onFailed(Throwable t) { Toast.makeText(mActivity, "数据获取失败:" + t.getMessage(), Toast.LENGTH_SHORT).show(); } }
MainActivity类
package com.example.com.jingdongfenlei; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.FrameLayout; public class MainActivity extends AppCompatActivity { /*@butterknife.BindView(R.id.fen_lei_you) FrameLayout fenLeiYou; @butterknife.BindView(R.id.fen_lei_zuo) FrameLayout fenLeiZuo; */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); butterknife.ButterKnife.bind(this); } }
adapter包
package com.example.com.jingdongfenlei.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.com.jingdongfenlei.R; import com.example.com.jingdongfenlei.entity.LeftCategory; import com.example.com.jingdongfenlei.inter.OnRecyclerViewItemClickListener; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; /** * Created by 丶未央 on 2018/4/21. */ public class LeftListAdapter extends RecyclerView.Adapter<LeftListAdapter.ViewHolder> { private Context context; private List<LeftCategory> list; private OnRecyclerViewItemClickListener listener; public void setListener(OnRecyclerViewItemClickListener listener) { this.listener = listener; } public LeftListAdapter(Context context, List<LeftCategory> list) { this.context = context; this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item_left_list, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { Glide.with(context).load(list.get(position).getIcon()).into(holder.imgLogo); holder.txtTitle.setText(list.get(position).getName()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.onItemClick(position); } }); } @Override public int getItemCount() { if (list != null) { return list.size(); } return 0; } class ViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.img_left_item) ImageView imgLogo; @BindView(R.id.txt_left_item) TextView txtTitle; public ViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } }
RightListAdapter类
package com.example.com.jingdongfenlei.adapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.com.jingdongfenlei.R; import com.example.com.jingdongfenlei.entity.RightCategory; import com.example.com.jingdongfenlei.entity.SecondProduct; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; /** * Created by 丶未央 on 2018/4/21. */ public class RightListAdapter extends RecyclerView.Adapter<RightListAdapter.ViewHolder> { private Context context; private List<RightCategory> list; public RightListAdapter(Context context, List<RightCategory> list) { this.context = context; this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item_right_first, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.txtTitle.setText(list.get(position).getName()); SecondAdapter adapter = new SecondAdapter(context, list.get(position).getList()); holder.rvSecond.setLayoutManager(new GridLayoutManager(context, 3)); holder.rvSecond.setAdapter(adapter); // holder.rvSecond; } @Override public int getItemCount() { if (list != null) { return list.size(); } return 0; } class ViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.txt_right_first) TextView txtTitle; @BindView(R.id.rv_second_right) RecyclerView rvSecond; public ViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } // 第二级的RecyclerView适配器 class SecondAdapter extends RecyclerView.Adapter<SecondAdapter.SecondViewHolder> { private Context context; private List<RightCategory.ListBean> products; public SecondAdapter(Context context, List<RightCategory.ListBean> products) { this.context = context; this.products=products; } @NonNull @Override public SecondViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item_right_second, null); SecondViewHolder holder = new SecondViewHolder(view); return holder; } @Override public void onBindViewHolder(@NonNull SecondViewHolder holder, int position) { Glide.with(context).load(products.get(position).getIcon()).into(holder.imgLogo); holder.txtSecondTitle.setText(products.get(position).getName()); } @Override public int getItemCount() { if (products != null) { return products.size(); } return 0; } class SecondViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.img_right_second_item) ImageView imgLogo; @BindView(R.id.txt_right_second_item) TextView txtSecondTitle; public SecondViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } }
布局
主布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.com.jingdongfenlei.MainActivity"> <fragment android:name="com.example.com.jingdongfenlei.view.LeftFragment" android:id="@+id/fen_lei_you" android:layout_width="0dp" android:layout_weight="3" android:layout_height="match_parent"> </fragment> <fragment android:name="com.example.com.jingdongfenlei.view.RightFragment" android:id="@+id/fen_lei_zuo" android:layout_width="0dp" android:layout_weight="7" android:layout_height="match_parent"> </fragment> </LinearLayout>
fragment_left
<?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:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_left" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </LinearLayout>
fragment_right
<?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:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_right" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </LinearLayout>
item_left_list
<?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:orientation="horizontal"> <ImageView android:id="@+id/img_left_item" android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/txt_left_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" /> </LinearLayout>
item_right_first
<?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:orientation="vertical"> <TextView android:id="@+id/txt_right_first" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv_second_right" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </LinearLayout>
item_right_second
<?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:orientation="horizontal"> <ImageView android:id="@+id/img_right_second_item" android:layout_width="45dp" android:layout_height="45dp" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/txt_right_second_item" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" /> </LinearLayout>
依赖
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.0.+' implementation 'com.android.support.constraint:constraint-layout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' compile 'com.android.support:recyclerview-v7:26.0.+' compile 'com.facebook.fresco:fresco:0.12.0' // Retrofit库 implementation 'com.squareup.retrofit2:retrofit:2.4.0' compile 'com.squareup.retrofit2:converter-gson:2.4.0' compile 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.1.12' // Okhttp库 compile 'com.squareup.okhttp3:okhttp:3.1.2' // rxjava+rxandroid+retrofit2+okhttp implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.1.12' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.google.code.gson:gson:2.2.4' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'org.greenrobot:eventbus:3.0.0' compile 'com.github.bumptech.glide:glide:3.7.0' }