欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android购物分类效果实现

程序员文章站 2022-05-27 10:22:33
本文实例为大家分享了android购物分类效果展示的具体代码,供大家参考,具体内容如下 secondactivity.java public class sec...

本文实例为大家分享了android购物分类效果展示的具体代码,供大家参考,具体内容如下

secondactivity.java

public class secondactivity extends appcompatactivity{
  private viewpager mpager;
  private radiogroup mradiogroup;

  @override
  protected void oncreate(@nullable bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.second);

     mpager = findviewbyid(r.id.pager);
     mradiogroup = findviewbyid(r.id.group);

     mpager.setadapter(new fragmentpageradapter(getsupportfragmentmanager()) {
       @override
       public fragment getitem(int position) {
         fragment fragment=null;

         switch (position){
           case 0:
             fragment = new afrag();
             break;
           case 1:
             fragment = new bfrag();
             break;
           case 2:
             fragment = new cfrag();
             break;
         }
         return fragment;
       }

       @override
       public int getcount() {
         return 3;
       }
     });

     mradiogroup.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() {
       @override
       public void oncheckedchanged(radiogroup radiogroup, int i) {
         switch (i){
           case r.id.r1:
             mpager.setcurrentitem(0);
             break;
           case r.id.r2:
             mpager.setcurrentitem(1);
             break;
           case r.id.r3:
             mpager.setcurrentitem(2);
             break;
         }
       }
     });
  }
}

secondactivity.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <android.support.v4.view.viewpager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="13"/>

  <radiogroup
    android:id="@+id/group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal">

    <radiobutton
      android:id="@+id/r1"
      android:layout_width="wrap_content"
      android:layout_weight="1"
      android:layout_height="match_parent"
      android:button="@null"
      android:gravity="center"
      android:text="首页"/>

    <radiobutton
      android:id="@+id/r2"
      android:layout_width="wrap_content"
      android:layout_weight="1"
      android:layout_height="match_parent"
      android:button="@null"
      android:gravity="center"
      android:text="分类"/>

    <radiobutton
      android:id="@+id/r3"
      android:layout_width="wrap_content"
      android:layout_weight="1"
      android:layout_height="match_parent"
      android:button="@null"
      android:gravity="center"
      android:text="购物车"/>
  </radiogroup>
</linearlayout>

afrag.java

public class afrag extends fragment{
  private recyclerview mrecyclerview;
  public string urlstring="http://www.wanandroid.com/tools/mockapi/6523/restaurants_offset_0_limit_4 ";

  @nullable
  @override
  public view oncreateview(@nonnull layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {
    view view = view.inflate(getactivity(), r.layout.afrag, null);
    mrecyclerview = view.findviewbyid(r.id.rv);
    initdata();

    return view;
  }

  private void initdata() {
    okhttps okhttps = okhttps.getinstance();

    okhttps.doget(urlstring, new okhttps.okcallback() {
      @override
      public void onresponse(string json) { //瀑布流

        pububean pububean = new gson().fromjson(json,pububean.class);
        list<pububean.databean> results = pububean.getdata();

        pubuadapter pubuadapter = new pubuadapter(getactivity());
        pubuadapter.setdatas(results);
        mrecyclerview.setadapter(pubuadapter);

        staggeredgridlayoutmanager manager = new staggeredgridlayoutmanager(2, staggeredgridlayoutmanager.vertical);
        mrecyclerview.setlayoutmanager(manager);
      }

      @override
      public void onfailure(exception e) {

      }
    });

  }
}

afrag.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <android.support.v7.widget.recyclerview
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</linearlayout>

pubuadapter.java

public class pubuadapter extends recyclerview.adapter<pubuadapter.viewholder>{
  private context mcontext;
  arraylist<pububean.databean> list;

  public pubuadapter(context context) {
    mcontext = context;
    list = new arraylist<>();
  }

  public void setdatas(list<pububean.databean> datas) {
    list.addall(datas);

  }
  @nonnull
  @override
  public viewholder oncreateviewholder(@nonnull viewgroup parent, int viewtype) {
    view view = view.inflate(mcontext, r.layout.pbitem, null);
    viewholder holder = new viewholder(view);
    return holder;
  }

  @override
  public void onbindviewholder(@nonnull viewholder holder, int position) {

    holder.mtextview.settext(list.get(position).getname());
    glide.with(mcontext).load(list.get(position).getpic_url()).into(holder.mimageview);


  }

  @override
  public int getitemcount() {
    return list.size();
  }



