服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现多级列表中的新建功能

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

2022-10-24 13:53free5156 Android

这篇文章主要为大家详细介绍了Android实现多级列表中的新建功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

窗口代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
 * 新建一个第一级列表的条目
 * 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();
 }
}

布局文件代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?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>

适配器代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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实现多级列表中的新建功能

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

原文链接:https://blog.csdn.net/csdncaoguo940713/article/details/90211722

延伸 · 阅读

精彩推荐