动态添加View和删除View的实例

来源:互联网 发布:软件后门检测工具 编辑:程序博客网 时间:2024/05/27 20:41


上代码,主要代码如下:


// 子布局Item列表
private HashMap<Integer, LinearLayout> conditions = new HashMap<>();


// 途经点Item列表
private ArrayList<LinearLayout> passPoints = new ArrayList<>();



public void addPassPointViewItem(final PoiInfo poiInfo) {
if (null != ll_itemContainer && ll_itemContainer.getChildCount() >= 3) {
ToastUtils.show(mContext, "支持添加三个途经点哦~");
} else {
final LinearLayout ll_passPoints = (LinearLayout) mContext
.getLayoutInflater().inflate(R.layout.add_pass_points_item,null);
// 途经点Item布局中,持有Poi的tag
ll_passPoints.setTag(poiInfo);


final TextView tv_passpoints = (TextView) ll_passPoints
.findViewById(R.id.tv_pass_points);
tv_passpoints.setText(poiInfo.name);
tv_passpoints.setBackgroundColor(mContext.getResources().getColor(
R.color.pass_points_tv_color));


tv_passpoints.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 修改当前选中的POI点
// modifyPassPointFlag = itemId;
poiInfoFlag = (PoiInfo) ll_passPoints.getTag();
//LogUtils.d("Fragment" + " poiInfoFlag = " + poiInfoFlag.name);


onAddPassPointsClicked(); // 调起搜索途经点页面,对当前选中的途经点Item进行修改

if (TextUtils.equals(fromTag, RoutineFragment.Tag)) {
RoutineFragment.addPassPointsClickFlag = true;
} else if (TextUtils.equals(fromTag, RoutineResultFragment.Tag)) {
RoutineResultFragment.addBtnClickFlag = true;
}
dismiss();
}
});


// 删除按钮
final ImageButton btn_remove = (ImageButton) ll_passPoints
.findViewById(R.id.ibn_del_points);
btn_remove.setTag(itemId);
btn_remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 从List中删除途经点的PoiInfo
RoutineResultFragment.delPassPointsPoiInfo(tv_passpoints
.getText().toString());


// 从List中删除途经点的name
RoutineResultFragment.delPassPointsName(tv_passpoints
.getText().toString());


if (TextUtils.equals(fromTag, RoutineFragment.Tag)) {
RoutineFragment routineFragment = (RoutineFragment) mapManager
.getMapMediator().getHostActivity()
.getSupportFragmentManager()
.findFragmentByTag(RoutineFragment.Tag);
routineFragment.setPassPointsContent();
}


// 从LinearLayout容器中删除当前点击到的ViewItem
ll_itemContainer.removeView(ll_passPoints);

// 当3个时,隐藏添加按钮,小于3个时,显示添加按钮
setAddBtnVisible();


// 获取绑定的id
int itemId = (int) v.getTag();


// 集合中删除指定的item对象
conditions.remove(itemId);
}
});


// 把当前对象保存到集合中
conditions.put(itemId, ll_passPoints);


passPoints.add(ll_passPoints);


ll_itemContainer.addView(ll_passPoints);


// 每调用一次id++,防止重复
itemId++;
}
}



/**
* 如果是修改途经点Dialog中的某个途经点,则调用该方法

* @param poiInfo
*/
@SuppressWarnings("rawtypes")
public void modifyPassPointItem(PoiInfo poiInfo) {
Set<Map.Entry<Integer, LinearLayout>> items = conditions.entrySet();
// 转换为迭代器
Iterator<Entry<Integer, LinearLayout>> iter = items.iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
LinearLayout val = (LinearLayout) entry.getValue();
if (val.getTag() == poiInfoFlag) {
//LogUtils.d("Fragment" + " poiInfoFlag.name = " + poiInfoFlag.name + " poiInfo.name = " + poiInfo.name);
TextView modified = (TextView) val.findViewById(R.id.tv_pass_points);
modified.setText(poiInfo.name);

RoutineResultFragment.updatePassPointsPoiInfo(poiInfoFlag.name, poiInfo);

RoutineResultFragment.updatePassPointsName(poiInfoFlag.name, poiInfo);

val.setTag(poiInfo);


poiInfoFlag = null; // 一次有效
}
}
}












原创粉丝点击