2
我有一個應用程序,顯示的細節項目列表。當用戶選擇該項目時,該應用程序會顯示額外的細節。我創建了兩個Views,其中一個用於擴展RelativeLayout
,一個用於listView,另一個用於詳細視圖並添加到相同的父級佈局。這裏是我的XML和視圖的示例代碼。如何有效地添加/刪除視圖?
查看與ListView控件與細節
class MyView2 extends RelativeLayout {
private Context context = null;
public MyView2(Context context) {
super(context);
this.context = context;
createView();
}
private void createView() {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.layout2s, null);
this.addView(view);
backbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyView2 .this.removeAllViews();
MyView2 .this.addView(new MyView1(context));
}
});
}
}
class MyView1 extends RelativeLayout {
private Context context = null;
public MyView1 (Context context) {
super(context);
this.context = context;
createView();
}
private void createView() {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.layout1, null);
this.addView(view);
listview = (ListView) findViewById(R.id.listView);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Object obj = list.get(position);
MyView1.this.removeAllViews();
MyView1.this.addView(new MyView2(context));
}
});
//Set Adapter
}
}
查看我添加了這兩種觀點的框架佈局。我的問題是,添加/刪除視圖時會降低應用程序的速度。我怎樣才能更有效地添加和刪除它們?
無論如何,它不會加速應用程序。 –
其他建議是在creatView()方法(例如,在onCreate。就時間而言,充氣是非常昂貴的功能 – Tobrun