public class ClsFragment extends Fragment implements
ClsContract.IClsView,LeftAdapter.ItemClickListener {
private RecyclerView leftRv,rightRv;
private LeftAdapter leftmadapter;
private RightAdapter rightmadapter;
private ClsPresenter clsPresennter;
private String cid = "1";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_cls,container,false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//初始化控件
initView(view);
}
//初始化控件
private void initView(View view) {
//获取资源id
leftRv= view.findViewById(R.id.rv_left);
rightRv= view.findViewById(R.id.rv_right);
//创建适配器
leftmadapter=new LeftAdapter(getActivity());
rightmadapter=new RightAdapter(getActivity());
//设置设配器
leftRv.setAdapter(leftmadapter);
rightRv.setAdapter(rightmadapter);
leftmadapter.setItemClickListener(this);
//布局管理器
leftRv.setLayoutManager(new LinearLayoutManager(getActivity()));
rightRv.setLayoutManager(new LinearLayoutManager(getActivity()));
//创建P层
clsPresennter = new ClsPresenter(this);
getLeft();
clsPresennter.getLeftList(new HashMap<String, String>());
}
//右边的方法
public void getLeft(){
HashMap<String, String> p2 = new HashMap<>();
p2.put("cid", cid);
clsPresennter.getRightList(p2);
}
@Override
public void leftSucess(List<LeftBean.Lecls> leftlist) {
if(leftlist!=null){
leftmadapter.setList(leftlist);
}
}
@Override
public void rightSucess(List<RightBean.Rgcls> rightlist) {
if (rightlist!=null){
rightmadapter.setList(rightlist);
}
}
@Override
public void leftFailure(String error) {
Toast.makeText(getActivity(),error,Toast.LENGTH_SHORT).show();
}
@Override
public void rightFailure(String error) {
Toast.makeText(getActivity(),error,Toast.LENGTH_SHORT).show();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
}
//点击事件
@Override
public void click(String cid) {
this.cid=null;
this.cid=cid;
getLeft();
rightmadapter.notifyDataSetChanged();
Toast.makeText(getActivity(),"点击了",Toast.LENGTH_SHORT).show();
}
}
LeftRecyclerView适配器
public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.LeftViewHolder> {
private Context context;
private List<LeftBean.Lecls> list;
public LeftAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<LeftBean.Lecls> list) {
if (list!=null){
this.list = list;
}
notifyDataSetChanged();
}
@NonNull
@Override
public LeftViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_left,viewGroup,false);
LeftViewHolder leftViewHolder = new LeftViewHolder(view);
return leftViewHolder;
}
@Override
public void onBindViewHolder(@NonNull LeftViewHolder leftViewHolder, final int i) {
leftViewHolder.textView.setText(list.get(i).name);
leftViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener!=null){
itemClickListener.click(list.get(i).cid+"");
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class LeftViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public LeftViewHolder(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.name);
}
}
private ItemClickListener itemClickListener;
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
//点击事件接口
public interface ItemClickListener{
void click(String cid);
}
}
RightRecyclerView适配器
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.RightViewHolder> {
private Context context;
private List<RightBean.Rgcls> list;
public RightAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<RightBean.Rgcls> list1) {
if (list1!=null){
this.list = list1;
}
notifyDataSetChanged();
}
@NonNull
@Override
public RightViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_right,viewGroup,false);
RightViewHolder rightViewHolder=new RightViewHolder(view);
return rightViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RightViewHolder rightViewHolder, int i) {
rightViewHolder.textView.setText(list.get(i).name);
//设置子分类列表适配器
rightViewHolder.rv.setLayoutManager(new GridLayoutManager(context,3));
RItemAdapter rItemAdapter = new RItemAdapter(context);
rightViewHolder.rv.setAdapter(rItemAdapter);
rItemAdapter.setList(list.get(i).list);
}
@Override
public int getItemCount() {
return list.size();
}
class RightViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private RecyclerView rv;
public RightViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.name);
rv = itemView.findViewById(R.id.rv);
}
}
}
RightAdapter适配器的子类适配器
public class RItemAdapter extends RecyclerView.Adapter<RItemAdapter.ItemViewHolder> {
private Context context;
private List<RightBean.Rgcls.Pcls> list;
public RItemAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<RightBean.Rgcls.Pcls> list) {
if (list!=null){
this.list = list;
}
notifyDataSetChanged();
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.right_item,viewGroup,false);
ItemViewHolder itemViewHolder=new ItemViewHolder(view);
return itemViewHolder;
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, int i) {
itemViewHolder.textView.setText(list.get(i).name);
String[] image=list.get(i).icon.split("\\|");
if (image!=null&&image.length>0){
Glide.with(context).load(image[0]).into(itemViewHolder.imageView);
}
}
@Override
public int getItemCount() {
return list.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private ImageView imageView;
public ItemViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.name);
imageView = itemView.findViewById(R.id.iv);
}
}
}