oLimxing 2012-02-20
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayoutandroid:orientation="horizontal"
android:layout_width="wrap_content"android:layout_height="wrap_content">
<Buttonandroid:text="加载ListView"android:id="@+id/Button01"
android:layout_width="wrap_content"android:layout_height="wrap_content">
</Button>
<Buttonandroid:text="加载另外一个页面"android:id="@+id/Button02"
android:layout_width="wrap_content"android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayoutandroid:id="@+id/LinearLayout01"android:background="#FFFFFF"
android:layout_width="fill_parent"android:layout_height="fill_parent"></LinearLayout>
</LinearLayout>
<LinearLayoutandroid:id="@+id/layout"
android:layout_width="fill_parent"android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListViewandroid:id="@+id/ListView01"android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/LinearLayout01"
android:layout_width="fill_parent"android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextViewandroid:text="@+id/TextView01"android:id="@+id/TextView01"
android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView>
</LinearLayout>
<LinearLayoutandroid:id="@+id/hellolayout"
android:layout_width="fill_parent"android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextViewandroid:text="HELLO"
android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView>
</LinearLayout>
importjava.util.ArrayList;
importjava.util.HashMap;
importandroid.content.Context;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importandroid.widget.TextView;
publicclasslistAdapterextendsBaseAdapter{
ArrayList<HashMap<String,Object>>list=newArrayList<HashMap<String,Object>>();
privateLayoutInflaterinflater;
publiclistAdapter(Contextcontex)
{
inflater=LayoutInflater.from(contex);
HashMap<String,Object>map=newHashMap<String,Object>();
for(inti=0;i<10;i++){
map.put("name","例子");
list.add(map);
}
}
@Override
publicintgetCount(){
//TODOAuto-generatedmethodstub
returnlist.size();
}
@Override
publicObjectgetItem(intposition){
//TODOAuto-generatedmethodstub
returnlist.get(position);
}
@Override
publiclonggetItemId(intposition){
//TODOAuto-generatedmethodstub
returnposition;
}
@Override
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
//TODOAuto-generatedmethodstub
finalviewHoldermyHolder;
if(convertView==null){
myHolder=newviewHolder();
convertView=inflater.inflate(R.layout.list_view_row,null);
myHolder.tv=(TextView)convertView.findViewById(R.id.TextView01);
convertView.setTag(myHolder);
}
else
{
myHolder=(viewHolder)convertView.getTag();
}
myHolder.tv.setText(list.get(position).get("name").toString());
returnconvertView;
}
}
点击第一个按钮
点击第二个按钮
importandroid.app.Activity;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
importandroid.widget.ListView;
importandroid.widget.TextView;
publicclassdynaActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finalLayoutInflaterinflater=LayoutInflater.from(this);
Buttonbtn=(Button)findViewById(R.id.Button01);
Buttonbtn2=(Button)findViewById(R.id.Button02);
finalLinearLayoutlin=(LinearLayout)findViewById(R.id.LinearLayout01);
btn.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
LinearLayoutlayout=(LinearLayout)inflater.inflate(
R.layout.listview,null).findViewById(R.id.layout);
ListViewlv=(ListView)layout.getChildAt(0);
lv.setAdapter(newlistAdapter(dynaActivity.this));
lin.removeAllViews();
lin.addView(layout);
}
});
btn2.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
LinearLayoutlayout=(LinearLayout)inflater.inflate(
R.layout.hello,null).findViewById(R.id.hellolayout);
TextViewlv=(TextView)layout.getChildAt(0);
lv.setTextColor(Color.RED);
lin.removeAllViews();
lin.addView(layout);
}
});
}
}