Android实现可复用的选择页面
作者:free5156 时间:2022-09-25 21:08:24
本文实例为大家分享了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;
}
}
效果如图
来源:https://blog.csdn.net/csdncaoguo940713/article/details/90241307
标签:Android,选择页面
0
投稿
猜你喜欢
Android实现记事本功能(26)
2023-02-17 22:30:23
Flutter图片缓存管理ImageCache原理分析
2023-08-20 23:26:19
Spring Security保护用户密码常用方法详解
2023-01-24 17:06:18
C#数值转换-显式数值转换表(参考)
2023-05-26 22:26:15
SpringMVC @NotNull校验不生效的解决方案
2021-07-19 23:20:07
Spring boot配置 swagger的示例代码
2023-03-07 13:10:44
C#事件标准命名规则及说明(包括用作事件类型的委托命名)
2022-02-27 06:57:43
spring中的注解事务演示和添加步骤详情
2023-03-03 08:32:48
Handler实现线程之间的通信下载文件动态更新进度条
2021-10-27 16:50:36
C#用RabbitMQ实现消息订阅与发布
2022-09-05 16:23:40
Java8中的LocalDateTime和Date一些时间操作方法
2022-06-28 12:28:57
java动态导出excel压缩成zip下载的方法
2022-07-13 07:29:27
SpringBoot创建WebService方法详解
2022-02-08 10:29:31
Java修饰符 abstract,static,final 的区别详解
2023-12-19 22:11:25
Android BannerView通用封装详解
2021-11-16 18:45:41
springboot多环境配置文件及自定义配置文件路径详解
2021-09-30 03:55:54
Java工厂模式的使用细则介绍
2022-03-18 01:57:22
利用C#版OpenCV实现圆心求取实例代码
2022-10-28 12:51:48
Android Gradle依赖管理、去除重复依赖、忽略的方式
2023-06-08 16:07:12
如何把Java程序窗口在屏幕中间显示
2022-12-20 04:59:32