我有一个应用程序,它显示带有详细信息的项目列表。当用户选择项目时,应用程序会显示额外的详细信息。我创建了两个扩展视图RelativeLayout
,一个用于 listView,另一个用于详细视图,并将两者添加到同一个父布局中。这是我的 xml 和视图的示例代码。
使用 ListView 查看
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
}
}
查看详细信息
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));
}
});
}
}
我将这两个视图添加到框架布局中。我的问题是,当像这样添加/删除视图时会减慢应用程序的速度。如何更有效地添加和删除它们?