必威体育Betway必威体育官网
当前位置:首页 > IT技术

android基础--ListView 详解

时间:2019-09-26 18:14:27来源:IT技术作者:seo实验室小编阅读:79次「手机版」
 

android listview

1.简单的listview

      在List列表中如果不存在过于复杂的东西 我们可以直接去new arrayadapter() 来绘制列表,无须继承ArrayAdapter,重写它的方法。但是如果列表中过于复杂的话就需要使用自定义布局来实现List列表。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public class SimpleList extends ListActivity {

private String[] mListStr = {"姓名:某某某","性别:男","年龄:18","居住地:北京","邮箱:xxxxxxxxx@gmail.com"};

ListView mListView = null;

@Override

protected void onCreate(Bundle savedinstanceState)

{

mListView = getListView();

setListAdapter(new ArrayAdapter<String>(this,Android.R.layout.simple_list_item_1, mListStr));

mListView.setOnItemClickListener(new OnItemClickListener()

{

@Override

public void onItemClick(AdapterView<?> adapterView, View view, int position,long id)

{

   toast.maketext(SimpleList.this,"您选择了" + mListStr[position], Toast.LENGTH_LONG).show();

}

});

super.onCreate(savedInstanceState);

}

}

2.带标题的ListView列表

使用 simpleAdapter 需要注意的是须要用Map<String,Object> item 来保存列表中每一项的显示的title与text , new SimpleAdapter的时候将map中的数据写入 ,程序就会帮我们绘制列表了。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

public class TitleList extends ListActivity

{

private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"};

private String[] mListStr = { "某某某", "女", "18", "北京","[email protected]" };

ListView mListView = null;

ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();

@Override

protected void onCreate(Bundle savedInstanceState)

{

mListView = getListView();

int lengh = mListTitle.length;

for(int i =0; i < lengh; i++)

{

Map<String,Object> item = new HashMap<String,Object>();

item.put("title", mListTitle[i]);

item.put("text", mListStr[i]);

mData.add(item);

}

SimpleAdapter adapter = new SimpleAdapter(this,mData,android.R.layout.simple_list_item_2,

new String[]{"title","text"},new int[]{android.R.id.text1,android.R.id.text2});

setListAdapter(adapter);

mListView.setOnItemClickListener(new OnItemClickListener()

{

@Override

public void onItemClick(AdapterView<?> adapterView, View view, int position,long id)

{

   Toast.makeText(TitleList.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show();

}

});

super.onCreate(savedInstanceState);

}

3.带图片的ListView列表

使用 simpleAdapter 来操作 但是构造simpleAdapter的时候须要使用我们自己写的布局来完成 ,因为系统的布局已经不能满足需求了,同样Map<String,Object> item 来保存列表中每一项须要的显示内容 如 图片 标题 内容等。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?xml version="1.0" encoding="utf-8"?>

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight">

<ImageView

android:id="@+id/image"

android:layout_width="wrap_content" android:layout_height="match_parent"

android:layout_alignParentTop="true" android:layout_alignParentBottom="true"

android:adjustViewBounds="true"

android:padding="2dip" />

<TextView

android:id="@+id/title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/image"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:layout_above="@+id/text"

android:layout_alignWithParentIfMissing="true"

android:gravity="center_vertical"

android:textSize="15dip" />

<TextView

android:id="@+id/text"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/image"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:singleLine="true"

android:ellipsize="marquee"

android:textSize="20dip" />

</RelativeLayout>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

public class IconList extends ListActivity

{

private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"};

private String[] mListStr = { "某某某", "男", "30", "北京","[email protected]" };

ListView mListView = null;

ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();

@Override

protected void onCreate(Bundle savedInstanceState)

{

mListView = getListView();

int lengh = mListTitle.length;

for(int i =0; i < lengh; i++)

{

  Map<String,Object> item = new HashMap<String,Object>();

    item.put("image", R.drawable.jay);

  item.put("title", mListTitle[i]);

  item.put("text", mListStr[i]);

  mData.add(item);

}

SimpleAdapter adapter = new SimpleAdapter(this,mData,R.layout.iconlist,

new String[]{"image","title","text"},new int[]{R.id.image,R.id.title,R.id.text});

setListAdapter(adapter);

mListView.setOnItemClickListener(new OnItemClickListener()

{

  @Override

  public void onItemClick(AdapterView<?> adapterView, View view, int position,long id)

    {

        Toast.makeText(IconList.this,"您选择了标题:" + mListTitle[position] + "内容:"+mListStr[position], Toast.LENGTH_LONG).show();

  }

});

super.onCreate(savedInstanceState);

}

}

4.自定义布局BaseAdapter修改列表颜色

      因为通过直接 构造系统的布局来绘制列表方法肯定是有限的,所以我们需要重写绘制方法 ,写一个类去继承BaseAdapter 并实现这个类中的方法,listView在一开始绘制的时候首先会调用getCout()方法得到绘制次数 ,然后会实例化自己定义的BaseAdapter通过getView()方法一层一层绘制ListView,所以我们可以在这里面根据position(当前绘制的ID)来任意的修改绘制的内容,做出好看漂亮的ListView,下面这个例子我通过重写getView修改每个列表的颜色 并且实现用户选中后成高亮状态。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<ImageView

android:id="@+id/color_image"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_alignParentTop="true"

android:layout_alignParentBottom="true"

android:adjustViewBounds="true"

android:padding="2dip" />

<TextView

android:id="@+id/color_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/color_image"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:singleLine="true"

android:ellipsize="marquee"

android:textSize="15dip"  />

<TextView

android:id="@+id/color_text"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/color_image"

android:layout_below="@+id/color_title"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:singleLine="true"

android:ellipsize="marquee"

android:textSize="20dip" />

</RelativeLayout>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

public class ColorList extends ListActivity {

private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"};

private String[] mListStr = { "某某某", "男", "25", "北京","[email protected]" };

ListView mListView = null;

MyListAdapter myAdapter = null;

@Override

protected void onCreate(Bundle savedInstanceState)

{

mListView = getListView();

myAdapter = new MyListAdapter(this);

setListAdapter(myAdapter);

mListView.setOnItemClickListener(new OnItemClickListener()

{

  @Override

  public void onItemClick(AdapterView<?> adapterView, View view, int position,long id)

    {

        View v=adapterView.getChildAt(position);

        v.setbackgroundcolor(Color.RED);

        Toast.makeText(ColorList.this,"您选择了" + mListStr[position], Toast.LENGTH_LONG).show();

  }

});

super.onCreate(savedInstanceState);

}

class MyListAdapter extends BaseAdapter

{

private int[] colors = new int[] { 0xff626569, 0xff4f5257 };

public MyListAdapter(context context)

{

  mContext = context;

}

public int getCount()

{

  return mListStr.length;

}

@Override

public boolean areAllItemsEnabled()

{

  return false;

}

public Object getItem(int position)

{

  return position;

}

public long getItemId(int position)

{

return position;

}

public View getView(int position, View convertView, ViewGroup parent)

{

ImageView iamge = null;

TextView title = null;

TextView text = null;

if (convertView == null)

{

    convertView = LayoutInflater.from(mContext).inflate(R.layout.colorlist, null);

    iamge = (ImageView) convertView.findViewById(R.id.color_image);

    title =(TextView) convertView.findViewById(R.id.color_title);

    text= (TextView) convertView.findViewById(R.id.color_text);

}

int colorPos = position % colors.length;

convertView.setBackgroundColor(colors[colorPos]);

title.setText(mListTitle[position]);

text.setText(mListStr[position]);

    iamge.setImageResource(R.drawable.jay);

return convertView;

}

private Context mContext;

}

}

5.自定义布局ArrayAdapter 

       ArrayAdapter是BaseAdapter的子类,ArrayAdapter不仅具有BaseAdapter的所有方法还自定义了一些新的方法来处理列表项,所以单纯的从功能能上来讲ArrayAdapter远远强与BaseAdapter,如果是绘制一些数量比较少的列表建议使用BaseAdapter 如果绘制一些比较复杂的列表项 并且列表项很多的 建议使用ArrayAdapter。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<Button

android:id="@+id/array_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="一个按钮"

/>

<ImageView

android:id="@+id/array_image"

android:layout_toRightOf="@+id/array_button"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_alignParentTop="true"

android:layout_alignParentBottom="true"

android:adjustViewBounds="true"

android:padding="2dip" />

<TextView

android:id="@+id/array_title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/array_image"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true" android:singleLine="true"

android:ellipsize="marquee"

android:textSize="15dip"  />

<TextView

android:id="@+id/array_text"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/array_image"

android:layout_below="@+id/array_title"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:singleLine="true"

android:ellipsize="marquee"

android:textSize="20dip" />

</RelativeLayout>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

public class ArrayList extends ListActivity {

private String[] mListTitle = { "姓名", "性别", "年龄", "居住地","邮箱"};

private String[] mListStr = { "某某某", "男", "20", "北京",

"[email protected]" };

ListView mListView = null;

MyListAdapter myAdapter = null;

ArrayList arrayList = null;

@Override

protected void onCreate(Bundle savedInstanceState)

{

arrayList = this;

mListView = getListView();

myAdapter = new MyListAdapter(this,R.layout.arraylist);

setListAdapter(myAdapter);

super.onCreate(savedInstanceState);

}

public class MyListAdapter extends ArrayAdapter<Object>

{

int mTextViewResourceID = 0;

private Context mContext;

public MyListAdapter(Context context, int textViewResourceId)

{

  super(context, textViewResourceId);

  mTextViewResourceID = textViewResourceId;

  mContext = context;

}

private int[] colors = new int[] { 0xff626569, 0xff4f5257 };

public int getCount()

{

  return mListStr.length;

}

@Override

public boolean areAllItemsEnabled()

{

  return false;

}

public Object getItem(int position)

{

  return position;

}

public long getItemId(int position)

{

  return position;

}

public View getView(final int position, View convertView, ViewGroup parent)

{

  ImageView iamge = null;

  TextView title = null;

    TextView text = null;

    Button button = null;

    if (convertView == null)

    {

        convertView = LayoutInflater.from(mContext).inflate(mTextViewResourceID, null);

        iamge = (ImageView) convertView.findViewById(R.id.array_image);

        title = (TextView) convertView.findViewById(R.id.array_title);

        text = (TextView) convertView.findViewById(R.id.array_text);

        button = (Button)convertView.findViewById(R.id.array_button);

        button.setOnClickListener(new OnClickListener()

        {

            @Override

            public void onClick(View arg0)

            {

                Toast.makeText(arrayList,"您点击的第"+position +"个按钮", Toast.LENGTH_LONG).show();

            }

        });

  }

  int colorPos = position % colors.length;

    convertView.setBackgroundColor(colors[colorPos]);

    title.setText(mListTitle[position]);

    text.setText(mListStr[position]);

    if(colorPos == 0)

        iamge.setImageResource(R.drawable.jay);

    else

        iamge.setImageResource(R.drawable.image);

    return convertView;

}

}

}

下载地址:http://vdisk.weibo.com/s/a9mg0

  • 本文固定链接: https://www.xuanyusong.com/archives/91
  • 转载请注明: 雨松MOMO 2012年04月25日 于 雨松MOMO程序研究院 发表

相关阅读

如何注册淘宝账号?注册流程步骤详解

步骤一、账户未登录情况下,点击淘宝网首页左上角【免费注册】。步骤二、根据页面提示输入手机接收验证码验证。若页面提示手机账户

create table ,create table as 与create like三种建

Hive的hql是基于sql而来,而sql中关于表的创建有几种方式。同样,hive也支持这些表的创建方式。 官网文档关于建表的地址:hive常见建表

Android使用AttributeSet自定义控件的方法

当系统提供的控件,无法满足我们的需求的时候,我们往往会 想到自定义控件,通过继承View来实现。当我们想像 系统提供的控件那样可以如

java中Map及Map.Entry详解

Map是java中的接口,Map.Entry是Map的一个内部接口。Map提供了一些常用方法,如keySet()、entrySet()等方法。keySet()方法返回值是Ma

adb操作命令详解及大全

转自:https://blog.csdn.net/janronehoo/article/details/6863772adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作

分享到:

栏目导航

推荐阅读

热门阅读