2013-05-15 63 views
0

在我的應用程序中,我必須生成一個包含大量元素的大表單(一個元素通常是和label + attribute(edditext,spinner,checkbox,...))。在Android中實現大型表單佈局的最佳方式是什麼?

當一個表單包含更多元素(> 20)時,應用程序開始滯後,通過示例打開鍵盤大約需要1-2秒或滾動速度很慢。

我試圖

  1. 目前的每個元素是從XML膨脹(有一個TextView和另一種組分的LinearLayout)中,加入以形成佈局。我試圖「重新使用」(實際上是重新添加)一個元素,但是我得到了異常,並說該視圖已經有了一個有意義的父項,因爲我已經添加到了我的表單佈局中。

  2. 我試圖添加ListView中的所有元素,以確保視圖被重用。 這是工作,但不是很好:在這種情況下的問題是,如果我有一個EditText,用戶輸入一些文本和滾動到列表的底部,我找不到一種方法來保存從EditText的文本,所以我將在EditText中添加,當元素在ListView中再次可見時(用戶滾動回到我的元素)。

問題

你知道任何方式使用大量的UI組件,但同時要保持良好的應用程序性能?你認爲可以在用戶開始滾動之前從EditText獲取文本嗎?

謝謝。

回答

2

聽起來像你有某種喜好屏幕。如果是這種情況,請考慮使用PreferenceActivity。如果這樣做不起作用,ListView怎麼樣,它有內置的重用佈局的方法?如果這兩個選項都不起作用,您可以創建自己的ViewAdapter,其工作方式與ListView非常相似。只需創建一個課程 - 如MyViewAdapter。它應該在的參數來幫助進行配置將是什麼樣子 - 如標籤文字和什麼其他元素是:

public class MyViewAdapter { 

    public static enum InputType { 
     editor, spinner, checkbox 
    }; 

    private CharSequence labelText; 
    private InputType input; 

    public MyViewAdapter(CharSequence text, InputType inputType) { 
     this.labelText = text; 
     this.input = inputType; 
    } 

} 

至於知道什麼文本里面你EditText - 只需使用TextWatcher聽更改到文本。雖然你可以在滾動回調之前和之後獲得,但它需要破解內置的ScrollView。這個例子被添加到下面的代碼中。

下,添加一個名爲getView(Context)方法如下:

public View getView(Context context) { 
    //here is where you inflate your view. For example: 
    LinearLayout ll = new LinearLayout(context); 
    ll.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    ll.setOrientation(LinearLayout.VERTICAL); 

    TextView tv = new TextView(context); 
    tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    tv.setText(labelText); 
    ll.addView(tv); 

    switch (input) { 
     case (InputType.spinner) : { 
      //TODO (see editor example) 
      break; 
     } 
     case (InputType.checkbox) : { 
      //TODO (see editor example) 
      break; 
     } 
     default ://default to editor 
     case (InputType.editor) : { 
      EditText et = new EditText(context); 
      et.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
      et.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
        // TODO Auto-generated method stub 
       } 

       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after){ 
        // TODO Auto-generated method stub 
       } 

       @Override 
       public void afterTextChanged(Editable s) { 
        // TODO Auto-generated method stub 
       } 
      }); 
      ll.addView(et); 
      break; 
     } 
    } 

    return ll; 
} 

最後,使用這種ViewAdapter,在您的主要活動中創建它們:

List<MyViewAdapter> adapters = new ArrayList<MyViewAdapter>();//easy way to manage these. 

//for each adapter, do something like: 
adapters.add(new MyViewAdapter("myText:", InputType.editor)); 

然後,膨脹的意見,做:

//Let's say your Main View is a LinearLayout called myView. 
for (MyViewAdapter adapter : adapters) { 
    myView.addView(adapter.getView(context)); 
} 
相關問題