簡體   English   中英

如何創建可擴展列表?

[英]How to create an expandable list?

我需要在Android中創建折疊/展開表單。 我正在考慮為此目的使用RelativeLayoutTableLayout 但是,什么XML元素使這些表單擴展並隱藏在android中?

如果你不知道我說的不是,采取像銷售人員的應用的一個例子。 在那里,您可以使用所有形式的這些可擴展菜單。 我怎樣才能做到這一點?

以下是一個示例(摘自Sales Force)

在此處輸入圖片說明

展開它們時,如下圖所示

在此處輸入圖片說明

您可以執行以下操作。 創建具有以下內容的布局:

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執行相同的操作。

希望有幫助!

我遇到了類似的問題,我希望表單中的某些部分被隱藏在sektion中,並為此問題創建了一個類。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM