在ScrollView中动态添加View

  1. 需求:要求在ScrollView中动态添加View,并且添加的View可以进行缩进。
    这里写图片描述

    这里写图片描述

  2. 思路:一开始的思路是使用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
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章