//bean名称
子类GoodsList
左侧SortBean
右侧SortRithtChildBean
//M层、
public interface IModel {
void onLoad(String url, int i);
}
//Model实现IModel
public class Model implements IModel {
private onFinishListenner onFinishListenner;
public interface onFinishListenner {
void onFinsh(SortBean sortBean, int i);
void onGoodsListFinsh(GoodsList goodsList, int i);
void onChildFinsh(SortRithtChildBean sortRithtChildBean, int i);
}
public void setOnFinishListenner(onFinishListenner onFinishListenner) {
this.onFinishListenner = onFinishListenner;
}
;
//获取数据
@Override
public void onLoad(String url, int i) {
if (i == 0) {
OkHttp3Utils.doGet(url, new GsonObjectCallback<SortBean>() {
@Override
public void onUi(SortBean sortBean) {
if (onFinishListenner != null) {
onFinishListenner.onFinsh(sortBean, 0);
}
}
@Override
public void onFailed(Call call, IOException e) {
}
});
} else if (i == 1) {
Log.e("Iv", url + "");
OkHttp3Utils.doGet(url, new GsonObjectCallback<SortRithtChildBean>() {
@Override
public void onUi(SortRithtChildBean sortRithtChildBean) {
if (onFinishListenner != null) {
onFinishListenner.onChildFinsh(sortRithtChildBean, 1);
}
}
@Override
public void onFailed(Call call, IOException e) {
}
});
} else if (i == 2) {
OkHttp3Utils.doGet(url, new GsonObjectCallback<GoodsList>() {
@Override
public void onUi(GoodsList goodsList) {
if (onFinishListenner != null) {
onFinishListenner.onGoodsListFinsh(goodsList, 2);
}
}
@Override
public void onFailed(Call call, IOException e) {
}
});
}
}
}
//V层
public interface IView {
void getSortBean(SortBean sortBean);
void getSortChildBean(SortRithtChildBean sortRithtChildBean);
}
//实现右侧商品点击事件
public interface GoodsListIView {
void getGoodsList(GoodsList goodsList);
}
//P层实现M层回调方式
public class Passer implements Model.onFinishListenner {
private IView iView;
private Model model;
private GoodsListIView goodsListIView;
public Passer(IView iView) {
this.iView = iView;
model = new Model();
}
public Passer() {
model = new Model();
}
public void setGoodsListIView(GoodsListIView goodsListIView) {
this.goodsListIView = goodsListIView;
}
public void onLoad(String url, int i) {
model.setOnFinishListenner(this);
model.onLoad(url, i);
}
//结果回传
@Override
public void onFinsh(SortBean sortBean, int i) {
if (iView != null) {
if (sortBean != null) {
iView.getSortBean(sortBean);
}
}
}
//子类结果回传
@Override
public void onChildFinsh(SortRithtChildBean sortRithtChildBean, int i) {
if (iView != null) {
if (sortRithtChildBean != null) {
iView.getSortChildBean(sortRithtChildBean);
}
}
}
//获取商品列表数据
@Override
public void onGoodsListFinsh(GoodsList goodsList, int i) {
if (goodsListIView != null) {
if (goodsList != null) {
goodsListIView.getGoodsList(goodsList);
}
}
}
}
//接口
public class Url {
public static final String SORTGOODSURL = "http://120.27.23.105/product/getCatagory";
public static final String SORTGOODSCHILDURL = "http://120.27.23.105/product/getProductCatagory";
public static final String GOODSDDETAILS = "http://120.27.23.105/product/getProducts";
}
//
//左侧适配器
public class SortLeftAdapter extends RecyclerView.Adapter<SortLeftAdapter.MVH> {
private List<SortBean.DataBean> list;
private Context context;
private setonclick setonclick;
public interface setonclick {
void onclick(View view, int positon, String str);
}
public SortLeftAdapter(List<SortBean.DataBean> list, Context context, setonclick setonclick) {
this.list = list;
this.context = context;
this.setonclick = setonclick;
}
@Override
public MVH onCreateViewHolder(ViewGroup parent, int viewType) {
View sortleft = View.inflate(context, R.layout.sortleft, null);
MVH mvh = new MVH(sortleft);
return mvh;
}
@Override
public void onBindViewHolder(final MVH holder, final int position) {
holder.sortname.setText(list.get(position).getName());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (setonclick != null) {
setonclick.onclick(holder.linearLayout, position, list.get(position).getCid() + "");
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MVH extends RecyclerView.ViewHolder {
private TextView sortname;
private LinearLayout linearLayout;
public MVH(View itemView) {
super(itemView);
sortname = (TextView) itemView.findViewById(R.id.sortname);
linearLayout = (LinearLayout) itemView.findViewById(R.id.sortleft);
}
}
}
//右侧适配器
public class SortRightAdapter extends RecyclerView.Adapter<SortRightAdapter.MVH> {
private List<SortRithtChildBean.DataBean> list;
private Context context;
public SortRightAdapter(List<SortRithtChildBean.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public MVH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.sortright, null);
MVH mvh = new MVH(view);
return mvh;
}
@Override
public void onBindViewHolder(MVH holder, int position) {
holder.textView.setText(list.get(position).getName());
final List<SortRithtChildBean.DataBean.ListBean> listBeanList = this.list.get(position).getList();
ChildssAdapter childssAdapter = new ChildssAdapter(context, listBeanList);
holder.gridview.setAdapter(childssAdapter);
holder.gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
SortRithtChildBean.DataBean.ListBean listBean = listBeanList.get(i);
Intent intent = new Intent(context, GoodsDetailsActivity.class);
intent.putExtra("listBean", listBean);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class MVH extends RecyclerView.ViewHolder {
TextView textView;
GridView gridview;
public MVH(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.titless);
gridview = (GridView) itemView.findViewById(R.id.childssgridview);
}
}
}
//商品子类适配器
public class ChildssAdapter extends BaseAdapter {
private Context context;
private List<SortRithtChildBean.DataBean.ListBean> list;
public ChildssAdapter(Context context, List<SortRithtChildBean.DataBean.ListBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1;
MyViewHolder holder;
if (view == null) {
holder = new MyViewHolder();
view1 = View.inflate(context, R.layout.childssitem, null);
holder.goodsname = (TextView) view1.findViewById(R.id.goodsname);
view1.setTag(holder);
} else {
view1 = view;
holder = (MyViewHolder) view1.getTag();
}
holder.goodsname.setText(list.get(i).getName());
return view1;
}
class MyViewHolder {
TextView goodsname;
}
}
//点击右侧商品适配器
public class GoodsListAdapter extends XRecyclerView.Adapter<GoodsListAdapter.MVH> {
private List<GoodsList.DataBean> list;
private Context context;
public GoodsListAdapter(List<GoodsList.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public MVH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.goodslistitem, null);
MVH mvh = new MVH(view);
return mvh;
}
@Override
public void onBindViewHolder(MVH holder, int position) {
ImageLoader instance = ImageLoader.getInstance();
instance.displayImage(list.get(position).getImages().split("//|")[0], holder.goodstupian);
holder.titles.setText(list.get(position).getTitle());
holder.subhead.setText(list.get(position).getSubhead());
holder.newprice.setText(list.get(position).getBargainPrice() + "");
holder.oldprice.setText(list.get(position).getPrice() + "");
}
@Override
public int getItemCount() {
return list.size();
}
class MVH extends XRecyclerView.ViewHolder {
private ImageView goodstupian;
private TextView titles;
private TextView subhead;
private TextView newprice;
private TextView oldprice;
public MVH(View itemView) {
super(itemView);
goodstupian = (ImageView) itemView.findViewById(R.id.goodstupian);
titles = (TextView) itemView.findViewById(R.id.settitles);
subhead = (TextView) itemView.findViewById(R.id.subhead);
newprice = (TextView) itemView.findViewById(R.id.newprice);
oldprice = (TextView) itemView.findViewById(R.id.oldprice);
}
}
}
//右侧点击商品跳转
public class GoodsDetailsActivity extends AppCompatActivity implements GoodsListIView {
private SortRithtChildBean.DataBean.ListBean listbean;
private TextView listtitles;
private XRecyclerView goodslist;
private Passer passer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods_details);
Intent intent = getIntent();
listbean = (SortRithtChildBean.DataBean.ListBean) intent.getSerializableExtra("listBean");
passer = new Passer();
passer.setGoodsListIView(this);
passer.onLoad(Url.GOODSDDETAILS + "?pscid=" + listbean.getPscid(), 2);
//初始化控件
initView();
}
//初始化控件
private void initView() {
listtitles = (TextView) this.findViewById(R.id.listtitles);
listtitles.setText(listbean.getName() + "列表");
goodslist = (XRecyclerView) this.findViewById(R.id.goodslist);
goodslist.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void getGoodsList(GoodsList goodsList) {
}
//获取商品列表数据
/*public void getGoodsList(GoodsList goodsList) {
if ()
Toast.makeText(this, goodsList.getData().toString() + "", Toast.LENGTH_SHORT).show();
}*/
}
//主类
public class SortFragment extends Fragment implements IView, SortLeftAdapter.setonclick {
private View view;
private RecyclerView sortleft;
private RecyclerView sortright;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_sort, null);
initView();
return view;
}
//初始化控件
private void initView() {
sortright = (RecyclerView) view.findViewById(R.id.sortright);
sortright.setLayoutManager(new LinearLayoutManager(getActivity()));
sortleft = (RecyclerView) view.findViewById(R.id.sortleft);
sortleft.setLayoutManager(new LinearLayoutManager(getActivity()));
new Passer(this).onLoad(Url.SORTGOODSURL, 0);
}
@Override
public void onResume() {
super.onResume();
}
//获取分类数据
@Override
public void getSortBean(SortBean sortBean) {
List<SortBean.DataBean> data = sortBean.getData();
SortLeftAdapter adapter = new SortLeftAdapter(data, getActivity(), this);
sortleft.setAdapter(adapter);
}
//点击事件
@Override
public void onclick(View view, int positon, String str) {
makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
new Passer(this).onLoad(Url.SORTGOODSCHILDURL + "?cid=" + str, 1);
}
//获取子分类数据
@Override
public void getSortChildBean(SortRithtChildBean sortRithtChildBean) {
if (sortRithtChildBean.getData().size() > 0) {
SortRightAdapter sortRightAdapter = new SortRightAdapter(sortRithtChildBean.getData(), getActivity());
sortright.setAdapter(sortRightAdapter);
//刷新Right适配器
sortRightAdapter.notifyDataSetChanged();
}
}
}
//主布局
<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"
tools:context="com.example.lenovo.taobaodemo.fragment.PageFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/sortleft"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"></android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="@+id/sortright"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10"
android:background="#BABABA"></android.support.v7.widget.RecyclerView>
</LinearLayout>
//左侧布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sortleft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/sortname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:text="京东超市"
android:textSize="30dp" />
</LinearLayout>
//右侧布局
<?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/titless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:textSize="25dp"
android:textColor="#000000"
/>
<GridView
android:id="@+id/childssgridview"
android:layout_width="250dp"
android:layout_height="150dp"
android:layout_marginLeft="50dp"
android:layout_marginBottom="10dp"
android:numColumns="3"></GridView>
</LinearLayout>
//商品子类
<?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/goodstupian"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:src="@mipmap/logo" />
<LinearLayout
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/settitles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="18dp" />
<TextView
android:id="@+id/subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/newprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FBBE00"
android:text="111111" />
<TextView
android:id="@+id/oldprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="111111" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
//标题
<?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="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/goodsname" android:layout_width="match_parent" android:layout_height="25dp" android:gravity="center" android:textSize="20dp" /> </LinearLayout>