二级联动

presenter层

public class MainPresenter extends BasePresenter>{
    private final HttpUtil httpUtils;

    public MainPresenter() {
        httpUtils = HttpUtil.getInstance();
    }
    public void loadDataFromNet(){
        httpUtils.getData(Constant.OneURL, HomeBean.class, new HttpUtil.CallbackData() {

            @Override
            public void onResponse(HomeBean homeBean) {
                if(getView()!=null){
                    getView().onSueess(homeBean,0);
                }else {
                    Log.e("myMessage","哥们没有绑定View");
                }
            }

            @Override
            public void onFail(String err) {
                getView().Err(err);
            }
        });
    }
    public void loadTwoDataFromNet(String id){
        httpUtils.getData(Constant.TwoURL+id, HomeBean.class, new HttpUtil.CallbackData() {

            @Override
            public void onResponse(HomeBean homeBean) {
                if(getView()!=null){
                    getView().onSueess(homeBean,1);
                }else {
                    Log.e("myMessage","哥们没有绑定View");
                }
            }

            @Override
            public void onFail(String err) {
                getView().Err(err);
            }
        });
    }
}

主页面(fragment,activity):

public class HomeFragment extends BaseFragment implements ITwoView {


    private ListView listView;
    private MainPresenter mainPresenter;
    private OneAdapter adapter;
    private RecyclerView recyclerView,tworecyclerView;
    private GridLayoutManager gridLayoutManager;
    private TwoAdapter twoAdapter;
    private TwoPresenter twoPresenter;

    @Override
    protected int bindLayout() {
        return R.layout.home_main;
    }

    @Override
    protected void initView() {
        listView = getActivity().findViewById(R.id.listview);
        recyclerView = getActivity().findViewById(R.id.recyclerView);
        tworecyclerView = getActivity().findViewById(R.id.tworecyclerView);
    }

    @Override
    protected void initData() {
        adapter = new OneAdapter(getActivity());
        listView.setAdapter(adapter);
        mainPresenter = new MainPresenter();
        mainPresenter.setView(this);
        mainPresenter.loadDataFromNet();
    }

    @Override
    public void onSueess(HomeBean homeBean, int what) {
        if(what==0){
            final List data = homeBean.getResult();
            adapter.setData(data);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                    String id = data.get(i).getId();
                    //Toast.makeText(getActivity(), ""+ id, Toast.LENGTH_SHORT).show();
                    mainPresenter.loadTwoDataFromNet(id);
                }
            });
        }else{
            final List data = homeBean.getResult();
            gridLayoutManager = new GridLayoutManager(getActivity(),3);
            recyclerView.setLayoutManager(gridLayoutManager);
            twoAdapter = new TwoAdapter(getActivity());
            recyclerView.setAdapter(twoAdapter);
            twoAdapter.setData(data);
            twoAdapter.setDeleteCallBack(new TwoAdapter.DeleteCallBack() {
                @Override
                public void delete(int i) {
                    String id = data.get(i).getId();
                    //Toast.makeText(getActivity(), ""+id, Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(getActivity(), ShowActivity.class);
                    intent.putExtra("id",id);
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void Err(String err) {

    }
}

接口(ITwoView)

public interface ITwoView extends IBaseView{
    void onSueess(T t,int what);
    void Err(String err);
}

适配器:
第一层的

public class OneAdapter extends BaseAdapter {
    private Context context;
    private ArrayList arr=new ArrayList<>();

    public OneAdapter(Context context) {
        this.context = context;
    }
    public void setData(List list){
        if(list!=null){
            arr.addAll(list);
            notifyDataSetChanged();
        }
    }
    @Override
    public int getCount() {
        return arr.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.item_one,null);
            vh=new ViewHolder();
            vh.name=view.findViewById(R.id.name);
            view.setTag(vh);
        }else{
            vh= (ViewHolder) view.getTag();
        }
        vh.name.setText(arr.get(i).getName());
        return view;
    }
    static class ViewHolder{
        TextView name;
    }
}

第二层的

public class TwoAdapter extends RecyclerView.Adapter {
    private Context context;
    private List arr=new ArrayList<>();

    public TwoAdapter(Context context) {
        this.context = context;
    }
    public void setData(List list){
        if(list!=null){
            arr.addAll(list);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public TwoAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_one, null);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull TwoAdapter.MyViewHolder myViewHolder, int i) {
        if(arr.get(i)!=null){
            myViewHolder.name.setText(arr.get(i).getName());
            myViewHolder.view.setTag(i);
            myViewHolder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int tag = (int) view.getTag();
                    deleteCallBack.delete(tag);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return arr.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView name;
        private View view;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.view=itemView;
            name = itemView.findViewById(R.id.name);
        }
    }
    public interface DeleteCallBack{
        void delete(int i);
    }
    private DeleteCallBack deleteCallBack;

    public void setDeleteCallBack(DeleteCallBack deleteCallBack) {
        this.deleteCallBack = deleteCallBack;
    }

}

通过intent传值的话 在传过去的那个页面 这样获取值

Intent intent = getIntent();
        id = intent.getStringExtra("id");

你可能感兴趣的:(二级联动)