项目开发,要求一个类似于钉钉审核的东西,如下
挺简单的一个东西,去网上找了一下,发现没有现成的,就想着自己做一个了
大概说一下思路,其实比较简单
1.这个审核列表可以提取为一个有字的view和右边的一个箭头这样的一个layout组成的,最后一个为一个加号的图片
2.点击加号可以添加新的项,点击项目,可以删除当前
3.颜色是随机的
其实这就是一个gridview就可以实现的简单ui了(其实还可以用recylerview,会更加简单好看,需要的可以自己尝试)
首先一个简单的gridview
<GridView
android:id="@+id/gv_audit"
android:layout_width="match_parent"
android:layout_height="200dp"
android:numColumns="5"
android:horizontalSpacing="3dp"
android:verticalSpacing="5dp">
</GridView>
参数意义就不讲了,都能看懂吧
然后就是一个gridview的适配器
public class AuditGridAdapter extends BaseAdapter {
private LayoutInflater inflater;
private boolean shape;
private Context context;
private List<NameEntity> nameList = new ArrayList<>();
public boolean isShape() {
return shape;
}
public void setShape(boolean shape) {
this.shape = shape;
}
public AuditGridAdapter(Context context, List<NameEntity> nameList) {
this.context = context;
this.nameList = nameList;
inflater = LayoutInflater.from(context);
}
public int getCount() {
if (nameList.size() == 9) {
return 9;
}
return (nameList.size() + 1);
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int arg0) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.item_audit, parent, false);
TextView tvName = (TextView) convertView.findViewById(R.id.tvAuditName);
ImageView ivArrow = (ImageView) convertView.findViewById(R.id.ivAuditArrow);
if (position == nameList.size()) {
tvName.setBackground(ContextCompat.getDrawable(context, R.mipmap.more));
ivArrow.setVisibility(View.INVISIBLE);
tvName.setText("");
if (position == 9) {
tvName.setVisibility(View.INVISIBLE);
}
} else {
NameEntity name = nameList.get(position);
if (position == 8) {
ivArrow.setVisibility(View.INVISIBLE);
} else {
ivArrow.setVisibility(View.VISIBLE);
}
tvName.setBackground(ContextCompat.getDrawable(context, name.getColor()));
tvName.setText(name.getName());
}
return convertView;
}
}
挨个看,首先是getcount( )方法
public int getCount() {
if (nameList.size() == 9) {
return 9;
}
return (nameList.size() + 1);
}
重写了这个方法,是为了最后加号的实现,判断是否为9是为了给审核列表设置一个上限,我这里最多只能有九个项,+1是为了给加号留一个位置,做过照片上传之类的东西的,应该都明白是什么意思吧
然后是getview方法,这里我没有用holder,恩,不是不能用,因为懒而已。。。这只是个demo
if (position == nameList.size()) {
tvName.setBackground(ContextCompat.getDrawable(context, R.mipmap.more));
ivArrow.setVisibility(View.INVISIBLE);
tvName.setText("");
if (position == 9) {
tvName.setVisibility(View.INVISIBLE);
}
} else {
NameEntity name = nameList.get(position);
if (position == 8) {
ivArrow.setVisibility(View.INVISIBLE);
} else {
ivArrow.setVisibility(View.VISIBLE);
}
tvName.setBackground(ContextCompat.getDrawable(context, name.getColor()));
tvName.setText(name.getName());
}
注意上面的这个方法,当position = nameList.size()的时候,意思是,当前是审核列表的最后一项,给最后一项设置背景图片为那个加号,设置右边的箭头为不可见,设置字为空,同时,如果position=9,即,现在已经到了最大值,就不再显示加号了
else
就设置文字啊,颜色啊什么的
我这里用了一个NameEntity,里面就两个参数
*/
public class NameEntity {
private String name;
private int color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
}
一个颜色,一个文字
好,下面就是使用方法了
public class AuditTestActivity extends AppCompatActivity {
private GridView gvAudit;
private AuditGridAdapter adapter;
private List<NameEntity> namelist = new ArrayList<>();
private String[] nameArray = new String[]{"张三", "李四", "王五", "巴拉巴", "舒克", "贝塔", "心相印", "stanny", "安卓", "java"};
private int[] colorArray = new int[]{R.mipmap.circle1, R.mipmap.circle2, R.mipmap.circle3, R.mipmap.circle4, R.mipmap.circle5, R.mipmap.circle6};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audit_test);
gvAudit = (GridView) findViewById(R.id.gv_audit);
adapter = new AuditGridAdapter(this, namelist);
gvAudit.setSelector(new ColorDrawable(Color.TRANSPARENT));
gvAudit.setAdapter(adapter);
gvAudit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == namelist.size()) {
NameEntity name = new NameEntity();
name.setName(nameArray[(int) (Math.random() * 10)]);
name.setColor(colorArray[(int) (Math.random() * 6)]);
namelist.add(name);
adapter.notifyDataSetChanged();
} else {
namelist.remove(position);
adapter.notifyDataSetChanged();
}
}
});
}
}
我这里初始化了两个数组,分别是文字数组和背景图片id数组
然后最重要的设置item点击事件
如果点击的position=nameList.size()
即我现在点的是最后一个加号,就添加一个数据进去,刷新列表
否则,就移除当前点击的那个数据
听简单的实现,效果如下