  public class viewholder extends recyclerview.viewholder {
    imageview mimageview;
    textview mtextview;
    public viewholder(view itemview) {
      super(itemview);
      mimageview = itemview.findviewbyid(r.id.image);
      mtextview = itemview.findviewbyid(r.id.t1);

    }
  }
}

bfrag.java

public class bfrag extends fragment{
  private recyclerview mrecyclerview;
  leftadapter adapter;
  string urlstring="http://www.zhaoapi.cn/product/getcatagory";
  string urlstring2="http://www.zhaoapi.cn/product/getproductcatagory";
  private recyclerview mrecyclerview2;

  @nullable
  @override
  public view oncreateview(@nonnull layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {
    view view = view.inflate(getactivity(), r.layout.bfrag, null);//分类页面

    mrecyclerview = view.findviewbyid(r.id.rv1); //左侧列表

    mrecyclerview2 = view.findviewbyid(r.id.rv2); //右
    initdata();

    return view;
  }

  private void initdata() {
    okhttps okhttps = okhttps.getinstance();

    okhttps.doget(urlstring, new okhttps.okcallback() {
      @override
      public void onresponse(string json) { //瀑布流

        leftbean leftbean = new gson().fromjson(json,leftbean.class);
        list<leftbean.databean> results = leftbean.getdata();

        adapter = new leftadapter(getactivity());
        adapter.setdatas(results);

        mrecyclerview.setadapter(adapter);


        mrecyclerview.setlayoutmanager(new linearlayoutmanager(getactivity(),linearlayoutmanager.vertical,false));

        adapter.setonitemclicklistener(new leftadapter.onitemclicklistener() {
          @override
          public void onitemclick(int position) {

            //toast.maketext(getactivity(),position+"",toast.length_short).show();
            rigth(position);
          }
        });
      }

      @override
      public void onfailure(exception e) {

      }
    });
  }

  public void rigth(int position){
    hashmap<string, string> map = new hashmap<>();
    map.put("cid",position+1+"");

    okhttps.getinstance().dopost(urlstring2,map,new okhttps.okcallback() {
      @override
      public void onresponse(string json) {

        rightbean rightbean = new gson().fromjson(json,rightbean.class);
        list<rightbean.databean> results = rightbean.getdata();

        rightadapter rightadapter = new rightadapter(getactivity());
        rightadapter.setdatas(results);

        mrecyclerview2.setadapter(rightadapter);

        mrecyclerview2.setlayoutmanager(new gridlayoutmanager(getactivity(),3));
      }

      @override
      public void onfailure(exception e) {

      }
    });
  }
}

bfrag.xml

<android.support.v7.widget.recyclerview
    android:id="@+id/rv1"
    android:layout_width="0dp"
    android:layout_weight="2"
    android:layout_height="match_parent"/>

  <android.support.v7.widget.recyclerview
    android:id="@+id/rv2"
    android:layout_width="0dp"
    android:layout_weight="10"
    android:layout_height="match_parent"/>

</linearlayout>

leftadapter.java

public class leftadapter extends recyclerview.adapter<leftadapter.viewholder>{
  private context mcontext;
  arraylist<leftbean.databean> list;

  public leftadapter(context context) {
    mcontext = context;
    list = new arraylist<>();
  }

  public void setdatas(list<leftbean.databean> datas) {
    list.addall(datas);

  }

  public interface onitemclicklistener{  //自定义接口回调设置点击事件
    void onitemclick(int position);
  }

  private onitemclicklistener monitemclicklistener;

  public void setonitemclicklistener(onitemclicklistener onitemclicklistener){
    monitemclicklistener=onitemclicklistener;
  }

  @nonnull
  @override
  public viewholder oncreateviewholder(@nonnull viewgroup parent, int viewtype) {
    view view = view.inflate(mcontext, r.layout.leftitem, null);
    viewholder holder = new viewholder(view);
    return holder;
  }

  @override
  public void onbindviewholder(@nonnull final viewholder holder, int position) {

    holder.mtextview.settext(list.get(position).getname());

    holder.itemview.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view view) {
        int position1 = holder.getlayoutposition();
        monitemclicklistener.onitemclick(position1);
      }
    });
  }

  @override
  public int getitemcount() {
    return list.size();
  }



  public class viewholder extends recyclerview.viewholder {
    textview mtextview;
    public viewholder(view itemview) {
      super(itemview);
      mtextview = itemview.findviewbyid(r.id.text1);

    }
  }
}

rightadapter.java

public class rightadapter extends recyclerview.adapter<rightadapter.viewholder>{
  private context mcontext;
  arraylist<rightbean.databean> list;

  public rightadapter(context context) {
    mcontext = context;
    list = new arraylist<>();
  }

