一个可以动态增删的自定义View
2017-04-10 本文已影响183人
浅吟且行的时光
AddListView.gif很多时候我们都会用到自动增删的组合View,例如:添加任务,添加文件等item,像这样简单的一个列表,我们完全可以封装为一个易用的组合控件。
先说说这个组合控件AddListView的整体思路,首先让AddListView继承LinearLayout,然后定义增加,删除按钮的接口,在合适的地方进行实现,以便在点击时可以监听,通过把数据传递到AddListView,使用addView()方法可以动态的改变AddListView显示状态
主要实现方法
1.让我们的自定义控件AddListView继承LinearLayout,然后定义增加,删除按钮的接口
public class AddListView extends LinearLayout {
private OnItemClickListener onItemClickListener;
private OnItemDelBtnClickListener onItemDelBtnClickListener;
private OnItemAddBtnClickListener onItemAddBtnClickListener;
public void setOnItemDelBtnClickListener(OnItemDelBtnClickListener onItemDelBtnClickListener){
this.onItemDelBtnClickListener = onItemDelBtnClickListener;
}
public void setOnItemAddBtnClickListener(OnItemAddBtnClickListener onItemAddBtnClickListener){
this.onItemAddBtnClickListener = onItemAddBtnClickListener;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public static interface OnItemClickListener{
public void onItemClick(int position);
}
public static interface OnItemLongClickListener{
public void onItemLongClick(int position);
}
public static interface OnItemDelBtnClickListener{
public void onItemDelClick(int position);
}
public static interface OnItemAddBtnClickListener{
public void onItemAddClick();
}
}
2.通过传递过来的数据改变AddListView的显示
public void setDatas(List<String> datas){
if(datas == null ){
datas = new ArrayList<String>();
}
mDatas = datas;
notifyDataSetChanged();
}
public void notifyDataSetChanged(){
removeAllViews();
if(mDatas == null || mDatas.size() == 0){
return;
}
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for(int i=0; i<mDatas.size(); i++){
final int index = i;
View view = getView(index);
if(view == null){
throw new NullPointerException("listview item layout is null, please check getView()...");
}
addView(view, index, layoutParams);
}
}
private View getView(final int position){
if(layoutInflater == null){
layoutInflater = LayoutInflater.from(getContext());
}
View convertView = layoutInflater.inflate(R.layout.item_comment, null, false);
TextView commentTv = (TextView) convertView.findViewById(R.id.commentTv);
ImageButton delBtn = (ImageButton) convertView.findViewById(R.id.del_imageButton);
final String name = mDatas.get(position);
SpannableStringBuilder builder = new SpannableStringBuilder();
// builder.append(": ");
builder.append(setClickableSpan(name,"处理", null));
commentTv.setText(builder);
//commentTv.setMovementMethod(circleMovementMethod);
delBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (onItemDelBtnClickListener != null){
onItemDelBtnClickListener.onItemDelClick(position);
}
}
});
commentTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListener!=null){
onItemClickListener.onItemClick(position);
}
}
});
return convertView;
}
3.简单使用
- 在xml中添加
<com.example.library.AddListView
android:id="@+id/commentListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:item_color="@color/praise_item">
- 监听操作
addListView = (AddListView) findViewById(R.id.AddListView);
addListDatas = new ArrayList<>();
//添加item
addListDatas.add("item0");
addListView.setDatas(addListDatas);
//删除按钮的监听
addListView.setOnItemDelBtnClickListener(new AddListView.OnItemDelBtnClickListener() {
@Override
public void onItemDelClick(int position) {
if (!addListDatas.isEmpty()){
if (addListDatas.size() > position){
addListDatas.remove(position);
addListView.setDatas(addListDatas);
}
}
}
});
//添加按钮的监听
addListView.setOnItemAddBtnClickListener(new AddListView.OnItemAddBtnClickListener() {
@Override
public void onItemAddClick() {
addListDatas.add("item"+ (++ni));
addListView.setDatas(addListDatas);
}
});
//点击item监听
addListView.setOnItemClickListener(new AddListView.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(getApplicationContext(),position+"",Toast.LENGTH_SHORT).show();
}
});
后记
这只是一个简单的自定义View,源码和详细使用请参考:https://github.com/456chenxigang/AddListView,欢迎大家start和fork ,有不足之处欢迎指正。