分類的簡單實現
阿新 • • 發佈:2019-02-07
新增依賴--和 --網路許可權(自己新增)
compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.1.7' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' compile 'com.squareup.retrofit2:retrofit:2.0.2' // Retrofit庫 compile 'com.squareup.okhttp3:okhttp:3.1.2' // Okhttp庫 compile 'com.squareup.retrofit2:converter-gson:2.0.2' compile 'com.facebook.fresco:fresco:0.12.0' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.jakewharton:butterknife:8.8.1'
主佈局檔案
<android.support.v7.widget.RecyclerView android:layout_width="100dp" android:layout_height="match_parent" android:id="@+id/recycler" ></android.support.v7.widget.RecyclerView> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="100dp" android:id="@+id/recycler2" ></android.support.v7.widget.RecyclerView>
建立item.xml檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:textSize="20sp" android:layout_weight="2" android:text="哈哈啊哈哈" /> </LinearLayout>
建立 leftitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Textview"
android:id="@+id/tv2"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler3"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
建立 leftitem2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/simpl"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv3"
/>
</LinearLayout>
建立三個介面卡
MyAdapter類
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private Bean bean;
public MyAdapter(Context context, Bean bean) {
this.context = context;
this.bean = bean;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.item, null);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final ViewHolder viewHolder=(ViewHolder)holder;
if(onItemClickLinster!=null){
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = holder.getLayoutPosition();
onItemClickLinster.OnItemClickLinser(view,pos);
onItemClickLinster.OnItemLongClickLinser(view,pos);
}
});
}
viewHolder.tv.setText(this.bean.getData().get(position).getName());
}
@Override
public int getItemCount() {
return bean.getData().size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tv)
TextView tv;
ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
public interface OnItemClickLinster{
void OnItemClickLinser(View v, int pos);
void OnItemLongClickLinser(View v, int pos);
}
private OnItemClickLinster onItemClickLinster;
public void setOnItemClickLinster(OnItemClickLinster onItemClickLinster) {
this.onItemClickLinster = onItemClickLinster;
}
}
建立
LeftAdpter類
public class LeftAdpter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LeftBean bean;
public LeftAdpter(Context context, LeftBean bean) {
this.context = context;
this.bean = bean;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.leftitem, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.tv2.setText(bean.getData().get(position).getName());
viewHolder.recycler3.setLayoutManager(new GridLayoutManager(context,4));
LeftAdapter2 adapter2=new LeftAdapter2(context,bean.getData().get(position).getList());
viewHolder.recycler3.setAdapter(adapter2);
}
@Override
public int getItemCount() {
return bean.getData().size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tv2)
TextView tv2;
@BindView(R.id.recycler3)
RecyclerView recycler3;
ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
建立
LeftAdapter2類
public class LeftAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<LeftBean.DataBean.ListBean> bean;
public LeftAdapter2(Context context, List<LeftBean.DataBean.ListBean> bean) {
this.context = context;
this.bean = bean;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.leftitem2, null);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ViewHolder viewHolder=(ViewHolder)holder;
viewHolder.simpl.setImageURI(bean.get(position).getIcon());
viewHolder.tv3.setText(bean.get(position).getName());
}
@Override
public int getItemCount() {
return bean.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.simpl)
SimpleDraweeView simpl;
@BindView(R.id.tv3)
TextView tv3;
ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
建立model包
建立getModel介面
public interface getModel {
public void getData(Finish finish);
}
建立
getModel2介面
public interface getModel2 {
public void getData2(LeftFinish leftfinish, String cid);
}
建立
getModelImpl
public class getModelImpl implements getModel{
@Override
public void getData(final Finish finish) {
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(UtilsURL.HOST_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
ServiceAPI serviceAPI = retrofit.create(ServiceAPI.class);
Flowable<Bean> bean = serviceAPI.bean();
bean.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Bean>() {
@Override
public void accept(Bean bean) throws Exception {
finish.onfinish(bean);
}
});
}
}
建立
getModelImpl2類
public class getModelImpl2 implements getModel2{
@Override
public void getData2(final LeftFinish leftfinish, String cid) {
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(UtilsURL.HOST_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
ServiceAPI serviceAPI = retrofit.create(ServiceAPI.class);
Flowable<LeftBean> leftbean = serviceAPI.leftbean(cid);
leftbean.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<LeftBean>() {
@Override
public void accept(LeftBean bean) throws Exception {
leftfinish.finish2(bean);
}
});
}
}
建立p包
建立
getpersenter介面
public interface getpersenter {
public void guanlian();
}
建立
getpersenter2介面
public interface getpersenter2 {
public void guanlian2(String cid);
}
建立
getpersenterImpl類
public class getpersenterImpl implements getpersenter,Finish {
private final getModel m;
getView view;
public getpersenterImpl(getView view) {
this.view = view;
m=new getModelImpl();
}
@Override
public void guanlian() {
m.getData(this);
}
@Override
public void onfinish(Bean bean) {
view.ShowData(bean);
}
}
建立
getpersenterImpl2類
public class getpersenterImpl2 implements getpersenter2,LeftFinish {
private final getModel2 model2;
getView view;
public getpersenterImpl2(getView view) {
this.view = view;
model2=new getModelImpl2();
}
@Override
public void guanlian2(String cid) {
model2.getData2(this,view.cid());
}
@Override
public void finish2(LeftBean bean) {
view.ShowData2(bean);
}
}
建立utils包
建立
App類
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
建立
UtilsURL類
public class UtilsURL {
public static final String HOST_URL="https://www.zhaoapi.cn/";
public static final String TAGS_URL="product/getCatagory";
}
建立
Finish
介面
public interface Finish {
void onfinish(Bean bean);
}
建立
LeftFinish
介面
public interface LeftFinish {
public void finish2(LeftBean bean);
}
建立
ServiceAPI
介面
public interface ServiceAPI {
@GET(UtilsURL.TAGS_URL)
Flowable<Bean> bean();
@GET("product/getProductCatagory")
Flowable<LeftBean> leftbean(@Query("cid") String cid);
}
建立v包
建立
Bean類
import java.util.List;
/**
* Created by lenovo on 2017/12/8.
*/
public class Bean {
/**
* msg :
* code : 0
* data : [{"cid":1,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/shop.png","ishome":1,"name":"京東超市"},{"cid":2,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/qqg.png","ishome":1,"name":"全球購"},{"cid":3,"createtime":"2017-10-10T19:45:11","icon":"http://120.27.23.105/images/category/phone.png","ishome":1,"name":"手機數碼"},{"cid":5,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/man.png","ishome":1,"name":"男裝"},{"cid":6,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girl.png","ishome":1,"name":"女裝"},{"cid":7,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/manshoe.png","ishome":1,"name":"男鞋"},{"cid":8,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girlshoe.png","ishome":1,"name":"女鞋"},{"cid":9,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/shirt.png","ishome":1,"name":"內衣配飾"},{"cid":10,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/bag.png","ishome":1,"name":"箱包手袋"},{"cid":11,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/beauty.png","ishome":1,"name":"美妝個護"},{"cid":12,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/jewel.png","ishome":1,"name":"鐘錶珠寶"},{"cid":13,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/luxury.png","ishome":1,"name":"奢侈品"},{"cid":14,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/computer.png","ishome":1,"name":"電腦辦公"},{"cid":15,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"家用電器"},{"cid":16,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"食品生鮮"},{"cid":17,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"酒水飲料"},{"cid":18,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"母嬰童裝"},{"cid":19,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"玩具樂器"},{"cid":20,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"醫藥保健"}]
*/
private String msg;
private String code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* cid : 1
* createtime : 2017-10-10T19:41:39
* icon : http://120.27.23.105/images/category/shop.png
* ishome : 1
* name : 京東超市
*/
private int cid;
private String createtime;
private String icon;
private int ishome;
private String name;
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public int getIshome() {
return ishome;
}
public void setIshome(int ishome) {
this.ishome = ishome;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
建立
LeftBean
類
import java.util.List;
/**
* Created by lenovo on 2017/12/8.
*/
public class LeftBean {
/**
* msg : 獲取子分類成功
* code : 0
* data : [{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"月餅","pcid":1,"pscid":1},{"icon":"http://120.27.23.105/images/icon.png","name":"堅果炒貨","pcid":1,"pscid":2},{"icon":"http://120.27.23.105/images/icon.png","name":"糖巧","pcid":1,"pscid":3},{"icon":"http://120.27.23.105/images/icon.png","name":"休閒零食","pcid":1,"pscid":4},{"icon":"http://120.27.23.105/images/icon.png","name":"肉乾肉脯","pcid":1,"pscid":5},{"icon":"http://120.27.23.105/images/icon.png","name":"餅乾蛋糕","pcid":1,"pscid":6},{"icon":"http://120.27.23.105/images/icon.png","name":"蜜餞果乾","pcid":1,"pscid":7},{"icon":"http://120.27.23.105/images/icon.png","name":"無糖食品","pcid":1,"pscid":8}],"name":"休閒零食","pcid":"1"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"新鮮水果","pcid":2,"pscid":9},{"icon":"http://120.27.23.105/images/icon.png","name":"海鮮水產","pcid":2,"pscid":10},{"icon":"http://120.27.23.105/images/icon.png","name":"精選肉類","pcid":2,"pscid":11},{"icon":"http://120.27.23.105/images/icon.png","name":"蛋類","pcid":2,"pscid":12},{"icon":"http://120.27.23.105/images/icon.png","name":"新鮮蔬菜","pcid":2,"pscid":13},{"icon":"http://120.27.23.105/images/icon.png","name":"冷凍食品","pcid":2,"pscid":14},{"icon":"http://120.27.23.105/images/icon.png","name":"飲品甜品","pcid":2,"pscid":15},{"icon":"http://120.27.23.105/images/icon.png","name":"大閘蟹","pcid":2,"pscid":16}],"name":"京東生鮮","pcid":"2"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"大米","pcid":3,"pscid":21},{"icon":"http://120.27.23.105/images/icon.png","name":"麵粉","pcid":3,"pscid":22},{"icon":"http://120.27.23.105/images/icon.png","name":"雜糧","pcid":3,"pscid":23},{"icon":"http://120.27.23.105/images/icon.png","name":"食用油","pcid":3,"pscid":24},{"icon":"http://120.27.23.105/images/icon.png","name":"調味品","pcid":3,"pscid":25},{"icon":"http://120.27.23.105/images/icon.png","name":"方便速食","pcid":3,"pscid":26},{"icon":"http://120.27.23.105/images/icon.png","name":"有機食品","pcid":3,"pscid":27}],"name":"糧油調味","pcid":"3"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"飲用水","pcid":4,"pscid":28},{"icon":"http://120.27.23.105/images/icon.png","name":"飲料","pcid":4,"pscid":29},{"icon":"http://120.27.23.105/images/icon.png","name":"牛奶乳品","pcid":4,"pscid":30},{"icon":"http://120.27.23.105/images/icon.png","name":"名茶","pcid":4,"pscid":31},{"icon":"http://120.27.23.105/images/icon.png","name":"蜂蜜","pcid":4,"pscid":32}],"name":"水飲茗茶","pcid":"4"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"白酒","pcid":5,"pscid":33},{"icon":"http://120.27.23.105/images/icon.png","name":"葡萄酒","pcid":5,"pscid":34},{"icon":"http://120.27.23.105/images/icon.png","name":"洋酒","pcid":5,"pscid":35},{"icon":"http://120.27.23.105/images/icon.png","name":"啤酒","pcid":5,"pscid":36},{"icon":"http://120.27.23.105/images/icon.png","name":"黃酒","pcid":5,"pscid":37},{"icon":"http://120.27.23.105/images/icon.png","name":"陳年老酒","pcid":5,"pscid":38}],"name":"中外名酒","pcid":"5"}]
*/
private String msg;
private String code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* cid : 1
* list : [{"icon":"http://120.27.23.105/images/icon.png","name":"月餅","pcid":1,"pscid":1},{"icon":"http://120.27.23.105/images/icon.png","name":"堅果炒貨","pcid":1,"pscid":2},{"icon":"http://120.27.23.105/images/icon.png","name":"糖巧","pcid":1,"pscid":3},{"icon":"http://120.27.23.105/images/icon.png","name":"休閒零食","pcid":1,"pscid":4},{"icon":"http://120.27.23.105/images/icon.png","name":"肉乾肉脯","pcid":1,"pscid":5},{"icon":"http://120.27.23.105/images/icon.png","name":"餅乾蛋糕","pcid":1,"pscid":6},{"icon":"http://120.27.23.105/images/icon.png","name":"蜜餞果乾","pcid":1,"pscid":7},{"icon":"http://120.27.23.105/images/icon.png","name":"無糖食品","pcid":1,"pscid":8}]
* name : 休閒零食
* pcid : 1
*/
private String cid;
private String name;
private String pcid;
private List<ListBean> list;
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPcid() {
return pcid;
}
public void setPcid(String pcid) {
this.pcid = pcid;
}
public List<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class ListBean {
/**
* icon : http://120.27.23.105/images/icon.png
* name : 月餅
* pcid : 1
* pscid : 1
*/
private String icon;
private String name;
private int pcid;
private int pscid;
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPcid() {
return pcid;
}
public void setPcid(int pcid) {
this.pcid = pcid;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
}
}
}
MainActivity主類
public class MainActivity extends AppCompatActivity implements getView {
@BindView(R.id.recycler)
RecyclerView recycler;
@BindView(R.id.recycler2)
RecyclerView recycler2;
private getpersenter p;
private getpersenter2 p2;
private getpersenter2 p21;
private String cid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
p=new getpersenterImpl(this);
p.guanlian();
p21 = new getpersenterImpl2(this);
recycler.setLayoutManager(new LinearLayoutManager(this));
recycler2.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void ShowData(final Bean bean) {
MyAdapter adapter=new MyAdapter(MainActivity.this,bean);
recycler.setAdapter(adapter);
adapter.notifyDataSetChanged();
adapter.setOnItemClickLinster(new MyAdapter.OnItemClickLinster() {
@Override
public void OnItemClickLinser(View v, int pos) {
cid = bean.getData().get(pos).getCid()+"";
p21.guanlian2(cid);
}
@Override
public void OnItemLongClickLinser(View v, int pos) {
}
});
}
@Override
public void ShowData2(LeftBean leftBean) {
LeftAdpter adpter=new LeftAdpter(MainActivity.this,leftBean);
recycler2.setAdapter(adpter);
}
@Override
public String cid() {
return cid;
}
}
在配置檔案中新增
android:name=".utils.App"