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

Android实现多级列表中的新建功能

程序员文章站 2023-02-21 21:15:16
本文实例为大家分享了android实现多级列表中的新建功能,供大家参考,具体内容如下 多级列表的页面实现比较简单,所以把新建的功能拿出来了。 窗口代码 /**...

本文实例为大家分享了android实现多级列表中的新建功能,供大家参考,具体内容如下

多级列表的页面实现比较简单,所以把新建的功能拿出来了。

窗口代码

/**
 * 新建一个第一级列表的条目
 * 1.选择图片和附件都用intent.action_get_content实现
 * 2.打开文件用intent.action_view实现
 * 3.回传的uri需要转化成真实路径
 * 4.提交数据之后需要刷新列表
 */

public class sectionnewactivity extends appcompatactivity implements view.onclicklistener {
 private static final string tag = "sectionnewactivity";
 @bindview(r.id.tv_title_middle)
 textview title;
 @bindview(r.id.title_left)
 imageview back;
 @bindview(r.id.edit_tv)
 textview edit;
 @bindview(r.id.filter_tv)
 textview filter;
 @bindview(r.id.section_new_logo)
 imageview sectionlogo;
 @bindview(r.id.section_new_manager)
 textview sectionmanager;
 @bindview(r.id.section_new_title)
 textview sectiontitle;
 @bindview(r.id.section_new_desc)
 textview sectiondesc;
 @bindview(r.id.tv_upload_attach)
 textview selectattach;
 @bindview(r.id.lv_attach)
 listview mlistview;

 private context mcontext;
 private list<clsattachment> mattachlist;
 private attachmentlistadapter madapter;

 @override
 protected void oncreate(@nullable bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_section_new);
  butterknife.bind(this);

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

 private void initdata() {
  mcontext = this;

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

 private void initview() {
  title.settext("新建板块");

  edit.setvisibility(view.visible);
  edit.setcompounddrawableswithintrinsicbounds(r.drawable.ic_send_black_24dp, 0, 0, 0);
 }

 private void initlistener() {

  back.setonclicklistener(this);
  edit.setonclicklistener(this);
  filter.setonclicklistener(this);
  sectionlogo.setonclicklistener(this);
  sectionmanager.setonclicklistener(this);
  selectattach.setonclicklistener(this);

  //点击附件列表条目的删除按钮,删除对应附件
  madapter.setmcallback((view, position) -> {
   mattachlist.remove(position);
   madapter.notifydatasetchanged();
  });


  //点击附件列表弹出打开方式
  mlistview.setonitemclicklistener((parent, view, position, id) -> {

   clsattachment clsattachment = mattachlist.get(position);
   intent intent = new intent();
   intent.addflags(intent.flag_activity_new_task);
   intent.setaction(intent.action_view);

   intent.setdataandtype(uri.parse(clsattachment.geturi()), "*/*");
   startactivity(intent);
  });
 }

 @override
 public void onclick(view v) {

  if (v.getid() == r.id.title_left) {
   finish();
  }
  if (v.getid() == r.id.edit_tv) {
   submit();
  }
  if (v.getid() == r.id.section_new_logo) {
   //打开手机原生的文件管理器,并且选取内容
   intent intent = new intent(intent.action_get_content);
   intent.addcategory(intent.category_openable);
   //文件类型为图片
   intent.settype("image/*");
   startactivityforresult(intent, 16352);
  }
  if (v.getid() == r.id.section_new_manager) {
   intent intent = new intent(mcontext, userselectactivity.class);
   startactivityforresult(intent, 12345);
  }

  if (v.getid() == r.id.tv_upload_attach) {

   //上传的附件数量不能超过4个
   if (mattachlist.size() < 4) {
    intent intent = new intent(intent.action_get_content);
    intent.addcategory(intent.category_openable);
    intent.settype("*/*");
    startactivityforresult(intent, 12367);

    if (mattachlist.size() == 0) {
     toast.maketext(mcontext, r.string.upload_warning, toast.length_long).show();
    }

   } else {
    toast.maketext(mcontext, "附件数量已达上限!", toast.length_short).show();
   }
  }
 }

 private void submit() {
  toast.maketext(mcontext, "在此处调用接口!", toast.length_short).show();
  finish();
 }

 @override
 //requestcode要对应上,resultcode都为默认值result_ok
 protected void onactivityresult(int requestcode, int resultcode, intent data) {

  //选择图片完成之后使用glide加载到控件上,此处有时需要把图片上传给后台
  //提交数据的时候传图片在后台的路径
  if (requestcode == 16352 && resultcode == result_ok) {
   glide.with(mcontext).load(data.getdata()).into(sectionlogo);
  }

  //打开选择用户的页面,根据传的参数不同页面也不同,默认是单选页面
  if (requestcode == 12345 && resultcode == result_ok) {
   clsnormaluser user = data.getparcelableextra("user");
   sectionmanager.settext(user.getcname());
  }

  //遍历已经上传的附件列表,如果已经存在就弹出提示
  if (requestcode == 12367 && resultcode == result_ok) {
   string uri = data.getdata().tostring();

   if (mattachlist.size() > 0) {
    for (int i = 0; i < mattachlist.size(); i++) {
     if (uri.equals(mattachlist.get(i).geturi())) {
      toast.maketext(mcontext, "请选择不同文件!", toast.length_short).show();
      break;
     }
     if (i == mattachlist.size() - 1) {
      addattach(data);
      break;
     }
    }
   } else {
    addattach(data);
   }
  }

  super.onactivityresult(requestcode, resultcode, data);
 }

 private void addattach(intent data) {
  //这里使用第三方库ucrop的getpath方法,也可以自己实现uri转换为path
  file file = new file(getpath(mcontext, data.getdata()));

  clsattachment clsattachment = new clsattachment();
  string name = file.getname();
  string type = name.split("\\.")[1];
  string size = file.length() + "";
  clsattachment.setsize(size);
  clsattachment.setfilename(name);
  clsattachment.seturi(data.getdata().tostring());

  //这里需要调用上传接口
  uploadfile(file.getpath());

  mattachlist.add(clsattachment);
  madapter.notifydatasetchanged();
 }

 private void uploadfile(string path) {
  toast.maketext(mcontext, "在此处调用接口!", toast.length_short).show();
 }
}

布局文件代码

<?xml version="1.0" encoding="utf-8"?>
<scrollview xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:background="@color/ghostwhite"
 android:layout_height="match_parent">

 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">

