转自http://griffinshi.javaeye.com/blog/583783
默认的ListView显示的item 是静态的 即不能改变的
[功能]
但是我们有有这样的需求 比如:当不选中某个item的时候 就显示一下大致信息 而当点开该item 会显示更多信息
有2个办法实现之:
1. ExpandableListActivity
2. BaseAdapter + notifyDataSetChanged()
[前提]
本例显示的大致信息是:星期一到星期天 当单击某个item 会显示更多信息 比如:ImageView TextView ...
写道
因为要显示的内容不是固定的 所以系统默认的Adapter肯定是不行的 只能 extends BaseAdapter
[代码 步骤]
1. 准备一些始终显示的内容 本例为:
- String[] week = new String[] {
- "January ","February ","March ","April ",
- "May","June","July","August","September","October","November","December "
- };
2. 定义 CustomItemAdapter extends BaseAdapter
- public class CustomItemAdapter extends BaseAdapter {
- Activity activity;
- public CustomItemAdapter(Activity a){
- activity = a;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- return null;
- }
- }
3. 填充CustomItemAdapter的各个method
写道
其中 关键是 getView() 实现 要求:
1. 星期TextView 是始终要显示的
2. 当某项item 被单击 要显示更多细节
所以 我打算如下做:
1. 定义 View addDefaultViewByPosition(int i) 用于星期的显示
2. 定义 View addCustomViewByPosition(int i) 用于更多细节的显示
1. 星期TextView 是始终要显示的
2. 当某项item 被单击 要显示更多细节
所以 我打算如下做:
1. 定义 View addDefaultViewByPosition(int i) 用于星期的显示
2. 定义 View addCustomViewByPosition(int i) 用于更多细节的显示
- public View addDefaultViewByPosition(int i){
- TextView tv = new TextView(activity);
- tv.setText(week[i]);
- tv.setTextColor(Color.RED);
- tv.setGravity(Gravity.CENTER_HORIZONTAL);
- return tv;
- }
- public View addCustomViewByPosition(int i){
- View view = new View(activity);
- switch(i){
- case 0:
- ImageView iv0 = new ImageView(activity);
- iv0.setImageResource(R.drawable.icon);
- view = iv0;
- break;
- case 1 :
- RadioGroup group1 = new RadioGroup(activity);
- group1.setOrientation(LinearLayout.HORIZONTAL);
- RadioButton radio1 = new RadioButton(activity);
- radio1.setText("Radio 1");
- group1.addView(radio1);
- RadioButton radio2 = new RadioButton(activity);
- radio2.setText("Radio 2");
- group1.addView(radio2);
- view = group1;
- break;
- case 2:
- TextView tv = new TextView(activity);
- tv.setText("Hello to Gryphone!");
- tv.setTextSize(20);
- view = tv;
- break;
- }
- return view;
- }
4. 通过 LinearLayout 把 addDefaultViewByPosition() 与 addCustomViewByPosition() 把二者以 LinearLayout.VERTICAL 方式组织起来
- public View getView(int arg0, View arg1, ViewGroup arg2) {
- // TODO Auto-generated method stub
- LinearLayout layout = new LinearLayout(activity);
- layout.setOrientation(LinearLayout.VERTICAL);
- layout.addView(addDefaultViewByPosition(arg0));
- if(ID == arg0){
- layout.addView(addCustomViewByPosition(arg0));
- }
- return layout;
- }
5. 上面用到的变量 ID 用来代表你单击的item的id
- list.setOnItemClickListener(new OnItemClickListener(){
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- // TODO Auto-generated method stub
- ID = arg2;
- adapter.notifyDataSetChanged();
- }
- });
6. emulator 运行截图:
* 无任何单击的截图:
* 单击item 1 的截图 并与 addCustomViewByPosition() 里的界面代码比较一下
* 单击item 2 的截图 并与 addCustomViewByPosition() 里的界面代码比较一下