简体   繁体   中英

Store views in a buffer and show them later

I want to put views in something like a buffer and then show them later. Something like:

public void addTextView(int belowId, int id,String text){
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.BELOW, belowId);
    lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    TextView tv = new TextView(this);
    tv.setId(id);

    tv.setText(text+"\n");

    ///i mean something like this:
    buffer = addview(tv,lp)

}

and later show it

public void showview(bufferview b){
        RelativeLayout rSub= (RelativeLayout) findViewById(R.id.rSub);
        rsub.addview(b);
}

How about using a Map ?

http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

Map<Integer,TextView> textViewMap = new HashMap<Integer,TextView>();

public void addTextView(int belowId, int id,String text){
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.BELOW, belowId);
    lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    TextView tv = new TextView(this);
    tv.setLayoutParams(lp);
    tv.setId(id);

    tv.setText(text+"\n");

    textViewMap.put(tv.getId(), tv);
}

public void showview(int id){
    RelativeLayout rSub= (RelativeLayout) findViewById(R.id.rSub);
    rsub.addview(textViewMap.get(id));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM