如何创建可扩展列表?

时间:2013-10-23 11:38:28

标签: java android user-interface android-tablelayout

我需要在Android中创建折叠/展开表单。我正在考虑为此目的使用RelativeLayoutTableLayout。但是,什么XML元素使这些表单扩展并隐藏在android中?

如果您不确定我不是在谈论什么,请选择 Sales Force 等应用程序作为示例。在那里你有所有形式的这些可扩展菜单。我怎么能这样做?

以下是一个例子(摘自Sales Force)

enter image description here

当您展开它们时,它看起来如下

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作。创建具有以下内容的布局:

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 .

第一种情况下,布局2和4将具有可见性

当用户点击布局1或第一个文本视图时,使布局2可见,反之亦然。对第二个textview做同样的事。

希望有所帮助。!

答案 1 :(得分:0)

我遇到了类似的问题,我希望我的表单部分隐藏在sektions中并为此问题创建了一个类。

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);
    }

}

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>  

展开和折叠:

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);
    }
}

如果创建表单并需要一个部分,我创建该类的实例并添加控件。

您可能需要启用硬件加速才能获得最佳性能

编辑: 用法如下:

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);