简单的说一下项目需求,人员管理时添加和编辑岗位,需要一个岗位的单选列表。我没找到现成的控件,所以我自己手写了一个。
说一下实现的思路:其实就是一个ListView,列表元素内含两个元素,第一个是textView,第二个就是ImageView,默认不显示√,点击后显示√。
思路很清晰,但是在实现的时候我遇到了一些问题,查过一些博客,关键就是关于列表元素下标的问题。
先上代码:
这段代码是项目列表:
因为是与后台交互,需要向服务器请求数据,initData实现这个功能。
其次我选择RecyclerView,ListView也行,这不纠结。需要将选择到的信息传到上一级活动,使用Intent实现传值,其他方法也有,比如bundle传值,这个不纠结。index初始化为-1。代码有点乱,这个以后我得改。
public class JobListActivity extends BaseActivity implements View.OnClickListener {
private TextView nav_title;
private RecyclerView rv_select_job;
private SelectListAdapter adapter;
private List listdata = new ArrayList<>();
private List jobBeanList = new ArrayList<>();
private ArrayList mPostIds = new ArrayList<>();
private ArrayList mPostNames = new ArrayList<>();
private String mIndex = "-1";
private String info;
private ImageView back;
private TextView add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_list);
mIndex = getIntent().getStringExtra("mIndex");
initData();
initView();
}
//初始化数据
public void initData() {
//这个使用网络请求框架请求数据,得到相关数据。
AdminApi.getInstance().getJob().subscribe(new OriginalSubscriber>(compositeDisposable) {
@Override
public void onError(ResponseThrowable e) {
ToastUtil.show(JobListActivity.this, e.getMsg());
}
@Override
public void onNext(List jobBeans, String msg) {
jobBeanList = jobBeans;
if (jobBeanList != null && jobBeanList.size() > 0) {
for (JobBean job : jobBeanList) {
listdata.add(new PostBean(job.getId(), job.getName(), false));
}
mPostIds.add("-1");
}
adapter = new SelectListAdapter(JobListActivity.this, listdata, mPostIds);
adapter.setmIndex(mIndex);
rv_select_job.setAdapter(adapter);
}
});
}
public void initView()
{
nav_title=findViewById(R.id.nav_txt_title);
nav_title.setText("选择岗位");
findViewById(R.id.nav_img_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JobListActivity.this.finish();
}
});
add = findViewById(R.id.nav_btn_add);
add.setText("确定");
add.setVisibility(View.VISIBLE);
add.setOnClickListener(this);
rv_select_job=findViewById(R.id.rv_job);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv_select_job.setLayoutManager(linearLayoutManager);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.nav_btn_add:
if (adapter.getPostidLists() != null && adapter.getPostidLists().size() > 0) {
//传值,从数据库获得的index和岗位名称信息,传到上一级Activity
Intent intent = new Intent();
intent.putExtra("mIndex", adapter.getPostidLists().get(0));
intent.putExtra("info", adapter.getNameLists().get(0));
setResult(Activity.RESULT_OK, intent);
//及时finish
JobListActivity.this.finish();
} else {
ToastUtil.show(this, "请选择岗位");
}
}
}
}
其次应该就是重点的Adapter类了,遇到的问题就是index的判断了,因为是选择和编辑都使用这个活动,所以Adapter还需要实现列表的默认值问题,如果是添加,则默认都不显示√,如果是编辑,则需要显示默认的岗位。所以index就很关键。
这里之所以index会如此麻烦,是因为我们的后端传回的index是从1开始的,所以需要处理一下才能转回。
public class SelectListAdapter extends RecyclerView.Adapter {
public Context mContext;
private final int COUNT=6;
private List listDataBeans;
private ArrayList postidLists = new ArrayList<>();
int index = -1;
private String mIndex = "-1";
private ArrayList projectIdList = new ArrayList<>();
private ArrayList nameLists = new ArrayList<>();
private ArrayList codeLists = new ArrayList<>();
public SelectListAdapter(Context mContext, List listDataBeans, ArrayList listids) {
this.mContext = mContext;
this.listDataBeans = listDataBeans;
if (listids != null) {
this.postidLists.addAll(postidLists);
}
}
public void setmIndex(String Index) {
if (!Index.equals("-1")) {
mIndex = Index;
index = Integer.parseInt(mIndex) - 1;
}
}
public List getListDataBeans() {
return listDataBeans;
}
public ArrayList getProjectIdList() {
return projectIdList;
}
public ArrayList getPostidLists() {
return postidLists;
}
public ArrayList getNameLists() {
return nameLists;
}
public ArrayList getCodeLists() {
return codeLists;
}
@Override
public void onBindViewHolder(@NonNull SelectListAdapter.ViewHolder viewHolder, int i) {
PostBean bean = listDataBeans.get(i);
viewHolder.iv_check.setImageResource(R.drawable.check);
//初始化各个集合,判断标准为index,及第一次是否选择元素,否则就不显示图片
if (index > -1) {
listDataBeans.get(index).setSelected(true);
postidLists.add(index + 1 + "");
nameLists.add(listDataBeans.get(index).getName());
projectIdList.add(listDataBeans.get(index).getId());
codeLists.add(listDataBeans.get(index).getCode());
}
//下标相同则显示图片
if (postidLists.contains(bean.getId()))
{
viewHolder.iv_check.setVisibility(View.VISIBLE);
if (!nameLists.contains(bean.getName())) {
nameLists.add(bean.getName());
}
}
else{
viewHolder.iv_check.setVisibility(View.INVISIBLE);
}
//根据元素的图片标志位来显示图片
viewHolder.iv_check.setVisibility(listDataBeans.get(i).isSelected() ? View.VISIBLE : View.INVISIBLE);
//显示具体数值
if (listDataBeans.get(i).getCode() != null)
viewHolder.tv_jop.setText(listDataBeans.get(i).getCode() + "\t\t\t\t" + listDataBeans.get(i).getName());
else
viewHolder.tv_jop.setText(listDataBeans.get(i).getName());
//刷新
viewHolder.itemView.setOnClickListener(v->{
//将所有集合的数据清空
postidLists.clear();
nameLists.clear();
projectIdList.clear();
codeLists.clear();
//添加数据
postidLists.add(i + 1 + "");
nameLists.add(listDataBeans.get(i).getName());
projectIdList.add(listDataBeans.get(i).getId());
if (codeLists != null) {
codeLists.add(listDataBeans.get(i).getCode());
}
//其余元素的imageView标志位为false
for (PostBean post : listDataBeans) {
post.setSelected(false);
}
//imageView标志位为True
listDataBeans.get(i).setSelected(true);
//刷新
notifyDataSetChanged();
index = -1;
});
}
@NonNull
@Override
public SelectListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(mContext).inflate(R.layout.item_job_list, viewGroup, false);
ViewHolder holder = new ViewHolder(v);
return holder;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_jop;
private ImageView iv_check;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tv_jop = itemView.findViewById(R.id.tv_group);
iv_check = itemView.findViewById(R.id.iv_check);
}
}
@Override
public int getItemCount() {
if (listDataBeans == null) {
return 0;
}
return listDataBeans.size();
}
}
数据项
public class PostBean {
String id;
String name;
String code;
boolean isSelected;
public PostBean() {
}
public PostBean(String id, String name, boolean isSelected) {
this.id = id;
this.name = name;
this.isSelected = isSelected;
}
public PostBean(String id, String name, String code, boolean isSelected) {
this.id = id;
this.name = name;
this.code = code;
this.isSelected = isSelected;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
我这写的也有点乱,也没有专门整理一下,大体理个思路。
参考了很多。