2013-10-23 115 views
0

我需要在Android中创建Collapse/Expand表单。我正在考虑为此使用RelativeLayoutTableLayout。但是,什么XML元素使这些窗体在android中展开和隐藏?如何创建可扩展列表?

如果您不确定我不是在说什么,请以销售队列为例。在这里你可以在所有表​​单中找到这些可扩展的菜单。我怎样才能做到这一点?

下面是一个例子(从销售人员采取)

enter image description here

当您展开这些,它看起来像下面

enter image description here

+0

你想要可扩展列表视图? –

+0

@Rani:嗨,请看看我更新的问题 –

+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,或第一TextView的消失,使布局2可见和反之亦然。对第二个textview也一样。

希望有帮助。

+0

这就是答案。谢谢 –

+0

快乐编码。 .. !!! –

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