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

Android实现可复用的选择页面

程序员文章站 2022-11-22 22:47:13
本文实例为大家分享了android实现可复用的选择页面的具体代码,供大家参考,具体内容如下 窗口代码 /** * 根据上一个页面传过来的ismultiple...

本文实例为大家分享了android实现可复用的选择页面的具体代码,供大家参考,具体内容如下

窗口代码

/**
 * 根据上一个页面传过来的ismultipleenable判断是否可以多选
 * 1.单选页面选中一个条目直接结束并回传
 * 2.多选页面选中之后需要提交
 * 3.提交数据之后需要刷新列表
 */
public class userselectactivity extends appcompatactivity implements view.onclicklistener {

  private static final string tag = "userselectactivity";

  @bindview(r.id.tv_title_middle)
  textview title;
  @bindview(r.id.title_left)
  imageview back;
  @bindview(r.id.normal_user_listview)
  listview mlistview;
  @bindview(r.id.edit_tv)
  textview edit;
  @bindview(r.id.filter_tv)
  textview filter;

  private context mcontext;
  private boolean ismultipleenable;
  private userselectadapter madapter;
  private list<clsnormaluser> mclsnormaluserlist;

  @override
  protected void oncreate(@nullable bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_normal_user_list);
    //初始化butterknife
    butterknife.bind(this);

    initview();
    initdata();
    initlistener();
  }

  private void initdata() {
    mcontext = this;

    //通过源页面传过来的值来生成对应的视图,默认是单选
    ismultipleenable = getintent().getbooleanextra("ismultipleenable", false);

    //初始化数据源
    mclsnormaluserlist = new arraylist<>();
    madapter = new userselectadapter(mclsnormaluserlist, mcontext);
    mlistview.setadapter(madapter);

    getofflinedata(20);

    //多选页面要显示提交按钮和checkbox
    if (ismultipleenable) {
      edit.setcompounddrawableswithintrinsicbounds(r.drawable.ic_send_black_24dp, 0, 0, 0);
      edit.setvisibility(view.visible);

      for (clsnormaluser user : mclsnormaluserlist) {
        user.setischeckboxvisible(true);
      }
    }
  }

  //生成模拟数据
  private void getofflinedata(int num) {
    list<clsnormaluser> clsnormaluserlist = new arraylist<>();
    for (int i = 0; i < num; i++) {
      clsnormaluser clsnormaluser = new clsnormaluser();
      clsnormaluser.setuserid("userid " + i);
      clsnormaluser.setworknumber("worknumber " + i);
      clsnormaluser.setusertype("usertype " + i);
      clsnormaluser.setorgname("orgname " + i);
      clsnormaluser.setcname("cname " + i);
      clsnormaluser.setsysusername("sysusername " + i);
      clsnormaluser.setorgid("orgid " + i);
      clsnormaluser.setworkpos("workpos " + i);
      clsnormaluserlist.add(clsnormaluser);
    }
    //数据填充后要同时适配器刷新视图
    mclsnormaluserlist.addall(clsnormaluserlist);
    madapter.notifydatasetchanged();
  }

  private void initview() {
    title.settext("选择用户");
  }

  private void initlistener() {
    edit.setonclicklistener(this);

    mlistview.setonitemclicklistener((parent, view, position, id) -> {
      clsnormaluser clsnormaluser = mclsnormaluserlist.get(position);
      if (ismultipleenable) {
        //多选状态下点击选中当前条目
        if (clsnormaluser.getischeckboxvisible()) {
          clsnormaluser.setischecked(!clsnormaluser.getischecked());
          madapter.notifydatasetchanged();
        }
      } else {
        //单选状态下点击结束当前页并回传值
        intent intent = new intent();
        //实体类要实现序列化接口parcelable
        intent.putextra("user", clsnormaluser);
        setresult(result_ok, intent);
        finish();
      }
    });
  }


  @override
  public void onclick(view v) {

    //点击提交按钮将选中的值回传并结束当前页
    if (v.getid() == r.id.edit_tv) {
      arraylist<clsnormaluser> normaluserlist = new arraylist<>();
      for (clsnormaluser normaluser : mclsnormaluserlist) {
        if (normaluser.getischecked()) {
          normaluserlist.add(normaluser);
        }
      }
      intent intent = new intent();
      //实体类要实现序列化接口parcelable
      intent.putparcelablearraylistextra("users", normaluserlist);
      setresult(result_ok, intent);
      finish();
    }
  }
}

适配器代码

public class userselectadapter extends baseadapter {

  private list<clsnormaluser> mlist;
  private layoutinflater minflater;

  public userselectadapter(list<clsnormaluser> list, context mcontext) {
    mlist = list;
    minflater = layoutinflater.from(mcontext);
  }

  @override
  public int getcount() {
    return mlist.size();
  }

  @override
  public clsnormaluser getitem(int position) {
    return mlist.get(position);
  }

  @override
  public long getitemid(int position) {
    return position;
  }

  @override
  public view getview(final int position, view convertview, viewgroup root) {
    itemholder holder;
    clsnormaluser clsnormaluser = mlist.get(position);

    if (convertview == null) {
      convertview = minflater.inflate(r.layout.item_alluser_sys, null);
      holder = new itemholder();
      holder.id = convertview.findviewbyid(r.id.user_id);
      holder.name = convertview.findviewbyid(r.id.user_name);
      holder.org = convertview.findviewbyid(r.id.user_org);

      holder.checkbox = convertview.findviewbyid(r.id.user_checkbox);
      convertview.settag(holder);
    } else {
      holder = (itemholder) convertview.gettag();
    }

    holder.id.settext(clsnormaluser.getuserid());
    holder.name.settext(clsnormaluser.getcname());
    holder.org.settext(clsnormaluser.getorgname());

    //是否显示checkbox
    if (clsnormaluser.getischeckboxvisible()) {
      holder.checkbox.setvisibility(view.visible);
    } else {
      holder.checkbox.setvisibility(view.gone);
    }

    //条目是否选中
    if (clsnormaluser.getischecked()) {
      holder.checkbox.setimageresource(r.drawable.ic_check_box_black_24dp);
    } else {
      holder.checkbox.setimageresource(r.drawable.ic_check_box_outline_blank_black_24dp);
    }

    return convertview;
  }

  class itemholder {
    textview id;
    textview name;
    textview org;
    imageview checkbox;
  }
}

效果如图

Android实现可复用的选择页面Android实现可复用的选择页面

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