public class ClsFragment extends Fragment implements ClsContract.IclsView,LeftAdapter.ItemClickListener{
private RecyclerView rv_left;
private RecyclerView rv_right;
private ClsPresenter clsPresenter;
private LeftAdapter leftAdapter;
private RightAdapter rightAdapter;
private String cid = "1";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_cls, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
rv_left = view.findViewById(R.id.rv_left);
rv_right = view.findViewById(R.id.rv_right);
initData();
}
private void initData() {
rv_left.setLayoutManager(new LinearLayoutManager(getActivity()));
rv_right.setLayoutManager(new LinearLayoutManager(getActivity()));
clsPresenter = new ClsPresenter(this);
getleft(cid);
clsPresenter.getLeftList(new HashMap());
}
private void getleft(String cid) {
HashMapparmas = new HashMap<>();
parmas.put("cid",cid);
clsPresenter.getRightList(parmas);
}
@Override
public void leftSuccess(ListleftClsBeanList) {
if (leftAdapter==null){
leftAdapter = new LeftAdapter(leftClsBeanList,getActivity());
rv_left.setAdapter(leftAdapter);
leftAdapter.setItemClickListener(this);
}else {
leftAdapter.notifyDataSetChanged();
}
}
@Override
public void rightSuccess(ListrightClsBeanList) {
rightAdapter = new RightAdapter(rightClsBeanList,getActivity());
rv_right.setAdapter(rightAdapter);
}
@Override
public void leftfailure(String error)
}
@Override
public void rightfailure(String error) {
}
@Override
public void click(String cid) {
getleft(cid);
}
}
public interface ClsContract {
public abstract class ClsPresenters{
public abstract void getLeftList(HashMapparams);
public abstract void getRightList(HashMapparams);
}
public interface IClsModel{
void getLeftList(HashMapparams, ClsModel.ClsModelCallback clsModelCallback);
void getRightList(HashMapparams, ClsModel.ClsModelCallback clsModelCallback);
}
public interface IclsView{
void leftSuccess(ListleftClsBeanList);
void rightSuccess(ListrightClsBeanList);
void leftfailure(String error);
void rightfailure(String error);
}
}
public class ClsPresenter extends ClsContract.ClsPresenters {
private ClsModel clsModel;
private ClsContract.IclsView iclsView;
public ClsPresenter(ClsContract.IclsView iclsView) {
this.iclsView = iclsView;
this.clsModel = new ClsModel();
}
@Override
public void getLeftList(HashMapparams) {
clsModel.getLeftList(params, new ClsModel.ClsModelCallback() {
@Override
public void leftsuccess(String result) {
LeftClsBean leftClsBean = new Gson().fromJson(result,LeftClsBean.class);
Listdata = leftClsBean.data;
iclsView.leftSuccess(data);
}
});
}
@Override
public void getRightList(HashMapparams) {
clsModel.getRightList(params, new ClsModel.ClsModelCallback() {
@Override
public void leftsuccess(String result) {
}
@Override
public void rightsuccess(String result) {
RightClsBean rightClsBean = new Gson().fromJson(result,RightClsBean.class);
Listdata = rightClsBean.data;
iclsView.rightSuccess(data);
}
});
}
}
public class ClsModel implements ClsContract.IClsModel {
@Override
public void getLeftList(HashMapparams, final ClsModelCallback clsModelCallback) {
OkhttpUtils.getInstance().doPost(ClsApi.LEFT_API, params, new OkHttpCallback() {
@Override
public void Success(String result) {
if (clsModelCallback!=null){
clsModelCallback.leftsuccess(result);
}
}
@Override
public void Failure(String msg) {
if (clsModelCallback!=null){
clsModelCallback.leftfailure(msg);
}
}
});
}
@Override
public void getRightList(HashMapparams, final ClsModelCallback clsModelCallback) {
OkhttpUtils.getInstance().doPost(ClsApi.RIGHT_API, params, new OkHttpCallback() {
@Override
public void Success(String result) {
if (clsModelCallback!=null){
clsModelCallback.rightsuccess(result);
}
}
@Override
public void Failure(String msg) {
if (clsModelCallback!=null){
clsModelCallback.rightfailure(msg);
}
}
});
}
public interface ClsModelCallback{
void leftsuccess(String result);
void rightsuccess(String result);
void leftfailure(String error);
void rightfailure(String error);
}
}
public class LeftAdapter extends RecyclerView.Adapter{
private Listlist;
private Context context;
public LeftAdapter(Listlist, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public leftViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.left_layout_item,viewGroup,false);
leftViewHolder leftViewHolder = new leftViewHolder(view);
return leftViewHolder;
}
@Override
public void onBindViewHolder(@NonNull leftViewHolder leftViewHolder, int i) {
final LeftClsBean.Cls cls = list.get(i);
leftViewHolder.tv_name.setText(cls.name);
leftViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener!=null){
itemClickListener.click(cls.cid);
}
}
});
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
class leftViewHolder extends RecyclerView.ViewHolder {
private final TextView tv_name;
public leftViewHolder(@NonNull View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
}
}
private ItemClickListener itemClickListener;
public void setItemClickListener(ItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}
public interface ItemClickListener{
void click(String cid);
}
}
public class RightAdapter extends RecyclerView.Adapter{
private Listlist;
private Context context;
public RightAdapter(Listlist, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public rightViewHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.right_layout_item,viewGroup,false);
rightViewHodler rightViewHodler = new rightViewHodler(view);
return rightViewHodler;
}
@Override
public void onBindViewHolder(@NonNull rightViewHodler rightViewHodler, int i) {
RightClsBean.DataBean dataBean = list.get(i);
Listlist = dataBean.list;
rightViewHodler.tv_name.setText(dataBean.name);
rightViewHodler.recyclerView.setLayoutManager(new GridLayoutManager(context,3));
rightViewHodler.recyclerView.setAdapter(new RightItemAdapter(context,list));
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
class rightViewHodler extends RecyclerView.ViewHolder{
private final TextView tv_name;
private final RecyclerView recyclerView;
public rightViewHodler(@NonNull View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
recyclerView = itemView.findViewById(R.id.rv);
}
}
}
public class RightItemAdapter extends RecyclerView.Adapter{
private Context context;
private ListlistBeans;
public RightItemAdapter(Context context, ListlistBeans) {
this.context = context;
this.listBeans = listBeans;
}
@NonNull
@Override
public rightitemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.rightitem_layout_item,viewGroup,false);
rightitemViewHolder rightitemViewHolder = new rightitemViewHolder(view);
return rightitemViewHolder;
}
@Override
public void onBindViewHolder(@NonNull rightitemViewHolder rightitemViewHolder, int i) {
RightClsBean.DataBean.ListBean listBean = listBeans.get(i);
rightitemViewHolder.tv_name.setText(listBean.name);
Glide.with(context).load(listBean.icon).into(rightitemViewHolder.imageView);
}
@Override
public int getItemCount() {
return listBeans==null?0:listBeans.size();
}
class rightitemViewHolder extends RecyclerView.ViewHolder {
private final TextView tv_name;
private final ImageView imageView;
public rightitemViewHolder(@NonNull View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
imageView = itemView.findViewById(R.id.iv);
}
}
}