android ListView和ProgressBar(進(jìn)度條控件)的使用方法
ListView控件的使用:
ListView控件里面裝的是一行一行的數(shù)據(jù),一行中可能有多列,選中一行,則該行的幾列都被選中,同時(shí)可以觸發(fā)一個(gè)事件,這種控件在平時(shí)還是用得很多的。
使用ListView時(shí)主要是要設(shè)置一個(gè)適配器,適配器主要是用來放置一些數(shù)據(jù)。使用起來稍微有些復(fù)雜,這里用的是android自帶的SimpleAdapter,形式如下:
android.widget.SimpleAdapter.SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
由此可以看出函數(shù)的第2個(gè)參數(shù)為一個(gè)list,該list里面存放的是一些hashmap,hashmap是一些映射,里面放的是鍵值對(duì);第3個(gè)參數(shù)為1個(gè)布局文件,即適配器輸出的布局;第4個(gè)參數(shù)為字符數(shù)組,數(shù)組的內(nèi)容為參數(shù)list中map每列的列名;第5個(gè)參數(shù)為整型數(shù)組,其意思為第4個(gè)參數(shù)對(duì)應(yīng)顯示的值的格式,一般為控件。
因?yàn)榈?個(gè)參數(shù)為1個(gè)布局文件,所以我們?cè)摴こ讨形覀冃枰賳为?dú)添加一個(gè)xml文件。
同時(shí)我們要知道設(shè)置ListView的監(jiān)聽器是用onListItemClick()函數(shù)。
另外還需注意的是在java中定義數(shù)組類型并初始化時(shí)中間不需要等號(hào),例如
new String[]{"user_name", "user_birthday"}。
這次實(shí)驗(yàn)的參考的是mars老師的資料.
ListView使用的顯示效果如下:
每次選中一行時(shí)在后臺(tái)會(huì)相應(yīng)的輸出該行的位置和id,依次選中這三行.
后臺(tái)輸出為:
實(shí)驗(yàn)代碼如下:
MainActivity.java:
package com.example.control3;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//建立一個(gè)ArrayList,ArrayList里面放的是Hash表
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>();
//給Hash表中填入鍵值對(duì)
map1.put("user_name", "小紅");
map1.put("user_birthday", "2012_07_30");
map2.put("user_name", "小明");
map2.put("user_birthday", "2012_07_31");
map3.put("user_name", "小冬");
map3.put("user_birthday", "2012_08_01");
list.add(map1);
list.add(map2);
list.add(map3);
//在該activity中創(chuàng)建一個(gè)簡(jiǎn)單的適配器
SimpleAdapter listAdapter = new SimpleAdapter(this, list,
R.layout.activity_user, new String[]{
"user_name", "user_birthday"}, new int[]
{R.id.user_name, R.id.user_birthday});
//載入簡(jiǎn)單適配器
setListAdapter(listAdapter);
}
//ListView監(jiān)聽器響應(yīng)函數(shù)
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
System.out.println("id--------------------" +id);
System.out.println("Position-------------" + position);
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/listLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"
/>
</LinearLayout>
</LinearLayout>
activity_user.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dip"
>
<!--
該布局文件時(shí)水平方向上2個(gè)TextView控件,并設(shè)置了其相應(yīng)的屬性
-->
<TextView
android:id="@+id/user_name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="10pt"
android:singleLine="true"
/>
<TextView
android:id="@+id/user_birthday"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="10pt"
android:gravity="right"
/>
</LinearLayout>
ProgressBar控件的使用:
ProgressBar控件的使用比較簡(jiǎn)單,在android開發(fā)中,其默認(rèn)的形式是1個(gè)圓周型的進(jìn)度條,也就是代表一直在等待,這時(shí)候是看不到實(shí)際上完成的進(jìn)度的。所以在程序中我們可以設(shè)置進(jìn)度條的形狀,比如呈水平或者垂直。界面中有1個(gè)按鈕,一開始時(shí)是看不到進(jìn)度條的,當(dāng)按下按鈕時(shí),會(huì)彈出2個(gè)進(jìn)度條,1個(gè)呈水平狀,一個(gè)還是圓形。且這2個(gè)進(jìn)度條出現(xiàn)在按鈕的上面,為什么會(huì)這樣呢?因?yàn)檫@個(gè)activity布局中,采用的是線性布局,而那2個(gè)進(jìn)度條是放在button的前面,只是一開始沒有設(shè)置顯示出來而已。繼續(xù)按進(jìn)度條時(shí),進(jìn)度條會(huì)前進(jìn)。且這里進(jìn)度條顯示分為主進(jìn)度條顯示和次進(jìn)度條顯示。
實(shí)驗(yàn)的結(jié)果如下:

