聽起來像你有某種喜好屏幕。如果是這種情況,請考慮使用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));
}