  <include layout="@layout/title_bar" />


  <imageview
   android:id="@+id/section_new_logo"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:scaletype="centercrop"
   android:background="@color/white"
   android:src="@drawable/logo" />

  <include layout="@layout/layout_item_divider_horizontal" />

  <edittext
   android:id="@+id/section_new_title"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:background="@null"
   android:hint="请输入名称"
   android:inputtype="text"
   android:textcolor="@color/black" />

  <include layout="@layout/layout_item_divider_horizontal" />

  <edittext
   android:id="@+id/section_new_desc"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:background="@null"
   android:gravity="top|start"
   android:hint="请输入内容"
   android:inputtype="textmultiline"
   android:minheight="100dp"
   android:textcolor="@color/black" />

  <include layout="@layout/layout_item_divider_horizontal" />

  <linearlayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:orientation="horizontal">

   <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="选择版主:"
    android:textcolor="@color/black"
    android:textsize="18sp" />

   <textview
    android:id="@+id/section_new_manager"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawableend="@drawable/ic_arrow_drop_down_black_24dp"
    android:text="default user"
    android:textcolor="@color/black"
    android:textsize="18sp" />
  </linearlayout>

  <include layout="@layout/layout_item_divider_horizontal" />

  <linearlayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:background="@color/white"
   android:orientation="horizontal">

   <textview
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginstart="20dp"
    android:gravity="center_vertical"
    android:minheight="30dp"
    android:text="文件名:"
    android:textcolor="@color/black" />

   <textview
    android:layout_width="100dp"
    android:layout_height="30dp"
    android:gravity="center_vertical"
    android:text="文件大小:"
    android:textcolor="@color/black" />

   <textview
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:gravity="center_vertical"
    android:text="操作:"
    android:textcolor="@color/black" />

  </linearlayout>

  <listview
   android:id="@+id/lv_attach"
   android:layout_width="match_parent"
   android:layout_height="160dp"
   android:divider="@null" />

  <include layout="@layout/layout_item_divider_horizontal" />

  <textview
   android:id="@+id/tv_upload_attach"
   style="@style/widget.appcompat.button"
   android:layout_width="wrap_content"
   android:layout_height="40dp"
   android:layout_gravity="end"
   android:text="添加附件"
   android:textcolor="@color/black"
   android:textsize="16sp" />
 </linearlayout>
</scrollview>

适配器代码

public class attachmentlistadapter extends baseadapter {

 private list<clsattachment> mlist;
 private layoutinflater minflater;
 private callback mcallback;

 //自定义回调接口,用于传值
 public interface callback {

  void onclick(view view, int position);
 }

 public attachmentlistadapter(list<clsattachment> attachments, context mcontext) {
  this.mlist = attachments;
  minflater = layoutinflater.from(mcontext);
 }

 public void setmcallback(callback mcallback) {
  this.mcallback = mcallback;
 }

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

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

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

 @override
 public view getview(int position, view convertview, viewgroup parent) {

  clsattachment clsattachment = mlist.get(position);
  viewholder holder;

  if (convertview == null) {
   holder = new viewholder();
   convertview = minflater.inflate(r.layout.item_attchment_list, null);
   holder.delete = convertview.findviewbyid(r.id.attachment_delete);
   holder.name = convertview.findviewbyid(r.id.attachment_name);
   holder.size = convertview.findviewbyid(r.id.attachment_size);

   convertview.settag(holder);
  } else {
   holder = (viewholder) convertview.gettag();
  }

  holder.name.settext(clsattachment.getfilename());
  long length = long.parselong(clsattachment.getsize());
  holder.size.settext(length / 1024 + "kb");

  //将position放在tag里面
  holder.delete.settag(position);

  holder.delete.setonclicklistener(v -> {
   //触发点击事件的时候将position回传
   mcallback.onclick(v, (integer) v.gettag());
  });
  return convertview;
 }

 private class viewholder {

  textview name;
  textview size;
  textview delete;
 }
}

效果如图:

Android实现多级列表中的新建功能

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