效果视图
1.写协议Contract
public interface IShopContract {
public interface IShopView{
public void LeftData(TypeBean type);
public void RightData(ClothesBean clothesBean);
}
public interface IShowPresenter<IShopView>{
public void attachView(IShopView iShopView);
public void detachView(IShopView iShopView);
public void getRseponseLeft();
public void getRseponseRight();
}
public interface IShopModel{
public void constantResponseData(MyCallBack callBack);
public interface MyCallBack{
public void ResponseLeftData(TypeBean type);
public void ResponseRightData(ClothesBean clothesBean);
}
}
2.V层
public class Fragment_fl extends Fragment implements IShopContract.IShopView {
@BindView(R.id.recycle_left)
RecyclerView recycleLeft;
@BindView(R.id.recycle_right)
RecyclerView recycleRight;
Unbinder unbinder;
private View view;
private IShopContract.IShowPresenter shopPresenter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_fl, null);
unbinder = ButterKnife.bind(this, view);
shopPresenter = new ShopPresenter();
shopPresenter.attachView(this);
shopPresenter.getRseponseLeft();
shopPresenter.getRseponseRight();
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
shopPresenter.detachView(this);
}
@Override
public void LeftData(final TypeBean type) {
getActivity().runOnUiThread(new Runnable() {
private TypeAdapter typeAdapter;
@Override
public void run() {
final List<TypeBean.DataBean> data = type.getData();
typeAdapter = new TypeAdapter();
typeAdapter.setData(data,getActivity());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recycleLeft.setLayoutManager(linearLayoutManager);
recycleLeft.setAdapter(typeAdapter);
typeAdapter.setCallBack(new TypeAdapter.CallBack() {
@Override
public void setOnItemClick(View v, int position) {
data.remove(position);
typeAdapter.notifyDataSetChanged();
}
});
}
});
}
@Override
public void RightData(final ClothesBean clothesBean) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
List<ClothesBean.DataBean> data = clothesBean.getData();
ClothesAdapter adapter = new ClothesAdapter();
adapter.setData(getActivity(),data);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recycleRight.setLayoutManager(linearLayoutManager);
recycleRight.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
3.P层
public class ShopPresenter implements IShopContract.IShowPresenter<IShopContract.IShopView> {
IShopContract.IShopView shopView;
private IShopContract.IShopModel shopModel;
private SoftReference<IShopContract.IShopView> reference;
@Override
public void attachView(IShopContract.IShopView iShopView) {
this.shopView=iShopView;
shopModel = new ShopModel();
reference = new SoftReference<>(shopView);
}
@Override
public void detachView(IShopContract.IShopView iShopView) {
reference.clear();
}
@Override
public void getRseponseLeft() {
shopModel.constantResponseData(new IShopContract.IShopModel.MyCallBack() {
@Override
public void ResponseLeftData(TypeBean type) {
shopView.LeftData(type);
}
@Override
public void ResponseRightData(ClothesBean clothesBean) {
shopView.RightData(clothesBean);
}
});
}
@Override
public void getRseponseRight() {
}
4.M层
public class ShopModel implements IShopContract.IShopModel {
private void requestEnqueue(MyCallBack callBack) {
requestEnqueue(callBack);
}
@Override
public void constantResponseData(final MyCallBack callBack) {
OkGo.<String>get(Constant.lefturlString).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String json = response.body().toString();
Gson gson = new Gson();
TypeBean typeBean = gson.fromJson(json, TypeBean.class);
callBack.ResponseLeftData(typeBean);
}
});
OkGo.<String>get(Constant.righturlString).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String json = response.body().toString();
Gson gson = new Gson();
ClothesBean clothesBean = gson.fromJson(json, ClothesBean.class);
callBack.ResponseRightData(clothesBean);
}
});
}
}
5.Adapter
public class TypeAdapter extends RecyclerView.Adapter<TypeAdapter.SubViewHolder> {
List<TypeBean.DataBean> data;
FragmentActivity activity;
private CallBack callBack;
@NonNull
public void setData(List<TypeBean.DataBean> data, FragmentActivity activity) {
this.data = data;
this.activity = activity;
}
@Override
public SubViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = View.inflate(activity, R.layout.left_item, null);
SubViewHolder subViewHolder = new SubViewHolder(inflate);
return subViewHolder;
}
@Override
public void onBindViewHolder(@NonNull SubViewHolder subViewHolder,final int position) {
subViewHolder.textType.setText(data.get(position).getName());
subViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callBack.setOnItemClick(v,position);
}
});
}
@Override
public int getItemCount() {
return data.size();
}
public class SubViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.text_type)
TextView textType;
public SubViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
public interface CallBack{
public void setOnItemClick(View v,int position);
}
public void setCallBack(CallBack callBack) {
this.callBack = callBack;
}}
public class ClothesAdapter extends RecyclerView.Adapter<ClothesAdapter.SubViewHolder> {
FragmentActivity activity;
List<ClothesBean.DataBean> data;
@NonNull
public void setData(FragmentActivity activity, List<ClothesBean.DataBean> data) {
this.activity = activity;
this.data = data;
}
@Override
public SubViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = View.inflate(activity, R.layout.right_item, null);
SubViewHolder subViewHolder = new SubViewHolder(inflate);
return subViewHolder;
}
@Override
public void onBindViewHolder(@NonNull SubViewHolder subViewHolder, int i) {
subViewHolder.textBrand.setText(data.get(i).getName());
RecycleItemAdapter recycleItemAdapter = new RecycleItemAdapter(activity, data);
subViewHolder.recycleClothes.setLayoutManager(new GridLayoutManager(activity,3));
subViewHolder.recycleClothes.setAdapter(recycleItemAdapter);
}
@Override
public int getItemCount() {
return data.size();
}
public class SubViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.text_brand)
TextView textBrand;
@BindView(R.id.recycle_clothes)
RecyclerView recycleClothes;
public SubViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}}
//嵌套里的recycleView
public class RecycleItemAdapter extends RecyclerView.Adapter<RecycleItemAdapter.SubViewHolder> {
FragmentActivity activity;
List<ClothesBean.DataBean> data;
public RecycleItemAdapter(FragmentActivity activity, List<ClothesBean.DataBean> data) {
this.activity = activity;
this.data = data;
}
@NonNull
@Override
public SubViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = View.inflate(activity, R.layout.recycle_item, null);
SubViewHolder subViewHolder = new SubViewHolder(inflate);
return subViewHolder;
}
@Override
public void onBindViewHolder(@NonNull SubViewHolder subViewHolder, int i) {
List<ClothesBean.DataBean.ListBean> list = data.get(i).getList();
subViewHolder.textClothes.setText(list.get(i).getName());
Glide.with(activity).load(list.get(i).getIcon()).into(subViewHolder.imageClothes);
}
@Override
public int getItemCount() {
return data.size();
}
public class SubViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.image_clothes)
ImageView imageClothes;
@BindView(R.id.text_clothes)
TextView textClothes;
public SubViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}}