Skip to content

Commit 1e5b851

Browse files
committed
支持添加和删除View
1 parent e6a9a8c commit 1e5b851

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

app/src/main/java/com/example/lvruheng/autoflowlayout/AutoFlowLayout.java

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@ public class AutoFlowLayout extends LinearLayout {
5959
* 记录选中的View
6060
*/
6161
private View mSelectedView;
62-
6362
/**
6463
*记录选中的View
6564
*/
6665
private List<View> mCheckedViews = new ArrayList<>();
66+
/**
67+
* 记录展示的数量
68+
*/
69+
private int mDisplayNumbers;
6770
public AutoFlowLayout(Context context) {
6871
super(context);
6972
}
@@ -293,6 +296,70 @@ public void setAllViews(List<View> views) {
293296
}
294297
addView(view);
295298
}
299+
requestLayout();
300+
}
301+
302+
/**
303+
* 添加子view
304+
* @param view
305+
*/
306+
public void addView(View view) {
307+
if (view != null) {
308+
addView(view);
309+
}
310+
requestLayout();
311+
}
312+
313+
/**
314+
* 删除指定索引的view
315+
* @param index true删除成功 false删除失败
316+
* @return
317+
*/
318+
public boolean deleteView(int index) {
319+
if (mCurrentItemIndex != 0) {
320+
mDisplayNumbers = mCurrentItemIndex/2;
321+
if (index > mDisplayNumbers) {
322+
return false;
323+
} else {
324+
removeViewAt(index);
325+
return true;
326+
}
327+
}
328+
return false;
329+
}
330+
331+
/**
332+
* 删除最后一个view
333+
* @return true删除成功 false删除失败
334+
*/
335+
public boolean deleteView() {
336+
if (mCurrentItemIndex != 0) {
337+
mDisplayNumbers = mCurrentItemIndex/2;
338+
removeViewAt(mDisplayNumbers);
339+
return true;
340+
}
341+
return false;
342+
}
343+
344+
/**
345+
* 删除指定范围的所有view
346+
* @param start 开始范围
347+
* @param end 结束范围
348+
* @return
349+
*/
350+
public boolean deleteView(int start, int end) {
351+
if (mCurrentItemIndex != 0) {
352+
mDisplayNumbers = mCurrentItemIndex/2;
353+
if (start < 0) {
354+
start = 0;
355+
}
356+
if (end > mDisplayNumbers) {
357+
end = mDisplayNumbers;
358+
}
359+
removeViews(start,end-start+1);
360+
return true;
361+
}
362+
return false;
296363
}
297364

298365
/**

0 commit comments

Comments
 (0)