  public void setdatas(list<rightbean.databean> datas) {
    list.addall(datas);

  }
  @nonnull
  @override
  public viewholder oncreateviewholder(@nonnull viewgroup parent, int viewtype) {
    view view = view.inflate(mcontext, r.layout.rightitem, null);
    viewholder holder = new viewholder(view);
    return holder;
  }

  @override
  public void onbindviewholder(@nonnull viewholder holder, int position) {
    holder.mtextview.settext(list.get(position).getname());
    glide.with(mcontext).load(list.get(position).getlist().get(position).geticon()).into(holder.mimageview);
  }

  @override
  public int getitemcount() {
    return list.size();
  }



  public class viewholder extends recyclerview.viewholder {
    imageview mimageview;
    textview mtextview;

    public viewholder(view itemview) {
      super(itemview);
      mimageview = itemview.findviewbyid(r.id.image1);
      mtextview = itemview.findviewbyid(r.id.text2);

    }
  }
}

okhttps.java

public class okhttps {
  handler handler;
  private final okhttpclient mokhttpclient;
  private static okhttps mokhttps;

  private okhttps() {
    handler = new handler(looper.getmainlooper());

    logginginterceptor logginginterceptor = new logginginterceptor(); //日志拦截器
    mokhttpclient = new okhttpclient.builder()
        .addinterceptor(logginginterceptor)  //设置日志拦截器
        .connecttimeout(5000, timeunit.milliseconds)
        .readtimeout(5000,timeunit.milliseconds)
        .writetimeout(5000,timeunit.milliseconds)
        .build();

  }

  public static okhttps getinstance(){
    if (mokhttps==null){
      synchronized (okhttps.class){
        if (mokhttps==null){
          return mokhttps=new okhttps();
        }
      }
    }
    return mokhttps;
  }

  public interface okcallback{
    void onresponse(string json);

    void onfailure(exception e);

  }

  public void dopost(string url, map<string,string> map, final okcallback okcallback){
    formbody.builder builder = new formbody.builder();
    if (map!=null){
      for (string key:map.keyset()){
        builder.add(key,map.get(key));
      }
    }

    formbody formbody = builder.build();

    final request request = new request.builder()
        .post(formbody)
        .url(url)
        .build();

    call call = mokhttpclient.newcall(request);
    call.enqueue(new callback() {
      @override
      public void onfailure(call call, final ioexception e) {
        if (okcallback!=null){
          handler.post(new runnable() {
            @override
            public void run() {

              okcallback.onfailure(e);
            }
          });
        }
      }

      @override
      public void onresponse(call call, response response) throws ioexception {

        try {
          if (response!=null && response.issuccessful()){
            final string json = response.body().string();
            handler.post(new runnable() {
              @override
              public void run() {
                if (okcallback!=null){

                  okcallback.onresponse(json);
                  return;
                }
              }
            });
          }
        }catch (ioexception e){
          e.printstacktrace();
        }
        if (okcallback!=null){
          okcallback.onfailure(new exception("网络异常"));
        }
      }
    });

  }
  public void doget(string url, final okcallback okcallback){
    request request = new request.builder()
        .url(url)
        .build();

    call call = mokhttpclient.newcall(request);
    call.enqueue(new callback() {
      @override
      public void onfailure(call call, final ioexception e) {
        if (okcallback != null) {
          //切换到主线程
          handler.post(new runnable() {
            @override
            public void run() {
              okcallback.onfailure(e);
            }
          });
        }
      }

      @override
      public void onresponse(call call, response response) throws ioexception {

        try {
          if (response != null && response.issuccessful()) {
            final string json = response.body().string();
            handler.post(new runnable() {
              @override
              public void run() {
                if (okcallback != null) {
                  okcallback.onresponse(json);
                }

              }
            });
          }
        } catch (ioexception e) {
          e.printstacktrace();
        }
      }
    });
  }
}

依赖

implementation 'com.google.code.gson:gson:2.8.5'
  implementation 'com.squareup.okhttp3:okhttp:3.12.0'
  implementation 'com.android.support:recyclerview-v7:27.1.1'
  implementation 'com.github.bumptech.glide:glide:3.7.0'

logginginterceptor.java //拦截器

class logginginterceptor implements interceptor {
 @override
 public response intercept(chain chain) throws ioexception {
  //拿到request对象
  request request = chain.request();

  long t1 = system.nanotime();
  system.out.println(" request = " + string.format("sending request %s on %s%n%s",
      request.url(), chain.connection(), request.headers()));

  //拿到response对象
  response response = chain.proceed(request);

  long t2 = system.nanotime();
  //得出请求网络,到得到结果,中间消耗了多长时间
  system.out.println("response " + string.format("received response for %s in %.1fms%n%s",
      response.request().url(), (t2 - t1) / 1e6d, response.headers()));
  return response;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。