需求:要求在ScrollView中動態添加View,並且添加的View可以進行縮進。
思路:一開始的思路是使用ListView動態添加Item的方法來實現,即當需要新增一個View時,就給適配器新增一條數據,並提醒適配器更新數據,如果ListView沒有嵌套在ScrollView中,並且新增的View不需要縮進,這樣的方法是可以實現這一需求的,但是悲催就悲催在,ListView必須嵌套在ScrollView中(因爲項目中的頁面比圖中的頁面要複雜的多,新增的view只是當前頁面的一小部分),這個方法就行不通了,因爲如果將新增的View進行縮進的話,手機屏幕底部就會出現大量空白。轉換思路,既然在ScrollView中嵌套ListView的方法行不通,可以考慮其他動態添加View的方法。於是想到了動態添加自定義View,即將需要添加的佈局關聯到自定義控件中,在需要新增View的地方將這個自定義控件添加進來。
首先是新增的View的佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_close_item"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="點擊收起"
android:background="#AAAAAA"
android:textColor="#FFFFFF"
android:gravity="center_vertical|right"
android:paddingRight="10dp"
android:layout_marginTop="10dp"/>
<LinearLayout
android:id="@+id/ll_check_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:background="#EEEEEE">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="選項一"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="選項二"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="選項三"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
android:background="#EEEEEE">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新增"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除"
android:layout_marginRight="10dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#AAAAAA"/>
</LinearLayout>
將佈局關聯到自定義的控件中
public class NewItem1 extends LinearLayout implements View.OnClickListener {
private TextView tv_close_item;
private LinearLayout ll_check_box;
private LinearLayout ll_button;
private Button btn_add;
private Button btn_delete;
private Context context;
public NewItem1(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.layout_add_new_view_1,this);//第二個參數必須傳this
initView();
initEvent();
this.context=context;
}
private void initEvent() {
tv_close_item.setOnClickListener(this);
btn_add.setOnClickListener(this);
btn_delete.setOnClickListener(this);
}
private void initView() {
tv_close_item=(TextView)findViewById(R.id.tv_close_item);
ll_check_box=(LinearLayout)findViewById(R.id.ll_check_box);
ll_button=(LinearLayout)findViewById(R.id.ll_button);
btn_add=(Button)findViewById(R.id.btn_add);
btn_delete=(Button)findViewById(R.id.btn_delete);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_close_item:
if (ll_check_box.getVisibility()==VISIBLE){
ll_check_box.setVisibility(GONE);
ll_button.setVisibility(GONE);
}else {
ll_check_box.setVisibility(VISIBLE);
ll_button.setVisibility(VISIBLE);
}
break;
case R.id.btn_add:
addNewItem();
break;
case R.id.btn_delete:
deleteItem();
break;
}
}
//新增Item
private void addNewItem() {
NewItem1 newItem1=new NewItem1(context);
((ViewGroup)getParent()).addView(newItem1);
}
//刪除當前Item
private void deleteItem(){
ViewGroup viewGroup= (ViewGroup) getParent();
int index=viewGroup.indexOfChild(this);
viewGroup.removeViewAt(index);
}
}
下面是ScrollView所在的佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:paddingLeft="10dp"
android:background="#EEEEEE"
android:text="文本"/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="10dp"
android:background="#EEEEEE"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="文本"/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="10dp"
android:background="#EEEEEE"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="文本"/>
<LinearLayout
android:id="@+id/ll_add_new_item_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="提交"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
這裏寫新增View和刪除View的具體操作
public class AddNewItemActivity extends Activity {
private LinearLayout ll_add_new_item_1;
private NewItem1 newViewItem1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_view);
initView();
initData();
}
private void initData() {
ll_add_new_item_1.addView(newViewItem1);
}
private void initView() {
ll_add_new_item_1=(LinearLayout)findViewById(R.id.ll_add_new_item_1);
newViewItem1=new NewItem1(this);//這裏傳this
}
}