大家好存谎,菜雞作者又來了拔疚,今天這篇文章,是分享下作者自己做過的一個需求既荚,以免自己以后忘記。
話不多說栋艳,直接進(jìn)入整題恰聘。
首先我們得先創(chuàng)建一個動態(tài)添加的item布局,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/item_tv"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="我是第一條第一個"/>
<EditText
android:id="@+id/item_et"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/item_et1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/item_tv1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="我是第一條第二個"/>
</LinearLayout>
</LinearLayout>
然后再來看看主布局的代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xjx.addview.MainActivity">
<LinearLayout
android:id="@+id/main_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<Button
android:id="@+id/main_btn_add"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加View"/>
<Button
android:id="@+id/main_btn_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="獲取所有Edit的值"/>
</LinearLayout>
布局文件寫完后,接下來就是java代碼了晴叨,動態(tài)添加addView的方法凿宾,
//動態(tài)添加一個item的方法
private void addItemView() {
mView = new View(MainActivity.this);
mView = View.inflate(this, R.layout.item_main, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
mTextView1 = mView.findViewById(R.id.item_tv);
mEditText1 = mView.findViewById(R.id.item_et);
mTextView2 = mView.findViewById(R.id.item_tv1);
mEditText2 = mView.findViewById(R.id.item_et1);
mLinearLayout.addView(mView, params);
//給當(dāng)前添加的View設(shè)置一個tag,這一步很重要。如果沒有這個tag兼蕊,我們后面無法取出view
mView.setTag(position);
//將tag和當(dāng)前生成的view存入集合
mapView.put(position, mView);
//讓tag自增一下初厚,因為tag在這還承擔(dān)著當(dāng)前view序號的作用
position++;
mTextView1.setText("我是第" + position + "條" + "第1個TextView");
mTextView2.setText("我是第" + position + "條" + "第2個TextView");
//設(shè)置長按刪除
setLongDelete();
}
在這個方法里,我們要做的就是通過和用戶交互孙技,動態(tài)的添加一條item产禾,代碼里的注釋已經(jīng)說的很詳細(xì)了,(畢竟作者的文章就是給菜鳥新手程序猿看的牵啦,大神可以略過亚情。。哈雏。)
長按刪除當(dāng)前item的方法
private void setLongDelete() {
mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View view) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("提示")
.setMessage("確定要刪除嗎楞件?")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//根據(jù)點擊的當(dāng)前View,獲取上面存儲的tag裳瘪,這個view就是onLongClick(View view)里的這個view土浸,直接拿來用,他可以判斷出當(dāng)前點擊的哪一個view
int positionView = (int) view.getTag();
//通過map集合把剛獲得的tag放進(jìn)去彭羹,從而獲取到當(dāng)前要刪除的view
mLinearLayout.removeView(mapView.get(positionView));
//移除掉這個tag
mapView.remove(positionView);
//當(dāng)map集合刪除掉最后一條時黄伊,會初始化tag和view
if (mapView.size() == 0) {
position = 0;
editText.clear();
mLinearLayout.removeAllViews();
}
Toast.makeText(getApplicationContext(), "刪除成功", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
}).show();
return false;
}
});
}
其中這個方法中,作者認(rèn)為較為不好理解的就是 int positionView = (int) view.getTag();這句代碼皆怕,因為水平太菜毅舆,當(dāng)初這里想不通,作者也是看了下這里的源碼才理解的愈腾,(大家可以看看源碼憋活,有助于理解)這句代碼只要大家理解了,以后再做類似的功能就是手到擒來了虱黄。
接下來就是要獲取用戶給添加的item中EditText輸入的值的問題了悦即。
這個問題也很簡單,大家只要搞清楚布局.getChildCount()
這個方法就行了橱乱,其中布局可以是任意的布局辜梳,線性,相對泳叠,幀作瞄。他的方法的意思就是獲取當(dāng)前布局內(nèi)有多少個一級子布局,(只能獲取它自身的子布局危纫,并不能獲取它子布局的子布局)宗挥,一句話乌庶,他只能知道他有多少個兒子,但是卻不能知道他兒子有幾個兒子(當(dāng)爺爺?shù)牟荒苤缹O子有幾個契耿。瞒大。。)代碼如下:
private boolean getEditItem() {
//getChildCount()此方法可以獲取到當(dāng)前的linearLayout里含有多少個view
for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
//將每一個view遍歷出來搪桂,然后findViewById后透敌,就可以獲取EditText的值
View childAtView = mLinearLayout.getChildAt(i);
mEditText1 = childAtView.findViewById(R.id.item_et);
mEditText2 = childAtView.findViewById(R.id.item_et1);
if (!mEditText1.getText().toString().equals("") && !mEditText2.getText().toString().equals("")) {
editText.add(mEditText1.getText().toString());
editText.add(mEditText2.getText().toString());
} else {
Toast.makeText(this, "第" + (i + 1) + "個item的EditText沒有完整填寫", Toast.LENGTH_SHORT).show();
return false;
}
}
return true;
}
此處我用的是一個boolean類型的返回值,項目后面會用到踢械,不想要的同學(xué)酗电,把boolean改掉就行了。好了這個項目就算完結(jié)了裸燎。下次再見顾瞻。
本文源碼地址:https://github.com/reverseAndroid/simple-addView