MainActivity.java:
package com.example.control2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private int i = 0;
private ProgressBar bar1 = null;
private ProgressBar bar2 = null;
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar1 = (ProgressBar)findViewById(R.id.bar1);
bar2 = (ProgressBar)findViewById(R.id.bar2);
button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new MyButtonOnClickListener());
}
class MyButtonOnClickListener implements OnClickListener{
public void onClick(View v) {
if( 0 == i )
{
bar1.setVisibility(View.VISIBLE);
bar2.setVisibility(View.VISIBLE);
}
else if( i < bar1.getMax() )
{
//設(shè)置主進(jìn)度和次進(jìn)度
bar1.setProgress(i);
bar1.setSecondaryProgress(i+10);
}
i+=10;
}
}
}
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/china"
/>
<ProgressBar
android:id="@+id/bar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/bar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
/>
</LinearLayout>
總結(jié):
通過這次實(shí)驗(yàn),對(duì)ListView和ProgressBar這2個(gè)進(jìn)度條的使用有了個(gè)初步的了解,其中ListView進(jìn)度條的使用需要用的到適配器,而視頻器的構(gòu)造稍微有點(diǎn)復(fù)雜,需要對(duì)各個(gè)參數(shù)徹底的了解。
作者:tornadomeet
相關(guān)文章
Android實(shí)現(xiàn)今日頭條訂閱頻道效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)今日頭條訂閱頻道效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android實(shí)現(xiàn)支付寶螞蟻森林水滴浮動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)支付寶螞蟻森林水滴浮動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android使用webView加載html頁(yè)面的詳細(xì)步驟
Android WebView是Android開發(fā)中提供的一種用于顯示網(wǎng)頁(yè)內(nèi)容的組件,它可以加載網(wǎng)頁(yè)的url鏈接,也可以加載本地的html文件,下面這篇文章主要給大家介紹了關(guān)于Android使用webView加載html頁(yè)面的相關(guān)資料,需要的朋友可以參考下2024-06-06
Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù)的操作方法
這篇文章主要介紹了Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù),本次也是介紹了用另外一種方法來實(shí)現(xiàn)RecyclerView高效刷新數(shù)據(jù)的功能,需要的朋友可以參考下2022-10-10
Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
這篇文章主要介紹了Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了改變ExpandableListView的indicator圖標(biāo)相關(guān)步驟與實(shí)現(xiàn)技巧,涉及Android配置文件的修改,需要的朋友可以參考下2016-03-03
Android viewpager在最后一頁(yè)滑動(dòng)之后跳轉(zhuǎn)到主頁(yè)面的實(shí)例代碼
這篇文章主要介紹了Android viewpager在最后一頁(yè)滑動(dòng)之后跳轉(zhuǎn)到主頁(yè)面的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-08-08
Android Activity Results API代替onActivityResul
說到onActivityResult,我們已經(jīng)非常熟悉來,通過在A activity啟動(dòng)B activity并且傳入數(shù)據(jù)到B中,然后在A中通過onActivityResult來接收B中返回的數(shù)據(jù)。在最新的activity-ktx的beta版本中,谷歌已經(jīng)廢棄了onActivityResult2022-09-09

