ListView中动态更改控件属性
来源:互联网 发布:centos desktop 编辑:程序博客网 时间:2024/05/23 02:02
前几天做项目,要实现一个设置字体属性的功能。其实这个做起来挺简单的,无非就是使用一个popWindow,在里面设置几个RadioGroup,RadioButton,实现选择,就行了。
是的,思路就是这样,不过在更改界面的字体属性时,我遇到了一个问题就是ListView中的TextView的字体属性,没有更改。
在网上查了许多的资料,最后终于找到了解决的方法。我们都知道BaseAdapter中有一个getView()方法。本来直接构造一个类,让它继承BaseAdapter,重写getView()方法就可以了。但是我的程序中,ListView包括ImageView和TextView,i且这个ImageView中的图像是动态生成的视频压缩图。我当时用了SimpleAdapter的setViewBinder()方法来处理图像的。
而SimpleAdapter是BaseAdapter的子类,所以我选择使用SimpleAdapter做为基类。具体方法如下:
public class MySimpleAdapter extends SimpleAdapter {Context context ;List<? extends Map<String, ?>> data;int resource;String[] from ;int[] to;LayoutInflater inflater = null ;//LayoutInflater 的作用类似于findViewById(),不过他是加载res/layout/***。xml文件,并实例化, //而findViewById()则是加载当前布局中的某些控件 //我们可以使用LayoutInflater.inflater来加载一个未被加载及我们想要加载的布局。private class ViewHolder{TextView textview;ImageView imageview;}public MySimpleAdapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from,int[] to) {super(context, data, resource, from, to);// TODO Auto-generated constructor stub this.context = context; this.data = data; this.resource = resource; this.from = from; this.to = to; inflater = LayoutInflater.from(context); //实例化这个控件}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { String str_pattern = null; int size = 18; String str_style = null ; String str_color = null; ViewHolder viewholder;//ViewHolder可以使ListView的性能优化, 使用View 的findViewById()方法,加载太耗费性能,所以使用view的getTag()方法,比较方便。 SharedPreferences share = context.getSharedPreferences("jkwords", Activity.MODE_PRIVATE );//使用SharedPreferences来存储数据。 if( convertView == null ){viewholder = new ViewHolder( );convertView = inflater.inflate(R.layout.video_list, null );//这里面的video_list就是我们要更改的ListView布局。 SharedPreferences.Editor edit = share.edit();//编辑文本edit.clear().commit();//这边一定要clear之后,要提交,否则只是clear了其中的内容不提交的话,内容也不会有所修改的convertView.setTag( viewholder );}else { viewholder = (ViewHolder)convertView.getTag( ); } viewholder.textview = (TextView)convertView.findViewById( R.id.video_url );//ListView中的TextView控件,代表视频路径 viewholder.imageview =(ImageView)convertView.findViewById(R.id.video_imageview);//ListView中的ImageView控件,代表视频图像 viewholder.textview.setText( data.get(position).get("video_path").toString()); viewholder.imageview.setImageBitmap( (Bitmap) data.get(position).get("video_pic")); str_pattern = share.getString("words_pattern", "custom");//得到存储的数据中的各个字段,str_pattern字体类型,size字体大小,str_style字体风格,str_color字体的颜色。 size = share.getInt("words_size", 18 ); str_style = share.getString("words_style", "标准"); str_color = share.getString("words_color", "黑色"); SettingWords setting = new SettingWords( str_pattern,size,str_style,str_color );//SettingWords是我封装好的一个设置字体属性的类,setting.SettingStyle(viewholder.textview );//设置TextView字体的各种属性 return convertView; }}其中SharePereferences类来存储我们选择的字体属性的到jkwords.xml中,在此类中只是直接使用存储的数据。
然后,在主程序中,我们直接使用这个适配器就可以了。
private SharedPreferences share ;private ListView videolist ;private MySimpleAdapter videoadapter;在onCreate()方法中: share = super.getSharedPreferences("jkwords",Activity.MODE_PRIVATE);//保存修改字体的信息到文本中 videoadapter = new MySimpleAdapter( this, list, R.layout.video_list, new String[]{ "video_pic","video_path"}, new int[]{ R.id.video_imageview,R.id.video_url}); videoadapter.setViewBinder( new ViewBinder(){@Overridepublic boolean setViewValue(View view, Object data,String textRepresentation) {// TODO Auto-generated method stubif( (view instanceof ImageView)&&( data instanceof Bitmap ) ){//instanceof 判断某一个实例是否为某种特定类型ImageView imageview = ( ImageView )view;imageview.setImageBitmap( (Bitmap)data);return true;}return false;}});videolist.setAdapter(videoadapter);后面的弹出窗口代码略过,下面贴出当用户选择了字体的属性后,点击确定按钮的事件:
okay_btn.setOnClickListener( new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stub RadioButton rb1 = (RadioButton)SearchResultActivity.popView.findViewById(wordpattern_radio.getCheckedRadioButtonId()); String str_pattern = rb1.getText().toString() ; RadioButton rb2 = (RadioButton)SearchResultActivity.popView.findViewById(wordsize_radio.getCheckedRadioButtonId());int size = Integer.parseInt( rb2.getText().toString() );RadioButton rb3 = (RadioButton)SearchResultActivity.popView.findViewById(wordstyle_radio.getCheckedRadioButtonId());String str_style = rb3.getText().toString( );RadioButton rb4 = (RadioButton)SearchResultActivity.popView.findViewById(wordcolor_radio.getCheckedRadioButtonId());String str_color = rb4.getText().toString();SharedPreferences.Editor edit = share.edit( );//编辑文本edit.putString("words_pattern", str_pattern);//保存字符串edit.putInt("words_size", size);edit.putString("words_style", str_style);edit.putString("words_color", str_color);edit.clear().commit();//删除旧数据后,提交更新// SettingWords setting = new SettingWords( str_pattern,size,str_style,str_color );//设置字体的各种属性 videoadapter.notifyDataSetChanged( );//当执行nofityDataSetChanged方法时,就是调用了getView()方法。 popWin.dismiss( );//弹出窗口消失 }});这样就可以动态改变字体的属性了。
图1
图2
这样getView()调用的太频繁了,也将设置字体属性的代码放在外部,不要直接放在getView( )中。
- ListView中动态更改控件属性
- Android动态更改ListView的内部控件的值
- svg中动态更改text属性值
- 关于ListView中Item点击事件,动态改变Item中控件属性
- 动态更改class属性
- 动态更改控件ID
- ListView中item控件修改其他控件属性
- 如何实现Fragment中控件动态控制ListView子控件属性
- ListView-更改控件显示样式
- Android中动态的更改selector中某张图片的属性
- wpf中动态更改控件或元素的样式
- delphi窗体动态设计 在系统运行时动态更改控件属性
- listview控件属性详解
- ListView动态添加控件
- ListView动态添加控件
- 容器控件中动态加载控件+ispostback属性说明
- WPF中控件ListView和DataGrid典型属性介绍
- 使用ListView控件动态显示Excel表中
- java中方法的调用
- linux常用命令(2)
- js操作select 包括增加删除选项
- uva 11027 - Palindromic Permutation(组合数)
- ubuntu 下使用游戏手柄
- ListView中动态更改控件属性
- http://dl-ssl.google.com/android/eclipse/ 无法访问的解决办法
- linux下修改时间戳---touch
- COcos2d-X 节点(CCScheduler.h)API
- android开发环境搭建
- 非程序员的人生__大四小子的那个啥
- Convert Sorted Array to Binary Search Tree
- 使用Mybatis3.1.1时J2EE容器启动不起来而且不报错
- MySql5,自带AES加密与hibernate相结合