listview的item(listview的用法)
你们好,最近小艾特发现有诸多的小伙伴们对于listview的item,listview的用法这个问题都颇为感兴趣的,今天小活为大家梳理了下,一起往下看看吧。
1、main.xml的布局很简单啦,只是一个ExpandableListView 就OK了
2、但值得简单说下的是android:cacheColorHint="#00000000",这个设置可以去除拖动view时背景变成黑色的效果
3、android:listSelector="#00000000" ,可以去除选中时的黄色底色
4、java代码:
5、package com.eyu.activity_test;
6、import android.app.Activity;
7、import android.graphics.Color;
8、import android.os.Bundle;
9、import android.view.Gravity;
10、import android.view.View;
11、import android.view.ViewGroup;
12、import android.view.Window;
13、import android.widget.AbsListView;
14、import android.widget.BaseExpandableListAdapter;
15、import android.widget.ExpandableListAdapter;
16、import android.widget.ExpandableListView;
17、import android.widget.ExpandableListView.OnChildClickListener;
18、import android.widget.ImageView;
19、import android.widget.LinearLayout;
20、import android.widget.TextView;
21、import android.widget.Toast;
22、public class ExpandableList extends Activity{
23、 protected void onCreate(Bundle savedInstanceState) {
24、 // TODO Auto-generated method stub
25、 super.onCreate(savedInstanceState);
26、 requestWindowFeature(Window.FEATURE_NO_TITLE);
27、 setContentView(R.layout.main);
28、 final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
29、 //设置组视图的图片
30、 int[] logos = new int[] { R.drawable.wei, R.drawable.shu,R.drawable.wu};
31、 //设置组视图的显示文字
32、 private String[] generalsTypes = new String[] { "魏", "蜀", "吴" };
33、 //子视图显示文字
34、 private String[][] generals = new String[][] {
35、 { "夏侯惇", "甄姬", "许褚", "郭嘉", "司马懿", "杨修" },
36、 { "马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云" },
37、 { "吕蒙", "陆逊", "孙权", "周瑜", "孙尚香" }
38、 };
39、 //子视图图片
40、 public int[][] generallogos = new int[][] {
41、 { R.drawable.xiahoudun, R.drawable.zhenji,
42、 R.drawable.xuchu, R.drawable.guojia,
43、 R.drawable.simayi, R.drawable.yangxiu },
44、 { R.drawable.machao, R.drawable.zhangfei,
45、 R.drawable.liubei, R.drawable.zhugeliang,
46、 R.drawable.huangyueying, R.drawable.zhaoyun },
47、 { R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,
48、 R.drawable.zhouyu, R.drawable.sunshangxiang } };
49、
50、 //自己定义一个获得文字信息的方法
51、 TextView getTextView() {
52、 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
53、 ViewGroup.LayoutParams.FILL_PARENT, 64);
54、 TextView textView = new TextView(
55、 ExpandableList.this);
56、 textView.setLayoutParams(lp);
57、 textView.setGravity(Gravity.CENTER_VERTICAL);
58、 textView.setPadding(36, 0, 0, 0);
59、 textView.setTextSize(20);
60、 textView.setTextColor(Color.BLACK);
61、 return textView;
62、 }
63、
64、 //重写ExpandableListAdapter中的各个方法
65、 @Override
66、 public int getGroupCount() {
67、 // TODO Auto-generated method stub
68、 return generalsTypes.length;
69、 }
70、 @Override
71、 public Object getGroup(int groupPosition) {
72、 // TODO Auto-generated method stub
73、 return generalsTypes[groupPosition];
74、 }
75、 @Override
76、 public long getGroupId(int groupPosition) {
77、 // TODO Auto-generated method stub
78、 return groupPosition;
79、 }
80、 @Override
81、 public int getChildrenCount(int groupPosition) {
82、 // TODO Auto-generated method stub
83、 return generals[groupPosition].length;
84、 }
85、 @Override
86、 public Object getChild(int groupPosition, int childPosition) {
87、 // TODO Auto-generated method stub
88、 return generals[groupPosition][childPosition];
89、 }
90、 @Override
91、 public long getChildId(int groupPosition, int childPosition) {
92、 // TODO Auto-generated method stub
93、 return childPosition;
94、 }
95、 @Override
96、 public boolean hasStableIds() {
97、 // TODO Auto-generated method stub
98、 return true;
99、 }
100、 @Override
101、 public View getGroupView(int groupPosition, boolean isExpanded,
102、 View convertView, ViewGroup parent) {
103、 // TODO Auto-generated method stub
104、 LinearLayout ll = new LinearLayout(
105、 ExpandableList.this);
106、 ll.setOrientation(0);
107、 ImageView logo = new ImageView(ExpandableList.this);
108、 logo.setImageResource(logos[groupPosition]);
109、 logo.setPadding(50, 0, 0, 0);
110、 ll.addView(logo);
111、 TextView textView = getTextView();
112、 textView.setTextColor(Color.BLACK);
113、 textView.setText(getGroup(groupPosition).toString());
114、 ll.addView(textView);
115、 return ll;
116、 }
117、 @Override
118、 public View getChildView(int groupPosition, int childPosition,
119、 boolean isLastChild, View convertView, ViewGroup parent) {
120、 // TODO Auto-generated method stub
121、 LinearLayout ll = new LinearLayout(
122、 ExpandableList.this);
123、 ll.setOrientation(0);
124、 ImageView generallogo = new ImageView(
125、 ExpandableList.this);
126、 generallogo
127、 .setImageResource(generallogos[groupPosition][childPosition]);
128、 ll.addView(generallogo);
129、 TextView textView = getTextView();
130、 textView.setText(getChild(groupPosition, childPosition)
131、 .toString());
132、 ll.addView(textView);
133、 return ll;
134、 }
135、 @Override
136、 public boolean isChildSelectable(int groupPosition,
137、 int childPosition) {
138、 // TODO Auto-generated method stub
139、 return true;
140、 }
141、 };
142、 ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
143、 expandableListView.setAdapter(adapter);
144、
145、
146、 //设置item点击的监听器
147、 expandableListView.setOnChildClickListener(new OnChildClickListener() {
148、 @Override
149、 public boolean onChildClick(ExpandableListView parent, View v,
150、 int groupPosition, int childPosition, long id) {
151、 Toast.makeText(
152、 ExpandableList.this,
153、 "你点击了" + adapter.getChild(groupPosition, childPosition),
154、 Toast.LENGTH_SHORT).show();
155、 return false;
156、 }
157、 });
158、 }
159、}
以上就是listview的用法这篇文章的一些介绍,希望对大家有所帮助。
免责声明:本答案或内容为用户上传,不代表本网观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。 如遇侵权请及时联系本站删除。