PeakGen
PeakGen

Reputation: 23035

How to create an expandable list?

I need to create Collapse / Expand forms in Android. I am thinking about using either RelativeLayout or TableLayout for this purpose. But, what XML element make these forms expand and hide in android?

If you are not sure what I am not talking about, take an application like Sales Force for an example. There you have these expandable menus in all the forms. How can I do this?

Following is an example (taken from Sales Force)

enter image description here

When you expand these, it looks like below

enter image description here

Upvotes: 0

Views: 187

Answers (2)

DSS
DSS

Reputation: 7259

You could do the following. create a layout that has the following:

1. A Heading or a textview with the label contacts
2. Below it a layout that has forms related to it
3. Add another textview below #2 and name it address
4. Add a lyout below #3 .

The layout 2 and 4 will have visibility gone in the first case

When the user taps on layout 1, or the first textview, make layout 2 visible and vice versa. Do the same with the second textview.

Hope that helps.!

Upvotes: 1

2red13
2red13

Reputation: 11227

I have had a similar problem, i want parts of my form to be hidden in sektions and created a class for this issue.

public class section extends LinearLayout{
    public LinearLayout container;
    public Button toggler;
    public section(Context context, String section_name, String section_state) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.flxsection, this);
        container = (LinearLayout) this.findViewById(R.id.container);
        container.setVisibility(section_state.equals("0") ? View.GONE:View.VISIBLE);
        toggler = ((Button)this.findViewById(R.id.section_toggle));
        toggler.setTag(section_state);
        toggler.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                String tag = (String) v.getTag();   
                v.setTag(tag.equals("0") ? "1":"0");
                if(tag.equals("0")){expand(container,false);}else{collapse(container,false);}
                setImage(tag.equals("0"));
            }

        });
        toggler.setText(" " + section_name);
        setImage(section_state.equals("1"));
        setTextSize();
    }

    public void setTextSize(){
        toggler.setTextSize(GV.Style.TextSize);
    }

    public void setImage(boolean open){
        int a = open ? R.drawable.minus_48_white: R.drawable.plus_48_white;
        Drawable img = main.res.getDrawable(a);
        final float scale = main.res.getDisplayMetrics().density;
        int size = (int) (12 * scale + 0.5f);
        img.setBounds(0,0,size,size);
        toggler.setCompoundDrawables(img,null,null,null);
    }

}

the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:focusable="false"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:orientation="vertical" >
<Button
    android:id="@+id/section_toggle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dip"
    android:layout_marginTop="4dip"
    android:background="@drawable/section"
    android:drawableLeft="@drawable/plus_48"
    android:focusable="false"
    android:gravity="left|center_vertical"
    android:padding="6dip"
    android:textAppearance="?android:attr/textAppearanceLargeInverse"
    android:textSize="22dip" />
<LinearLayout 
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:background="@android:color/transparent"
    android:orientation="vertical" >
</LinearLayout>  

Expand and collapse:

public static void expand(final View v,boolean quick) {

    v.requestLayout();
    v.measure(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    final int targtetHeight = v.getMeasuredHeight();

    v.getLayoutParams().height = 0;
    v.getLayoutParams().width = LayoutParams.FILL_PARENT; 
    v.setVisibility(View.VISIBLE);
    if(quick){
        v.getLayoutParams().height =  LayoutParams.WRAP_CONTENT;
        v.requestLayout();

    }else{
          android.view.animation.Animation a = new android.view.animation.Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? LayoutParams.WRAP_CONTENT
                        : (int)(targtetHeight * interpolatedTime);
                v.requestLayout();

            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        int duration  = (int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density);
        if(duration> 500)duration=500;
        a.setDuration(duration);
        //(int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density)
        v.startAnimation(a);
    }

}

public static void collapse(final View v,boolean quick) {
    v.requestLayout();
    final int initialHeight = v.getMeasuredHeight();
    if(quick){
        v.setVisibility(View.GONE);
        v.requestLayout();

    }else{
         android.view.animation.Animation a = new android.view.animation.Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if(interpolatedTime == 1){
                    v.setVisibility(View.GONE);

                }else{

                    v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        int duration  = (int)( initialHeight/ v.getContext().getResources().getDisplayMetrics().density);
        if(duration> 500)duration=500;
        a.setDuration(duration);
        v.startAnimation(a);
    }
}

If if create a form and need a section, i create a instance of this class and add the controls.

You might need to turn the hardware acceleration on in order to get the best performance

edit: Usage is like:

section s = new section(context, section_name, section_state);
s.container.addView([your view 1]); 
s.container.addView([your view 2]); 
s.container.addView([your view 3]); 
//...
form.addView(s);

Upvotes: 0

Related Questions