android中ProgressBar和ListView

  ProgressBar进度条的使用情况:

           进度条的.xml声明:如果不声明格式,则默认格式为转圆圈的形式,声明进度条的visibility为不可见。

      <ProgressBar
        android:id="@+id/firstBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:visibility="gone"/>
      <ProgressBar
        android:id="@+id/secondBar"
        android:layout_below="@id/firstBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

           .java文件控制进度条的代码:

      firstBar.setVisibility(View.VISIBLE);
      secondBar.setVisibility(View.VISIBLE);
      firstBar.setMax(200); //设置进度条的最大值
      firstBar.setProgress(50); //设置第一进度
      firstBar.setSecondaryProgress(60); //设置第二进度
      firstBar.setVisibility(View.GONE); //代码设置不可见

  ListView的使用说明:

    首先为在listviewmain.xml文件中为ListView设置布局方式:

      <LinearLayout android:id="@+id/listLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical">
        <ListView
          android:id="@id/android:list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:drawSelectorOnTop="false"
          android:scrollbars="vertical">
        </ListView>
      </LinearLayout>

    其次需要给ListView中的内容显示格式添加相应的xml文件(user.xml):

      <?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/User_name"
          android:layout_width="180dp"
          android:layout_height="match_parent"
          android:singleLine="true"
          android:textSize="10pt" />

        <TextView
          android:id="@+id/User_id"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:textSize="10pt"
          android:gravity="right"/>
        </LinearLayout>

  在.java文件的onCreate方法中通过ArrayList对象给ListView控件添加内容:

    setContentView(R.layout.listviewmain);
    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
    HashMap<String,String> map1 = new HashMap<String,String>();
    HashMap<String,String> map2 = new HashMap<String,String>();
    HashMap<String,String> map3 = new HashMap<String,String>();
    map1.put("user_name", "zhangsan");
    map1.put("user_ip", "192.168.1.1");
    map2.put("user_name", "lisi");
    map2.put("user_ip", "192.168.1.12");
    map3.put("user_name", "wangwu");
    map3.put("user_ip", "192.168.1.18");
    list.add(map1);
    list.add(map2);
    list.add(map3);
    SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user,
               new String[]{"user_name","user_ip"}, new int[]{R.id.User_name,R.id.User_id});
    setListAdapter(listAdapter);

  通过onListItemClick方法监听到底点击了哪个ListView的哪个View:

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
      // TODO Auto-generated method stub
      //v被点击控件的对象,position被点击控件的位置,id被点击控件的Id
      super.onListItemClick(l, v, position, id);     

      if(id == 0){
        //点击了第一行
      }
    }

 

posted @ 2015-07-19 10:10  best.lei  阅读(740)  评论(0编辑  收藏  举报