Android 仿京东分类页面

activity_main




   
   
       
   

MianActivity

public class MainActivity extends AppCompatActivity implements GroupPresenter.GroupListener, RightPresenter.RightListener {
    RecyclerView mRecyclerView,recycle_right;
    Madapter madapter;
    RightAdapter rightAdapter;
    RightPresenter rightPresenter;
    GroupPresenter groupPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView  =findViewById(R.id.recycle);
        recycle_right = findViewById(R.id.recycle_right);

        //设置左边布局
        initLeft();
        //设置右边布局   默认加载第一页
        initRight("http://www.zhaoapi.cn/product/getProductCatagory?cid=1");
    }

    private void initRight(String urls) {

        //p层
        rightPresenter = new RightPresenter(this);
        rightPresenter.loadData(urls);
        //适配器
        rightAdapter = new RightAdapter(MainActivity.this);
        recycle_right.setAdapter(rightAdapter);

        recycle_right.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));

    }


    private void initLeft() {
        //左边布局  适配器
        madapter = new Madapter(this);
        mRecyclerView.setAdapter(madapter);

        //p层  请求数据
        groupPresenter = new GroupPresenter(this);
        groupPresenter.requestData();

        //布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        //条目点击
        madapter.setMadapterListener(new Madapter.MadapterListener() {
            @Override
            public void setonitemclick(int cid) {

              Log.e("哈哈哈哈哈哈哈哈哈哈",cid+"");
                String path =  "http://www.zhaoapi.cn/product/getProductCatagory?cid="+cid;
                 initRight(path);

            }
        });
    }

    @Override
    public void datasuccess(List dataBeans) {

        madapter.setData(dataBeans);
    }

    @Override
    public void rightsuccess(List dataBeans) {
       rightAdapter.setData(dataBeans);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

         rightPresenter = null;
         groupPresenter=null;

    }
}

左边条目的适配器 Madapter

**
 * date:2018/11/21
 * author:QMY(QMY)
 * function:
 */
public class Madapter extends RecyclerView.Adapter {
    private final MainActivity mainActivity;
    private final List grouplist;

    public Madapter(MainActivity mainActivity) {
        this.mainActivity=mainActivity;
        grouplist = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(mainActivity, R.layout.group, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.tv.setText(grouplist.get(i).getName());

        final GroupBean.DataBean bean = grouplist.get(i);
        //条目点击事件
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMadapterListener.setonitemclick(bean.getCid());
            }
        });
    }

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

    public void setData(List dataBeans) {
        grouplist.clear();
        grouplist.addAll(dataBeans);
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tv = itemView.findViewById(R.id.tv);
        }
    }

    public interface  MadapterListener{
        void setonitemclick(int cid);
    }

    public MadapterListener mMadapterListener;

    public void setMadapterListener(MadapterListener madapterListener){
        mMadapterListener=madapterListener;
    }
}

//右边第一个适配器

public class RightAdapter extends RecyclerView.Adapter {
    private final List rightlist;
    private final MainActivity mainActivity;


    public RightAdapter(MainActivity mainActivity) {

        this.mainActivity =mainActivity;
        rightlist=new ArrayList<>();

    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(mainActivity, R.layout.right, null);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.name.setText(rightlist.get(i).getName());
        List list = rightlist.get(i).getList();

       RightAdapter1 rightAdapter1 = new RightAdapter1(list,mainActivity);
       viewHolder.recycle_right1.setAdapter(rightAdapter1);

       viewHolder.recycle_right1.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

       rightAdapter1.setOnItemClickListener(new RightAdapter1.OnItemClickListener() {
           @Override
           public void onItemClickListener(int layoutPosition) {
               //点击条目的时候要根据点击的下标跳转到相应的页面,
               // 所以需要拿到解析完的集合list.get(传过来的下标).地址
                Intent intent = new Intent(mainactivity, Goods.class);
                intent.putExtra("pscid",pscid);
                activity.startActivity(intent);

           }
       });
    }

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

    public void setData(List dataBeans) {
        this.rightlist.clear();
        rightlist.addAll(dataBeans);
        notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recycle_right1;
        TextView name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            recycle_right1 = itemView.findViewById(R.id.recycle_right1);
            name = itemView.findViewById(R.id.name);
        }
    }

//右边下边适配

/**
 * date:2018/11/22
 * author:QMY(QMY)
 * function:
 */
class RightAdapter1 extends RecyclerView.Adapter {
    private final List list;
    private final MainActivity mainactivity;

    public RightAdapter1(List list, MainActivity mainActivity) {
        this.list=list;
        this.mainactivity = mainActivity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(mainactivity, R.layout.right1, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {

        viewHolder.name.setText(list.get(i).getName());
        ImageLoader.getInstance().displayImage(list.get(i).getIcon(),
                viewHolder.image,ImageLoderConfigs.getimageoption(mainactivity));

        viewHolder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(list.get(i).getPscid());
            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            image = itemView.findViewById(R.id.image);
            name = itemView.findViewById(R.id.name);
        }
    }

    //实现接口
    private OnItemClickListener mOnItemClickListener;
    //创建接口
    public interface OnItemClickListener{
        void onItemClickListener(int layoutPosition);
    }
    //外界调用的方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener= onItemClickListener;
    }

}

//左边条目p层

public class GroupPresenter {

    public  void requestData(){
       String path =  "http://www.zhaoapi.cn/product/getCatagory";
        OkHttpUtils.getInstance().doGet(path, new OkHttpUtils.Utils() {
            @Override
            public void failed() {

            }

            @Override
            public void success(String string) {
                GroupBean groupBean = new Gson().fromJson(string, GroupBean.class);
                mGroupListener.datasuccess(groupBean.getData());
            }
        });
    }

    public interface GroupListener{
        void datasuccess(List dataBeans);
    }

    public GroupListener mGroupListener;

    public GroupPresenter(GroupListener groupListener) {
        mGroupListener = groupListener;
    }
}

右边p层

public class RightPresenter {



     public void loadData(String urls){

         OkHttpUtils.getInstance().doGet(urls, new OkHttpUtils.Utils() {
             @Override
             public void failed() {

             }

             @Override
             public void success(String string) {
                 //解析
                 RightBean rightBean = new Gson().fromJson(string, RightBean.class);
                 List data = rightBean.getData();
                 Log.e("gsaga",rightBean+"");

                 mRightListener.rightsuccess(data);
             }
         });



     }

     public interface  RightListener{
         void rightsuccess(List dataBeans);
     }
     public RightListener mRightListener;

    public RightPresenter(RightListener rightListener) {
        mRightListener = rightListener;
    }
}

左边布局




    

右上





    
    

右下




    
    

你可能感兴趣的:(Android 仿京东分类页面)