问题如下:
1.在自定义的ListView中,在一个Item中同时添加图片、文字、单选按钮时很困难。
最简单的解决办法(网上摘录):
把单选按钮用图片表示即可,然后使用SimpleAdapter放进ListView中,代码如下
- simpleAdapter = new SimpleAdapter( this , getData(),
- R.layout.picture_info, new String[] { imageViewIdStr,
- pictureNameIdStr, picturePixelIdStr, radioImageIdStr },
- new int [] { R.id.imageViewId, R.id.pictureNameId,
- R.id.picturePixelId, R.id.radioImageId });
- listView.setOnItemClickListener(new OnListClickListener());
- listView.setAdapter(simpleAdapter);
- //
- private class OnListClickListener implements OnItemClickListener {
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- // TODO Auto-generated method stub
- ChangeRadioImage(selectItemIndex, false );
- ChangeRadioImage(arg2, true );
- selectItemIndex = arg2;
- }
- //主要是下面这个函数用来选中和未选中时交换图片的
- private void ChangeRadioImage( int position, boolean isChangeImage) {
- SimpleAdapter tmpSimpleAdapter = simpleAdapter;
- HashMap<String, Object> hm = (HashMap<String, Object>) simpleAdapter
- .getItem(position);
- if (isChangeImage) {
- hm.put("radioImageId" , R.drawable.pressed);
- } else {
- hm.put("radioImageId" , R.drawable.press);
- }
- tmpSimpleAdapter.notifyDataSetChanged();
- }
2.在ListView中选中一项时,要弹出一个对话框,框中要有动画,这个困扰我很久,其实就是自定义对话框。
解决办法:
(1)使用AnimationDrawable,但是使用它时必须要以事件触发控件才能使图片活动起来,此办法不佳,代码如下
- AlertDialog.Builder alertBuild = new AlertDialog.Builder( this ); //创建对话框构造器
- AlertDialog alertDialog;
- LayoutInflater inflater = LayoutInflater.from(this );
- View layout = inflater.inflate(R.layout.anim_dialog,
- (ViewGroup) findViewById(R.id.layoutId));
- imageAnim = (ImageView) layout.findViewById(R.id.imageAnimId);
- imageAnim.setBackgroundResource(R.drawable.fly01_anim);
- Button btn = (Button) layout.findViewById(R.id.testButtonId);
- btn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- AnimationDrawable anim = (AnimationDrawable) imageAnim
- .getBackground();
- anim.stop();
- anim.start();
- }
- });
- alertBuild.setTitle(pictureName[id]);
- alertBuild.setView(layout);
- alertDialog = alertBuild.create();
- return alertDialog;
- ///anim_dialog.xml
- <? xml version = "1.0" encoding = "utf-8" ?>
- < LinearLayout
- xmlns:android = "http://schemas.android.com/apk/res/android"
- android:id = "@+id/layoutId"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent"
- android:orientation = "vertical" >
- < ImageView
- android:id = "@+id/imageAnimId"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content" />
- < Button
- android:id = "@+id/testButtonId"
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- android:text = "Test" />
- </ LinearLayout >
- //fly01_anim.xml
- <? xml version = "1.0" encoding = "utf-8" ?>
- < animation-list
- xmlns:android = "http://schemas.android.com/apk/res/android"
- android:oneshot = "false" >
- < item android:drawable = "@drawable/fly01001" android:duration = "100" />
- < item android:drawable = "@drawable/fly01002" android:duration = "100" />
- < item android:drawable = "@drawable/fly01003" android:duration = "100" />
- < item android:drawable = "@drawable/fly01004" android:duration = "100" />