If you need to slide, use RecyclerView. When you click Add, insert data into the adapter and update the update viewRecyclerView,点击新增的时候往adapter里面插入数据然后更新更新视图
If the number is not large, just use LinearLayout, then dynamically add sub-Views when clicked, and then redraw LinearLayout.
This effect has two main components. The first one is the outer container (hereinafter called the parent view) used to place specific subviews. You can use a vertical LinearLayout. The second one is the subview you want to add dynamically. This subview should contain an EditText and a Button. I assume you know how to create a subview🎜🎜
🎜The operation is to click the new button and use the ViewGroup.addView() method to add a subview in the parent view, which is what you call "EditText", and then if the size of the parent view If there is no change, call the invalidate() method of the parent view to redraw the parent view. Then the subview you just added will be displayed, and the addition is successful. If the parent view If the size changes, just call requestLayout(). The deletion operation is the same, that is, when you click the delete button, use ViewGroup.removeView() to delete the corresponding subview, and then Then invalidate()/requestLayout()🎜🎜
🎜
🎜activity layout🎜
rrreee
🎜R.layout.edt file, layout of sub-controls🎜
rrreee
🎜Key code of activity🎜
rrreee
🎜The key code is as above🎜
🎜container is the parent control mentioned above, which is a LinearLayout🎜🎜
🎜R.layout.edt is the layout of the sub-control mentioned above, and the button is R.id.btn🎜🎜
🎜
If you need to slide, use
RecyclerView
. When you click Add, insert data into the adapter and update the update viewRecyclerView
,点击新增的时候往adapter里面插入数据然后更新更新视图如果数量不会很多,直接用
LinearLayout
,然后点击时动态添加子View,然后重绘LinearLayout
就行了这种效果有2个主要部件, 第一个是外层的容器(后面叫父视图)用来放置具体的子视图, 可以用一个竖直的
LinearLayout
. 第二个就是你要动态添加的子视图, 这个子视图应该包含一个EditText
和一个Button
. 我假设你知道怎样创建子视图了操作就是点击新增的按钮就在父视图中使用
ViewGroup.addView()
方法增加一个子视图, 也就是你说的"EditText", 然后如果父视图的大小没有发生变化的话, 就调用父视图的invalidate()
方法重绘父视图, 那么你刚才add进去的子视图就会显示出来了, 也就新增成功了. 如果父视图的大小发生了变化, 就调用requestLayout()
. 删除操作一样, 就是点击删除按钮的时候就用ViewGroup.removeView()
删除对应的子视图, 然后再invalidate()
/requestLayout()
activity的布局
R.layout.edt
文件, 子控件的布局activity的关键代码
关键代码如上
container就是上面说的父控件, 是一个
LinearLayout
If the number is not large, just useR.layout.edt
就是上面说的子控件的布局, 其中的按钮就是R.id.btn
LinearLayout
, then dynamically add sub-Views when clicked, and then redrawLinearLayout
.LinearLayout
. The second one is the subview you want to add dynamically. This subview should contain anEditText
and aButton
. I assume you know how to create a subview🎜🎜ViewGroup.addView()
method to add a subview in the parent view, which is what you call "EditText", and then if the size of the parent view If there is no change, call theinvalidate()
method of the parent view to redraw the parent view. Then the subview you just added will be displayed, and the addition is successful. If the parent view If the size changes, just callrequestLayout()
. The deletion operation is the same, that is, when you click the delete button, useViewGroup.removeView()
to delete the corresponding subview, and then Theninvalidate()
/requestLayout()
🎜🎜 🎜 🎜activity layout🎜 rrreee 🎜R.layout.edt
file, layout of sub-controls🎜 rrreee 🎜Key code of activity🎜 rrreee 🎜The key code is as above🎜LinearLayout
🎜🎜R.layout.edt
is the layout of the sub-control mentioned above, and the button isR.id.btn
🎜🎜 🎜