ping8080 发表于 2013-1-30 04:32:12

处女apk纠结过的技术点<4>

                                                       listView布局自定义和数据填充,以及结合tabhost使用
1,tabhost定义选项
LayoutInflater.from(this).inflate(R.layout.pressure,
    host.getTabContentView(), true);
  // 加载tab选项字体布局
  LayoutInflater inflater = this.getLayoutInflater();
  View view1 = inflater.inflate(R.layout.tabview1, null);
  View view2 = inflater.inflate(R.layout.tabview2, null);
  View view3 = inflater.inflate(R.layout.tabview3, null);
  View view4 = inflater.inflate(R.layout.tabview4, null);
  View view5 = inflater.inflate(R.layout.tabview5, null);
  View view6 = inflater.inflate(R.layout.tabview6, null);//自定义view填充到选项卡中,改变选项卡风格
  host.addTab(host.newTabSpec("7days").setIndicator(view1)
    .setContent(R.id.pressure));
  host.addTab(host.newTabSpec("15days").setIndicator(view2)
    .setContent(R.id.pressure));
  host.addTab(host.newTabSpec("30days").setIndicator(view3)
    .setContent(R.id.pressure));
  host.addTab(host.newTabSpec("90days").setIndicator(view4)
    .setContent(R.id.pressure));
  host.addTab(host.newTabSpec("180days").setIndicator(view5)
    .setContent(R.id.pressure));
  host.addTab(host.newTabSpec("360days").setIndicator(view6)
    .setContent(R.id.pressure));
  setContentView(host);//设置host为主布局
2,在tabhost选项中设置一直线布局为为显示视图,而布局中填充,列表选项和一个listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:id="@+id/pressure">
 <LinearLayout android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:orientation="horizontal">
  <TextView android:layout_width="140dip" android:layout_height="wrap_content"
   android:text="@string/tab1" android:textColor="@color/frontcolor"
   android:textSize="20sp"/>
  <TextView android:layout_width="150dip" android:layout_height="wrap_content"
   android:text="@string/tab2" android:textColor="@color/frontcolor"
   android:textSize="20sp"/>
  <TextView android:layout_width="50dip" android:layout_height="wrap_content"
   android:text="@string/tab3" android:textColor="@color/frontcolor"
   android:textSize="20sp"/>
  <TextView android:layout_width="150dip" android:layout_height="wrap_content"
   android:text="@string/tab4" android:textColor="@color/frontcolor"
   android:textSize="20sp"/>
  <TextView android:layout_width="220dip" android:layout_height="wrap_content"
   android:text="@string/tab5" android:textColor="@color/frontcolor"
   android:textSize="20sp"/>
  <TextView android:layout_width="120dip" android:layout_height="wrap_content"
   android:text="@string/tab6" android:textColor="@color/frontcolor"
   android:textSize="20sp"/>
 </LinearLayout>
 <ListView android:id="@+id/pressureData" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:layout_weight="1" />
</LinearLayout>
 
tab选项卡中view视图布局
 
 
3,设置listview的布局
 
 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:orientation="horizontal">
 <TextView android:id="@+id/tx1"  android:layout_width="140dip"
  android:layout_height="wrap_content" android:text=""
  android:textColor="@color/white" android:textSize="20sp"/>
 <TextView android:id="@+id/tx2" android:layout_width="150dip"
  android:layout_height="wrap_content" android:text=""
  android:textColor="@color/white" android:textSize="20sp"/>
 <TextView android:id="@+id/tx3" android:layout_width="50dip"
  android:layout_height="wrap_content" android:text=""
  android:textColor="@color/white" android:textSize="20sp"/>
 <TextView android:id="@+id/tx4" android:layout_width="150dip"
  android:layout_height="wrap_content" android:text=""
  android:textColor="@color/white" android:textSize="20sp"/>
 <TextView android:id="@+id/tx5" android:layout_width="220dip"
  android:layout_height="wrap_content" android:text=""
  android:textColor="@color/white" android:textSize="20sp"/>
 <TextView android:id="@+id/tx6" android:layout_width="120dip"
  android:layout_height="wrap_content" android:text=""
  android:textColor="@color/white" android:textSize="20sp"/>
</LinearLayout>
主视图下的一个listview中填充的小view
 
4,获取要填充的数据填充到listview中去
 
//自定义适配器
public class PressureAdapter extends BaseAdapter {
  Context mContext;
  JSONArray jsonObj;
  public PressureAdapter(Context mContext, JSONArray jsonObj) {
   super();
   this.mContext = mContext;
   this.jsonObj = jsonObj;
  }
  @Override
  public int getCount() {
   return jsonObj.length();
  }
  @Override
  public Object getItem(int position) {
   return position;
  }
  @Override
  public long getItemId(int position) {
   return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   LayoutInflater mInflater = getLayoutInflater();
   View view = mInflater.inflate(R.layout.pressure_data, null);
   TextView tx1 = (TextView) view.findViewById(R.id.tx1);//获取listview布局中的view子项ID并且赋值
   TextView tx2 = (TextView) view.findViewById(R.id.tx2);
   TextView tx3 = (TextView) view.findViewById(R.id.tx3);
   TextView tx4 = (TextView) view.findViewById(R.id.tx4);
   TextView tx5 = (TextView) view.findViewById(R.id.tx5);
   TextView tx6 = (TextView) view.findViewById(R.id.tx6);
   try {
    tx1.setText(jsonObj.getJSONObject(position).getString(
      "systolic"));
    tx2.setText(jsonObj.getJSONObject(position).getString(
      "diastolic"));
    tx3.setText(jsonObj.getJSONObject(position).getString("pulse"));
    tx4.setText(jsonObj.getJSONObject(position).getString(
      "bodyTemperature"));
    tx5.setText(jsonObj.getJSONObject(position).getString(
      "dateTime"));
    tx6.setText(jsonObj.getJSONObject(position).getString("tcText"));
   } catch (JSONException e) {
    e.printStackTrace();
   }
   return view;
  }
 }
 
附带:
list = (ListView) findViewById(R.id.pressureData);
  host.setCurrentTab(0);  //设置当前被选中选项
  list.setAdapter(createAdapter("7"));
  host.setOnTabChangedListener(new OnTabChangeListener() {
   @Override
   public void onTabChanged(String tabId) {//选项切换事件
    if (host.getCurrentTab() == 0) {         
     list.setAdapter(createAdapter("7"));
    }
    if (host.getCurrentTab() == 1) {         
     list.setAdapter(createAdapter("15"));
    }
    if (host.getCurrentTab() == 2) {       
     list.setAdapter(createAdapter("30"));
    }
    if (host.getCurrentTab() == 3) {     
     list.setAdapter(createAdapter("90"));
    }
    if (host.getCurrentTab() == 4) {     
     list.setAdapter(createAdapter("180"));
    }
    if (host.getCurrentTab() == 5) {
     list.setAdapter(createAdapter("360"));
    }
   }
  });
页: [1]
查看完整版本: 处女apk纠结过的技术点<4>