Friday, November 27, 2015

Add and Remove view dynamically, another approach

Last post show a example of "Add and Remove view dynamically", each Remove Button have its own OnClickListener object, each OnClickListener object refer to a specified view.


It's another approach: all Remove Buttons share a common OnClickListener object. Inside the OnClickListener, retrieve the associate addView from the trigger view by calling its getParent() method.


MainActivity.java
package com.blogspot.android_er.androiddynamicview;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText textIn;
    Button buttonAdd;
    LinearLayout container;
    TextView reList, info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textIn = (EditText)findViewById(R.id.textin);
        buttonAdd = (Button)findViewById(R.id.add);
        container = (LinearLayout)findViewById(R.id.container);
        reList = (TextView)findViewById(R.id.relist);
        info = (TextView)findViewById(R.id.info);
        info.setMovementMethod(new ScrollingMovementMethod());

        buttonAdd.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater =
                        (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.row, null);
                TextView textOut = (TextView)addView.findViewById(R.id.textout);
                textOut.setText(textIn.getText().toString());
                Button buttonRemove = (Button)addView.findViewById(R.id.remove);
                /*
                final View.OnClickListener thisListener = new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        info.append("thisListener called:\t" + this + "\n");
                        info.append("Remove addView: " + addView + "\n\n");
                        ((LinearLayout)addView.getParent()).removeView(addView);

                        listAllAddView();
                    }
                };

                buttonRemove.setOnClickListener(thisListener);
                container.addView(addView);

                info.append(
                        "thisListener:\t" + thisListener + "\n"
                                + "addView:\t" + addView + "\n\n"
                );
                */

                buttonRemove.setOnClickListener(commonRemoveOnClickListener);
                container.addView(addView);
                info.append(
                        "CommonRemoveOnClickListener:\t" + commonRemoveOnClickListener + "\n"
                                + "addView:\t" + addView + "\n"
                                + "buttonRemove:\t" + buttonRemove + "\n\n");

                        listAllAddView();
            }
        });
    }

    private void listAllAddView(){
        reList.setText("");

        int childCount = container.getChildCount();
        for(int i=0; i<childCount; i++){
            View thisChild = container.getChildAt(i);
            reList.append(thisChild + "\n");
        }
    }

    View.OnClickListener commonRemoveOnClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            info.append("this View clicked:\t" + v + "\n");
            info.append("this Listener called:\t" + this + "\n");
            View thisParentView = (View) v.getParent();
            info.append("Remove thisParentView: " + thisParentView + "\n\n");
            ((LinearLayout)thisParentView.getParent()).removeView(thisParentView);

            listAllAddView();
        }
    };
}


activity_main.xml and row.xml, refer to last post "Add and Remove view dynamically, keep track of child views".

No comments: