1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
ArrayList<String> objects =
new
ArrayList<String>();
objects.add(
"aaaaaaaaaaaaaaaaaaa"
);
objects.add(
"bbbbbbbbbbbbbbbbbbb"
);
objects.add(
"ccccccccccccccccccc"
);
//ArrayAdapter只支持显示一个textview
ArrayAdapter adapter =
new
ArrayAdapter(
this
, R.layout.list_item1, objects);
listView.setAdapter(adapter);
}
|
ListView自定义设置:
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
|
public
class
MainActivity
extends
ListActivity
{
private
String[] ss =
new
String[]{
"title1"
,
"content"
,
"content"
,
"title2"
,
"content"
,
"content"
,
"content"
,
"content"
};
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setListAdapter(
new
BaseAdapter()
{
@Override
public
View getView(
int
position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
TextView textView =
null
;
if
(position ==
0
|| position ==
3
)
//判断对应布局
{
textView = (TextView) inflater.inflate(R.layout.mylayout,
null
);
}
else
{
textView = (TextView) inflater.inflate(android.R.layout.simple_list_item_1,
null
);
}
textView.setText(ss[position]);
return
textView;
}
@Override
public
int
getCount()
{
return
ss.length;
}
@Override
public
boolean
areAllItemsEnabled()
//先设置成全部不可操作
{
return
false
;
}
@Override
public
boolean
isEnabled(
int
position)
//设置不可操作的列表项
{
if
(position ==
0
|| position ==
3
)
{
return
false
;
}
else
{
return
true
;
}
}
@Override
public
long
getItemId(
int
position)
{
return
0
;
}
@Override
public
Object getItem(
int
position)
{
return
null
;
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}
|
注:
1.R.layout.list_item1这个layout里只能放textview.
2.ListView相关属性:
1
2
3
4
5
6
7
8
|
/**
* divider 间隔颜色
* dividerheight 间隔高度
* fastscrollenable 快进条滚动可见,内容必须大于三屏
* fastallwaysenable 快进条总是可见
* cachecolorhint 缓存背景,设置为透明,滑动时的背景颜色(设置背景图片用)
* listSelector="@drawable/grouplist_item_bg" 设置行布局被选中时的背景
*/
|
3.获取listview行对象:getAdapter()得到适配器对象
1
2
3
4
5
6
7
8
9
10
|
//测试监听listview
@Override
public
void
onItemClick(AdapterView<?> parent, View view,
int
position,
long
id)
{
PlaylistJson json = (PlaylistJson) mListView_playlist.getAdapter().getItem(position);
if
(
null
!= mBinder)
{
mBinder.play(MyContant.URL_HOST + json.link);
}
}
|
重写适配器里的方法:
1
2
3
4
5
6
7
8
9
10
11
12
|
//适配器里的两个方法设置
@Override
public
long
getItemId(
int
position)
{
return
0
;
}
@Override
public
Object getItem(
int
position)
{
return
mPlaylistJsonList.get(position);
}
|
4.将数据集合保存到adapter内部的方法:
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
|
class
SpecialAdapter
extends
BaseAdapter
{
//将数据集合转移到适配器里
private
ArrayList<SpecialInfo> dataList;
@Override
public
View getView(
int
position, View convertView, ViewGroup parent)
{
if
(convertView ==
null
)
{
LayoutInflater inflater = getActivity().getLayoutInflater();
convertView = inflater.inflate(R.layout.speciallist_item,
null
);
}
ImageView iv_main = ViewHolder.get(convertView, R.id.speciallist_item_image);
TextView tv_title = ViewHolder.get(convertView, R.id.speciallist_item_title);
SpecialInfo data = dataList.get(position);
if
(position ==
0
)
{
layoutView.setVisibility(View.GONE);
}
else
{
layoutView.setVisibility(View.VISIBLE);
}
Bitmap bm = BitmapFactory.decodeResource(getActivity().getResources(), data.icon);
iv_main.setImageBitmap(bm);
tv_title.setText(data.title);
return
convertView;
}
@Override
public
int
getCount()
{
return
dataList ==
null
?
0
: dataList.size();
}
@Override
public
Object getItem(
int
position)
{
return
null
;
}
@Override
public
long
getItemId(
int
position)
{
return
0
;
}
//更新数据
public
void
setDataList(ArrayList<SpecialInfo> list)
{
if
(list !=
null
)
{
dataList = (ArrayList<SpecialInfo>) list.clone();
notifyDataSetChanged();
}
}
//释放数据
public
void
clearDataList()
{
if
(dataList !=
null
)
{
dataList.clear();
}
notifyDataSetChanged();
}
}
|
注:
1.listview滚动:
1
|
getListView().setSelection(
int
);
|
2.listview滑动:
1
|
getListView().smoothScrollToPosition(
int
);
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1201266,如需转载请自行联系原作者